Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update Statement using IF or Else

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello everyone I am not sure how to write this sql statement correctly,please help.

I have 4 columns below
The 2 left columns are flags that can be set as Y or N depending on the values in the right columns

If any column on the right is not null
the flags on the left should read AnyDet = 'Y' and NoDet = 'N'

If both columns on the right are null
the flags should read AnyDet = 'N' and NoDet ='Y'

col-1 col-2 col-3 col-4
AnyDet - NoDet DocDet DetDet

I am not sure how to right this please help, I have been stuck on this for 2 days, not sure how to proceed
Thank you for reading this post.

 
You don't mention what should happen when neither of the right two columns are NULL.

Assuming that there should be no change in those columns in that situation, how about
Code:
update [TABLENAME]
set [AnyDet] = case when [DocDet] is NULL and [DetDet] is NULL then 'N' when [DocDet] is NULL or [DetDet] is NULL then 'Y' else [AnyDet] end,
[NoDet] = case when [DocDet] is NULL and [DetDet] is NULL then 'Y' when [DocDet] is NULL or [DetDet] is NULL then 'N' else [NoDet] end
?

soi la, soi carré
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top