Result of Max() function for the varchar datatype in SQL

  • Hi,

    What is result of the query if i use max function for the field which datatype is varchar

    Ex:

    Select Max(sname) from Student

  • You will get the student with last character of your alphabet, probably Z.

    Zanzibar is MAX over Sweden. It's the way they are sorted.


    N 56°04'39.16"
    E 12°55'05.25"

  • hi

    its simply returns the last sname in alphabetical order....

    Its something similar to

    select top 1 sname from students order by sname desc

  • To be a little more specific:

    to find the max sname is like searching for the largest ASCII value of the first position of sname, continues moving to the right for ties.

    SELECT max(sname) from(

    select char(8) + 'Zanzibar' AS sname UNION ALL

    select 'Zanzibar' UNION ALL

    select 'zanzibar' UNION ALL

    select ' Zanzibar' UNION ALL

    select '_Zanzibar' UNION ALL

    select '1Zanzibar' UNION ALL

    select '9anzibar' UNION ALL

    select '~anzibar') t



    Lutz
    A pessimist is an optimist with experience.

    How to get fast answers to your question[/url]
    How to post performance related questions[/url]
    Links for Tally Table [/url] , Cross Tabs [/url] and Dynamic Cross Tabs [/url], Delimited Split Function[/url]

  • The sorting order is defined by the collation used.

    See Working With Collations

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

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