Sorting NVARCHAR with ' and - symbols is not sorted properly?

  • Hi there,

    :hehe: Ive been playing around with my code and I noticed this sorting disorder.

    the symbols ' and - are not detected when sorting nvarchar values.

    Example:

    DECLARE @tbl TABLE (Item NVARCHAR(MAX))

    INSERT INTO @tbl

    SELECT 'q-Ant'

    UNIONSELECT 'q----------Dog'

    UNIONSELECT 'q''''''''''Cat'

    UNIONSELECT 'Quatrei'

    UNIONSELECT 'q-Zebra'

    --USING VARCHAR

    SELECT * FROM @tbl

    ORDER BY CAST(Item AS VARCHAR(MAX))

    --USING NVARCHAR

    SELECT * FROM @tbl

    ORDER BY Item

    VARCHAR would produce this sorted data...

    q'''''Cat

    q----------Dog

    q-Ant

    q-Zebra

    Quatrei

    While NVARCHAR produces this... uhhh... sorted data...

    q-Ant

    q'''''Cat

    q----------Dog

    Quatrei

    q-Zebra

    This isn't really a big issue for me (for now)... just noticed... what do you guys think? :hehe:

    _____________________________________________
    [font="Comic Sans MS"]Quatrei Quorizawa[/font]
    :):D:P;):w00t::cool::hehe:
    MABUHAY PHILIPPINES!

    "Press any key...
    Where the heck is the any key?
    hmmm... Let's see... there's ESC, CTRL, Page Up...
    but no any key"
    - Homer Simpson
  • I'm pretty confident SQLserver doesn't sort wrong.

    Have a look at the binary values of your columns and it'll be clear why sorting is resulting in the set you get to see.

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

  • :w00t: Yeah, I've thought about binary, just thinking... wonder what's on their mind why they decided to implement this. XD

    _____________________________________________
    [font="Comic Sans MS"]Quatrei Quorizawa[/font]
    :):D:P;):w00t::cool::hehe:
    MABUHAY PHILIPPINES!

    "Press any key...
    Where the heck is the any key?
    hmmm... Let's see... there's ESC, CTRL, Page Up...
    but no any key"
    - Homer Simpson
  • Ware still restricted to 0 and 1 😉

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply