Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jones_d
Yak Posting Veteran
61 Posts |
Posted - 2004-12-01 : 15:22:16
|
| Hi,I have a table (test_table) with 2 recordsCODE Name296 PAT296 PatI then run the following SQL against the table:SELECT [CODE], MIN([NAME]) FROM test_table GROUP BY [CODE]The value returned is: 296 PATEven if I change the MIN function to MAX - I still get back the result PAT.Using the MIN function I would've expected Pat to be returned.Does anyone know why this isn't working properly? |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-12-01 : 15:49:45
|
We were just having that conversation...sort ofhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=43121USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(CODE int,[Name] varchar(10))GOINSERT INTO myTable99(CODE, [Name])SELECT 296, 'PAT' UNION ALLSELECT 296, 'Pat'GOSELECT * FROM myTable99SELECT [CODE], MIN([NAME]COLLATE SQL_Latin1_General_Cp1253_CS_AS),MAX([NAME]COLLATE SQL_Latin1_General_Cp1253_CS_AS)FROM myTable99GROUP BY [CODE]GOSET NOCOUNT ONDROP TABLE myTable99GO Brett8-) |
 |
|
|
jones_d
Yak Posting Veteran
61 Posts |
Posted - 2004-12-02 : 05:31:29
|
| Thanks Bret,The compatibility level of my database is 70 so the COLLATE syntax doesn't work.I changed it to 80 to test your code and it works fine.However, I need to keep the compatibility level at 70 for backward compatibility. The MIN function works fine on SQL Server 7.0The database is now on SQL 2000 and it doesn't work.Do you have any other suggestions? |
 |
|
|
Bustaz Kool
Master Smack Fu Yak Hacker
1834 Posts |
Posted - 2004-12-04 : 15:53:37
|
| If your collation is set to be case insensitive then the two PAT's are equal. The MIN or MAX function could return either one with equal justification.To solve your dilemma, you could CONVERT the string to a BINARY format and then perform the MIN/MAX.SELECT Code, Min(convert(varbinary, Name))HTH=========================================Let X = {All sets s such that s is not an element of s}(X element of X) ==> (X not element of X)(X not element of X) ==> (X element of X) (Bertrand Russell Paradox) |
 |
|
|
|
|
|
|
|