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)
 Query with different time formats

Author  Topic 

mauler05
Starting Member

17 Posts

Posted - 2007-10-12 : 15:16:16
Hi Guys,

I have a SSIS datetime variable createDate ......When i read this varaiable it contains date in the following format 11/10/2005 05:32:46 PM

When i compare it to the dates thats are stored in some table i dont get any match as the dates are stored in 2005-11-10 17:32:46.893 format. I can work around by just comparing the datepart but my problem is that i have to take time into consideration and also add some fuzzy time to that to fetch records...

Can anyone let me know how to comapre two dates making sure time in consideration and how to comapre 05:32:46 PM and 17:32:46.893 kind of records

I appreciate any help in advance

Kristen
Test

22859 Posts

Posted - 2007-10-12 : 17:33:51
Always store date / time data in a DATETIME Datatype. If you use, for example, Varchar you have no ability to perform date calculations or compare dates etc.

Convert string date to datetime datatype using

CONVERT(datetime, MyStringDate, nnn)

where "nnn" is a number representing the format (e.g. d/m/y or m/d/y). See Books Online for further details.

To compare two dates which also include a time element on the basis of "whole day" comparison you need to check that the first is with a range of Midnight-night-before to Midnight-night-following.

So

11/10/2005 05:32:46 PM

is within the range of >= "20051110 00:00:00.000" AND < "20051111 00:00:00.000"

To "round" a date, including time, to only a date-at-Midnight use:

DATEADD(Day, DATEDIFF(Day, 0, MyDate), 0)

this tortuous looking formula is the fastest available, because all the date calculation are maintained as DATETIME, rather than (say) converting to String and Back again, which is slower.

"I can work around by just comparing the datepart"

Best not to use the Date's Parts. That is slow (particularly if the DateTime Column is indexed - applying a Function tot he DateTime Column will prevent the index being used)

So for a worked example:

DECLARE @strDate1 varchar(50),
@strDate2 varchar(50)

SELECT @strDate1 = '11/10/2005 05:32:46 PM',
@strDate2 = '2005-11-10 17:32:46.893'

DECLARE @Date1 datetime,
@Date2 datetime

SELECT @Date1 = CONVERT(datetime, @strDate1, 101), -- 101 = mm/dd/yyyy
@Date2 = CONVERT(datetime, @strDate2, 121) -- 121 = yyyy-mm-dd hh:mi:ss.mmm

SELECT [@strDate1] = @strDate1,
[@strDate2] = @strDate2,
[@Date1] = @Date1,
[@Date2] = @Date2,
[tMidnightBefore] = DATEADD(Day, DATEDIFF(Day, 0, @Date2), 0),
[tMidnightFollowing] = DATEADD(Day, DATEDIFF(Day, 0, @Date2)+1, 0)

SELECT 'Yes'
WHERE @Date1 >= DATEADD(Day, DATEDIFF(Day, 0, @Date2), 0)
AND @Date1 < DATEADD(Day, DATEDIFF(Day, 0, @Date2)+1, 0)

Kristen
Go to Top of Page

mauler05
Starting Member

17 Posts

Posted - 2007-10-15 : 15:31:27
Thanks ...
Go to Top of Page
   

- Advertisement -