Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 comparing dates

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2014-12-22 : 10:11:13
Why don't I get that print?


declare @prevrequiredDate as date = '12/22/2014',
@requiredDate as date,
@actioninfo as varchar(100)
select @prevrequiredDate, @requiredDate
if (@prevrequiredDate <> @requiredDate)
begin
select @actioninfo = 'Changed from ' + CONVERT(varchar(10), @prevrequireddate, 10) + ' to ' + CONVERT(varchar(10), @requiredDate, 10)
print @actioninfo
end


Dave
Helixpoint Web Development
http://www.helixpoint.com

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-22 : 10:23:03
@requireDate is never set, so it is NULL, so the condition in the if statement evaluates to false.
Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2014-12-22 : 10:30:10
Not true. Put null in there. I need this to eval as a true statement

Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-12-22 : 17:45:26
if (@prevrequiredDate <> @requiredDate) or (@requiredDate is null)
Go to Top of Page

MuralikrishnaVeera
Posting Yak Master

129 Posts

Posted - 2014-12-29 : 00:21:35
[code]
declare @prevrequiredDate as date
SET @prevrequiredDate = '12/22/2014'
declare @requiredDate as date
SET @requiredDate = NULL
declare @actioninfo as varchar(100)
select @prevrequiredDate, @requiredDate
if (@prevrequiredDate <> (ISNULL(@requiredDate,'')))
BEGIN
select @actioninfo = 'Changed from ' + CONVERT(varchar(10), @prevrequireddate, 10) + ' to ' + CONVERT(varchar(10), ISNULL(@requiredDate,''), 10)
PRINT @actioninfo
END
[/code]

---------------
Murali Krishna

You live only once ..If you do it right once is enough.......
Go to Top of Page

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-12-29 : 09:04:49
quote:
Originally posted by helixpoint

Not true. Put null in there. I need this to eval as a true statement

Dave
Helixpoint Web Development
http://www.helixpoint.com



What is "not true"? I see a declaration for the variable. I cannot see anywhere where the variable is set. So, it is most certainly "true" that the the value of the variable is NULL when you test it in the if-statement.

Note: you can only test for null values using the "IS NULL" or "IS NOT NULL" expressions.
Go to Top of Page
   

- Advertisement -