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
 General SQL Server Forums
 New to SQL Server Programming
 compare dates string and int

Author  Topic 

tonyz
Starting Member

5 Posts

Posted - 2007-07-05 : 05:52:20
need to look through hundreds of rows and compare two columns, and pull out rows where the date difference is 7 days

problem is one column stores the date as a string and values in that column look like this:

01/12/2007 10:15 pm
or
01/12/2007

the other column stores the date as seconds since 1/1/1970, which is good and easy to work with..

how do I write the select?
any ideas
thnx
tony

nr
SQLTeam MVY

12543 Posts

Posted - 2007-07-05 : 06:01:51
select *
from tbl
where abs(datediff(dd,convert(datetime, col1, 103), dateadd(ss,col2,'19700101'))) = 7

assumning 01/12/2007 = 1 dec 2007.


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

tonyz
Starting Member

5 Posts

Posted - 2007-07-05 : 06:17:41
thnx nr

yes 01/12/2007 is 1 dec 2007.

but will it handle the string:

01/12/2001 10:15 pm

the column containing string dates contains both.. I previuously recall seeing an error when using datetime with the string 01/12/2001 10:15 pm

tony
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-07-05 : 06:46:02
SELECT *
FROM Table1
WHERE DATEDIFF(SECOND, '19700101', Col1) - Col2 BETWEEN -604800 AND 604800


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -