update date using case

Posted on 2012-09-14
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
Question by:VBdotnet2005
    LVL 65

    Accepted Solution

    (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
    LVL 75

    Assisted Solution

    by:Aneesh Retnakaran
    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
    LVL 32

    Assisted Solution


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

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    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 …
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    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
    Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now