String Comparison in T-SQL

Posted on 2004-11-30
Last Modified: 2012-05-05
Is it "legal" to compare strings in T-SQL such as what I am doing here?  I am having some issues with our accounting system not reflecting my "mappings" below and I was wondering if there was an issue with my code.  Another pair of eyes would be greatly apprecieated.

update dbo.loadfile
set = 206002, dbo.loadfile.product = 21, dbo.loadfile.acct = 59002
where = 'VNG0'
      and ((dbo.loadfile.product >= '2300' and dbo.loadfile.product <= '5550')
            or (dbo.loadfile.product >= 'A021' and dbo.loadfile.product <= 'A388')
            or (dbo.loadfile.product >= 'D290' and dbo.loadfile.product <= 'FSTD')
            or dbo.loadfile.product in ('HNDG','K849','RMCO','RMPK','RMCT','RMPP','RMPE','RMLA','RMMM'))
      and dbo.loadfile.acct = 54110;

update dbo.loadfile
set = 206002, dbo.loadfile.product = 80000277, dbo.loadfile.acct = 59002
where = 'VNG0'
      and ((dbo.loadfile.product >= 'LITR' and dbo.loadfile.product <= 'LLTM')
            or dbo.loadfile.product in ('RMKT','RMLC','TCSV','ZSID'))
      and dbo.loadfile.acct = 54110;
Question by:ltrain2015
    LVL 16

    Accepted Solution

    Yes it is legal. The only thing to bear in mind, is that where you do:

    dbo.loadfile.product = 80000277

    if product is a text field, SQL Server's default approach is to attempt to cast all the values in the product column to integer values in order to do the comparison. If it does not contain exclusively numerical values, you will get an error. In this case, you should use:

    dbo.loadfile.product = '80000277'
    LVL 18

    Expert Comment

    concur with muzzy
    LVL 50

    Expert Comment

    i think it really depends on what your case sensitivity is  for the database....

    also do you really wish to allow '2300       zzz'  as a value?

    since its an accounting system you may be better off categorising those values on a separate table
    to allow for more auditablity...

    LVL 16

    Expert Comment

    And collation, since the comparison contains inequalities with strings. Of course, you can cast (right word?) the value into the right collation before making the comparison to achieve the results you want anyway.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now