How can I execute an Immediate IF in a SQL statement? (t-SQL)

I have a file, it has (5) check amount fields. I need to update the record with a field that will give the valid number of checks in the record.  The possibilities are; 2 checks, or 5 checks.  I can assume that if "check amount 3" has a zero value, expressed as '000.00' then it is a (2) check record, and if it is not '000.00' then it is a (5) check record.

I need to do this query along with several other updates at the same time.  Is there a way to do something along the lines of a (iif check_number_3 = '000.00,'2','5')? inside an update statement?
TommyMac501Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Scott PletcherConnect With a Mentor Senior DBACommented:
Best is to use a computed column, which SQL will not store but will automatically (re)compute every time you need to use the value.  For example:

ALTER TABLE tableName
ADD CheckCount AS CASE WHEN [Check Amount 3] = 000.00 THEN 5 ELSE 2 END

From then on, you can use CheckCount just like any regular column.
0
 
Snarf0001Connect With a Mentor Commented:
update <<tablename>> set <<columnname>> = case when check_number_3 = '000.00' then 2 else 5 end
0
 
TommyMac501Author Commented:
Thank you both, this solution worked great.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.