?
Solved

String Comparison in T-SQL

Posted on 2004-11-30
4
Medium Priority
?
4,943 Views
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 dbo.loadfile.cc = 206002, dbo.loadfile.product = 21, dbo.loadfile.acct = 59002
where dbo.loadfile.cc = '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 dbo.loadfile.cc = 206002, dbo.loadfile.product = 80000277, dbo.loadfile.acct = 59002
where dbo.loadfile.cc = '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;
-----------------------------------------------------------------------------------------------------
0
Comment
Question by:ltrain2015
  • 2
4 Comments
 
LVL 16

Accepted Solution

by:
muzzy2003 earned 500 total points
ID: 12706308
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'
0
 
LVL 18

Expert Comment

by:ShogunWade
ID: 12707327
concur with muzzy
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 12707906
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...



0
 
LVL 16

Expert Comment

by:muzzy2003
ID: 12707988
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.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

621 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