1

I have a table with a column DateTime datatype with default value null .

I am using following code to update the Datetime to proper datetime , but it is not working .

My code as below

declare @SomeDate datetime = GETDATE()

update Testing set SomeDate = @SomeDate  where  SomeDate  <> @SomeDate 
0

4 Answers 4

1

Try below code

declare @SomeDate datetime = GETDATE()

update Testingset 
set SomeDate = @SomeDate  
where  SomeDate  is null
or SomeDate  <> @SomeDate 
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

UPDATE Testingset
   SET SomeDate = @SomeDate
 WHERE SomeDate IS NULL
    OR SomeDate != @SomeDate

1 Comment

Hi , I want to make sure the statement run only when the value is different or null
1

I am guessing that your code is not doing what you want. And your code should be:

declare @SomeDate datetime = GETDATE();

update Testingset
    set SomeDate = @SomeDate 
    where SomeDate is null;

There is no apparent reason for using a variable. You might as well do:

update Testingset
    set SomeDate = getdate()
    where SomeDate is null;

EDIT:

Based on your comment, you seem to want:

declare @SomeDate datetime = GETDATE();

update Testingset
    set SomeDate = @SomeDate 
    where SomeDate is null or SomeDate <> @SomeDate;

1 Comment

Hi , I want to make sure the statement run only when the value is different or null
0

I believe the following would accomplish what you're trying to do:

UPDATE Testing
   SET SomeDate = getdate()
 WHERE SomeDate IS NULL
    OR SomeDate <> getdate();

You might do better to use the ANSI standard CURRENT_TIMESTAMP over getdate():

UPDATE Testing
   SET SomeDate = CURRENT_TIMESTAMP
 WHERE SomeDate IS NULL
    OR SomeDate <> CURRENT_TIMESTAMP;

2 Comments

The OR here is really kind of meaningless unless you happen to have a row with the EXACT value as getdate() or CURRENT_TIMESTAMP.
That's true, I wasn't thinking. One may as well eliminate the entire WHERE clause.

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.