[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

update date using case

Posted on 2012-09-14
3
Medium Priority
?
429 Views
Last Modified: 2012-09-27
What am I doing wrong here?

@username       varchar(12)



update mytable
      set date_sent = case when date_sent  = null then current_timestamp end
      set username  = case when username = '' then @username
      where id = @id
0
Comment
Question by:VBdotnet2005
3 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 668 total points
ID: 38399639
(1)  Use IS NULL instead of = NULL
(2)  You need commas between the columns you are updating
(3) The END at the end of the username CASE block was missing

UPDATE mytable
SET date_sent = case when date_sent  IS NULL then current_timestamp end,
      username  = case when username = '' then @username END
WHERE id = @id
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 668 total points
ID: 38399654
you need to add the else condition too

UPDATE mytable
SET date_sent = case when date_sent  IS NULL then current_timestamp else date_sent end,
      username  = case when username = '' then @username else username END
WHERE id = @id
0
 
LVL 32

Assisted Solution

by:Ephraim Wangoya
Ephraim Wangoya earned 664 total points
ID: 38400132
try

update mytable 
      set date_sent = isnull(date_sent, current_timestamp) ,
      username  = case 
				when username = '' then
					@username
				else 
					username
				end	
      where id = @id

Open in new window

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how the fundamental information of how to create a table.
Suggested Courses

873 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