Can''t insert certain charcters into SQL table

  • I ran below script in Query ananlyzer 

    insert into lookup_tbl values('L,l,L,l')

    However When I ran  

    select * from Lookup_tbl

    I get the following result.

    L,l,L,l

    In addition, I was able to add following 'Lall' into the DB using in-house application.

    However, when I search for the string in the DB, even though data exists, I was not able to find the string in the DB. In fact, SQl returned me no results.

    select name_preferred from person_tbl where name_preferred = 'Lall'

    No result was returned. I am really puzzled. Can anyone help?

     

  • I'm puzzled as well.

    What's wrong with your resultset from Lookup_tbl?

    What did you expect?

    _____________
    Code for TallyGenerator

  • The answer is the difference between the single-quote character:

    '

    as a part of a string value, as for example ,

    lastname
    --------
    O'Toole
     
    (which is part of the data)
    and as part of a string literal, for example:

    set @string = 'Burton'.

    (Where the two single-quote characters occur as part of the code, which tells the SQL parser that what is between them is a string value.)
    If you try to execute

    set @string = 'O'Toole'

    then the SQL parser thinks that you are saying

    set @string = 'O'

    and then doesn't know what to do with the rest of your code:

    Toole'

    So in order to specify the data value

    '

    inside a string literal, you have to replace it with two single-quote characters:

    set @string = 'O''Toole'

    When you select @string, you will get:
     
    --------
    O'Toole

    Because you were typing into the front end, the application treated your single-quotes as part of the data. To search for the string you entered, you would have to use:

    select

    name_preferred from person_tbl where name_preferred = '''Lall'''

    Tim Wilkinson

    "If it doesn't work in practice, you're using the wrong theory"
    - Immanuel Kant

  • If that's a case I would suggest to get familiar with the syntax of the programming language before trying to use it.

    BTW, it's not about only TSQL, it's about any language.

    _____________
    Code for TallyGenerator

  • And what better way of doing so than calling on the help of members of this community?

  • The apostrophe trips up many a DBA.  See discussion http://www.sqlservercentral.com/forums/shwmessage.aspx?forumid=145&messageid=290168

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

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