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 2005 Forums
 Transact-SQL (2005)
 Problem comparing dates

Author  Topic 

techdawg270
Starting Member

4 Posts

Posted - 2010-08-25 : 15:59:18
I am trying to compare dates from two different tables and running into an issue. One of the tables has an associated time with the date, and the other does not. The code looks like this:
select gbd.client_id, gbd.val_date, cbd.date_added, cbd.date_updated
into #clientsToInactivate
from #tempGetBirthDate gbd
join #tempCheckBirthDate cbd
on gbd.client_id = cbd.uid
where gbd.val_date = cbd.date_added

An example of a date from #tempGetBirthDate would be 2003-10-07 00:00:00.000, and from #tempCheckBirthdate would be 2003-10-07 20:17:36.000. How can I go about ignoring the time stamp and do the comparison on just the yyyy-mm-dd?

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-08-25 : 16:09:19
where gbd.val_date = dateadd(dd,datediff(dd,0,cbd.date_added),0)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

techdawg270
Starting Member

4 Posts

Posted - 2010-08-25 : 16:30:47
You are a genius webfred
Thank you for your assistance
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-08-25 : 18:27:45
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

glendcruz
Yak Posting Veteran

60 Posts

Posted - 2010-08-29 : 00:29:29
The best solution is to convert the date to a string and back again to truncate the time information.
In the eg, If you alter the date you wont get any data displayed

Eg :
select * from <tablename>
where convert(varchaR(12),<column name of date type>, 101) = convert(varchar(12),<date to be compared>, 101)

Better to create a function and use it when required.

CREATE FUNCTION dbo.ComparingDates(@date datetime)
RETURNS varchar(12)
AS
BEGIN
RETURN CONVERT(varchar(12), @date,101)
END
-- Your example tried out in various stages

declare @tempGetBirthDate table
(
id smallint,
dob datetime
)
insert into @tempGetBirthDate
select 1,'2003-10-07 00:00:00.000'

declare @tempCheckBirthdate table
(
id smallint,
dob datetime
)
insert into @tempCheckBirthdate
select 1,'2003-10-07 20:17:36.000'

select * from @tempGetBirthDate
select * from @tempCheckBirthdate

select bd.dob as 'Birthdate', cd.dob as 'check date'
from @tempGetBirthDate bd
join @tempCheckBirthdate cd
on bd.id = cd.id
where dbo.ComparingDates(bd.dob) = dbo.ComparingDates(cd.dob)
----------------
(1 row(s) affected)

(1 row(s) affected)
id dob
------ -----------------------
1 2003-10-07 00:00:00.000

(1 row(s) affected)

id dob
------ -----------------------
1 2003-10-07 20:17:36.000

(1 row(s) affected)

Birthdate check date
----------------------- -----------------------
2003-10-07 00:00:00.000 2003-10-07 20:17:36.000

(1 row(s) affected)
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-08-29 : 06:53:23
quote:
The best solution is to convert the date to a string and back again to truncate the time information

I don't think that convert to string and back is THE BEST solution.
My given solution is stripping the time part without converting.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-29 : 07:02:06
Agreed. glendcruz's solution is not the best solution as it will be inefficient.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

glendcruz
Yak Posting Veteran

60 Posts

Posted - 2010-08-29 : 19:51:19
quote:
Originally posted by tkizer

Agreed. glendcruz's solution is not the best solution as it will be inefficient.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



I have been using this solution for quite some time and i know that it is not an effecient way. However if yo find a better way do let me have the code for it. i will appreciate it. Thanks
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-29 : 20:22:28
webfred posted the better solution already. See above.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2010-08-30 : 03:14:12
Well...the example is with temp-table and for this case my recommendation will be obsolete but if the two tables were permanent none of the two solutions are optimal. I agree that webfreds method is the better of the two but as far as I know it will still create a full table scan due to the dateadd/datediff-functions. If this comparison if done very frequently the best would be to create an additional column in the table that has the time also, either as a persisted computed column or just a regular column that is populated/updated along with the "parent"-column.

- Lumbago

My blog (yes, I have a blog now! just not that much content yet)
-> www.thefirstsql.com
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-08-30 : 12:49:17
webfred's solution will only have the scan on the cbd table, not the gbd table (assuming indexes are in place). With the other solution, it'll have issues on both tables.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -