1

DateTime Should update if the data is passed else update it with original value which is already saved. This update does not works

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
  SET   
      FolderStatusDate = ISNULL(@FolderStatusDate, FolderStatusDate)
      WHERE Id = @Id  
4
  • 1
    The question is tagged SQL Server. AFAIK, ifnull is MySql and is not available in SQL Server Commented Jun 11, 2014 at 4:15
  • 2
    The script in the question will not work because @id is neither declared nor set, but the query seems correct. What "does not works" mean? You get an error, the data is not updated when it should, or the opposite, or what else? Commented Jun 11, 2014 at 8:12
  • Side note: It's a good habit to get into to write "literal" dates with T as the separator rather the space, e.g. '2012-07-04T14:09:04.043' - '2012-07-04 14:09:04.043' can be interpreted (under weird circumstances) as either yyyy-mm-dd... or yyyy-dd-mm... Commented Jun 11, 2014 at 9:54
  • To expand on @Damien's point, there's an alternative way to represent a timestamp unambiguously – just omit the hyphens (-) in the date part: 20120704 14:09:04.043. It is true that either method makes the original form less readable, but I believe consistency is more important and at least you've got more than one unambiguous format to choose from. Commented Jun 11, 2014 at 10:35

3 Answers 3

1

Why don't you move the check for NULL to the WHERE clause?

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
  SET   
      FolderStatusDate = @FolderStatusDate
      WHERE Id = @Id  
      AND @FolderStatusDate IS NOT NULL
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't the IS NULL check be on @FolderStatusDate? (and be IS NOT NULL)
Your query is identical to the OP's in terms of the final data state. The only difference is that your query would skip updating any rows if @FolderStatusDate was NULL instead of overwriting the column with same values. Seems a good measure in terms of performance optimisation but how does it solve the problem at hand (which is not even clear at this point)?
0

You can also do it this way

DECLARE @FolderStatusDate DATETIME = NULL

SET @FolderStatusDate = '2012-07-04 14:09:04.043'

UPDATE CM.PfmFolder     
SET   
  FolderStatusDate = case when ISNULL(@FolderStatusDate, '') = '' 
                           then FolderStatusDate else @FolderStatusDate end
  WHERE Id = @Id

Comments

0

A slight edit on Hitesh Salian's answer

 DECLARE @FolderStatusDate DATETIME = NULL

 SET @FolderStatusDate = '2012-07-04 14:09:04.043'

 UPDATE CM.PfmFolder     
 SET   
 FolderStatusDate = case @FolderStatusDate is Null
                       then FolderStatusDate else @FolderStatusDate end
 WHERE Id = @Id

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.