Solved

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

Posted on 2007-11-14
3
817 Views
Last Modified: 2010-04-21
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?
0
Comment
Question by:TommyMac501
3 Comments
 
LVL 22

Assisted Solution

by:Snarf0001
Snarf0001 earned 50 total points
ID: 20284993
update <<tablename>> set <<columnname>> = case when check_number_3 = '000.00' then 2 else 5 end
0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 75 total points
ID: 20285083
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
 

Author Closing Comment

by:TommyMac501
ID: 31409249
Thank you both, this solution worked great.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

707 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

18 Experts available now in Live!

Get 1:1 Help Now