How do I do a boolean string comparison in SQL Server 2008

Lawrence

In simple terms I want to add the contents of some columns together but only if the associated columns contain a specific value. To do this in MS Access I use the following:

STRUint:(Abs(1*([Code01]='0192'))*[Code01int])+(Abs(1*([Code02]='0192'))*[Code02int])...

Breaking this down, if Code01='0192' I get the result TRUE which I multiply by 1 to get -1 and use Abs to convert to 1. This is then multiplied by Code01int, so I'd have an integer equal to Code01int only when Code01 was '0192' otherwise I'd have zero. The result of that calculation is added to the next calculation in the sequence and so on.

The sequence is repeated 15 times adding together the integers in Code01int through Code15int where the content of Code01 through Code15 is '0192'

The only way I personally can think how to do this in SQL is with

UPDATE Table1 
SET STRUint = 0

UPDATE Table1 
SET STRUint = CASE 
                 WHEN Code01='0192' THEN STRUint + Code01int 
              END

UPDATE Table1 
SET STRUint = CASE 
                 WHEN Code02='0192' THEN STRUint + Code02int 
              END
etc

Is this the only way to achieve my goal, by doing many UPDATEs? I do this for 15 sets of columns and repeat for different string content 5 times, so 80 updates (including the initial set to zero).

I'm hoping there's a way to do this without so many updates on the assumption that it's better to not do so many.

Richard Hansell

What's wrong with:

UPDATE Table1 SET STRUint = 0;

UPDATE 
    Table1 
SET 
    STRUint += 
    CASE WHEN Code01='0192' THEN Code01int ELSE 0 END
    + CASE WHEN Code02='0192' THEN Code02int ELSE 0 END
    + CASE WHEN Code03='0192' THEN Code03int ELSE 0 END
etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I do a Date comparison in Javascript?

How do I concatenate a boolean to a string in Python?

How do I concatenate a boolean to a string in Python?

How do I do a case-insensitive string comparison?

How do I do multiple CASE WHEN conditions using SQL Server 2008?

Do while loop in SQL Server 2008

How do you create a yes/no boolean field in SQL server?

How do I check if a Sql server string is null or empty

How do I get all records where date is 6 months greater than today's date, using Microsoft SQL Server 2008?

How can I do a string comparison in a while loop in JavaScript

How do find a string in SQL Server database

How do i insert a date in Sql Server 2008?

How do I use SELECT on columns that don't have column names in SQL Server 2008 R2?

How do I Output into Self Specified Data like Yes or No into Self Specified Columns Based on the result of a Query SQL Server 2008

How do I format a number in T-SQL for SQL Server 2008 R2?

Silent Install of SQL Server 2008 R2 Still Shows UI to User - How do I keep the UI from showing?

SQL Server 2008 another way to do this

How do I loop through some XML data in SQL Server 2008?

How to do a "conditional comparison" in SQL Server?

How do I fetch a string from a server?

How to do IF...ELSE like programming in SQL Server 2008

How do I create query in sql server 2008 r2

How do I get the average of a count in Microsoft SQL Server 2008

How do I perform this kind of month-over-month comparison in SQL Server?

How do I perform a logic comparison using dynamic T-SQL with an empty string?

How do I to use Pivot in SQL Server?

How do I evaluate if a string is a number or boolean in Haxe?

How do i update sql value with sum comparison

How can i do a concat() in SQL 2008