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 2000 Forums
 Transact-SQL (2000)
 Date Comaprison

Author  Topic 

nimmie82
Starting Member

4 Posts

Posted - 2005-07-18 : 06:51:51
I have a problem with the following code...cud any one pls help me out in solving it.

here is the code:

declare @d1 datetime,@d2 datetime
set @d1='7/19/05'
select @d2=getdate()
if @d1=@d2
begin
print 'Both r equql'
end
else if @d1<@d2
begin
print 'D1 less than D2'
end
else if @d1 > @d2
begin
print ' D1 greater than D2'
end
go

when the dates are equal...i'm getting date is less....!

d1 = 7/18/05
getdate = 7/18/05 ....still i'm getting d1 is less tahn d2

how to solve.


madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-07-18 : 06:58:07
As as alternative, try this

declare @d1 datetime,@d2 datetime
set @d1='7/19/05'
select @d2=getdate()
if datediff(d,@d1,@d2)=0
begin
print 'Both r equql'
end
else if datediff(d,@d1,@d2)>0
begin
print 'D1 less than D2'
end
else if datediff(d,@d1,@d2)<0
begin
print ' D1 greater than D2'
end
go


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2005-07-18 : 10:01:08
getDate() returns date and time, so when you compare it with date without time it will almost certenly be different. Take a look at madhivanan's script.
Go to Top of Page
   

- Advertisement -