How to make SQL statement "case sessible"?

Last post 07-18-2008 2:56 PM by limno. 2 replies.

Sort Posts:

  • How to make SQL statement "case sessible"?

    07-18-2008, 12:54 PM
    • Member
      13 point Member
    • marks416
    • Member since 07-06-2008, 3:12 AM
    • Posts 54

    Hi

    I have a login form use the  following sql statement

    select from user where username = "?" and password ="?"

    now the two users can login

    select from user where username = "TEST" and password ="pwd"

    select from user where username = "test" and password ="pwd" 

    The system see "TEST" same with "test",but I only allow "TEST" login.how to make the select statement case sensible?

     

    How to fix this?Thanks

     

    Mk 

     

     

     

     

     

  • Re: How to make SQL statement "case sessible"?

    07-18-2008, 2:01 PM
    Answer
    • Contributor
      2,980 point Contributor
    • santa_1975
    • Member since 06-30-2008, 6:20 AM
    • Posts 496

    Cast the user name and password to binary or varbinary datatype and compare as given below.

    syntax: 

    select 1 from user where CAST(username as varbinary(<length of username field>)) =  CAST(<user entered value for user name> as varbinary(<length of username field>))

    usage:

    select 1 from user where CAST(username as varbinary(20)) = CAST("TEST" as varbinary(20))

    and CAST(password as varbinary(10)) = CAST("pwd" as varbinary(10))

    Note: The basic idea here is that when you convert the string to binary/varbinary you get different values for "A" and "a" and it solves the issue.

    Hope this helps.

     

  • Re: How to make SQL statement "case sessible"?

    07-18-2008, 2:56 PM
    • All-Star
      87,071 point All-Star
    • limno
    • Member since 06-10-2005, 3:50 PM
    • Iowa, USA
    • Posts 4,947
    • Moderator
      TrustedFriends-MVPs

    Run this for your user column to make it case sensitive:

    ALTER TABLE user

    ALTER COLUMN username VARCHAR(50)

    COLLATE Latin1_General_CS_AS

    GO

    Limno

    Format your SQL query with instant sql formatter:
    http://www.dpriver.com/pp/sqlformat.htm
Page 1 of 1 (3 items)