Loginc compare char values using operator

  • Hi All

    What is the logic behind, When you compare string using logical operation >= , below are the some different examples.

    Actually, i was thinking i will convert into Ascii value and compare but it not true.

    If you have any information or document or link regarding this, please keep forward.

    Queries

    drop

    table tt

    create

    table tt

    (

    fname varchar(70),

    onechar

    char(1)

    )

    insert

    into tt values('azint','a')

    insert

    into tt values('hemang','h')

    insert

    into tt values('kumar','k')

    insert

    into tt values('manoj','m')

    insert

    into tt values('yanoj','y')

    select

    ascii(fname),ascii(onechar),* from tt

    select

    ascii(fname),ascii(onechar),ascii('m'),* from tt where fname >= 'm'

    select

    ascii(fname),ascii(onechar),ascii('mz'),* from tt where fname >= 'mz' --Query 1

    select

    ascii(fname),ascii(onechar),ascii('mz'),* from tt where ascii(fname) >= ascii('mz') --Query--2

    If It converts in ascii and compare than query(Query 1,Query--2) would have same result....but it is not.

    Any help will be appreciated

    Thank

    Manoj kumar

  • The ASCII() function only returns the ASCII integer value of the first character:

    select ascii('m'), ascii('mz')

    ----------- -----------

    109 109

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • And besides all that, the answer of what happens when you compare strings with >= entirely depends on your collation setting.

    under normal circumstances >= in varchar/nvarchar means further down the list in a descending sort

    like so:

    if 'aaaa' > = 'abbb'

    print 'aaaa is greater than or equal to bbbb'

    else

    print 'it isnt'

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

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