0

This is one of the most interesting issue that I have come across in quiet some time. I have a scalar function defined in SQL server 2008 which whose return type is varchar(max)

This is the query:

 Select dbo.GetJurisdictionsSubscribed(u.UserID) as 'Jurisdiction' From Users u ORDER BY Jurisdiction desc

Could anybody explain why would AAAA... 2nd record in the resultset? I am doing a descending sort, AAA... should appear at the last. If I change the query to

Jurisdiction asc

AAA goes 2nd last in the list instead of the 1st record.

This is the screenshot of the resultset: http://i48.tinypic.com/23j5vzq.jpg

Am I missing something?

1

4 Answers 4

4

That is the correct sort order. You have spaces. You must read Case Sensitive Collation Order.

1
  • Thanks! Don't know why couldn't I see the space! I wasted nearly 1/2 hr and still couldn't make out that there was space :)
    – TCM
    Dec 11, 2012 at 15:12
3

Because, as you can see in your screenshot, they are a white space in other rows before 'Wise' word (and withe space is greater than 'A')

You can left trim this spaces with:

ORDER BY ltrim( Jurisdiction ) desc
0
0

notice the leading white spaces, try

SELECT ...
FROM ...
ORDER BY LTRIM(Jurisdiction) desc

LTRIM would be fine.

1
  • Is there a Trim function? I guess you meant LTrim(RTrim())
    – TCM
    Dec 11, 2012 at 15:11
0

It's a bit hard to tell on the screenshot, but can you check the length of the values because I think some of them have a leading space. If so then they would be sorted correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.