• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

update date using case

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
VBdotnet2005
Asked:
VBdotnet2005
3 Solutions
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
(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
 
Aneesh RetnakaranDatabase AdministratorCommented:
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
 
Ephraim WangoyaCommented:
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
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.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now