Link to home
Start Free TrialLog in
Avatar of FranklinRaj22
FranklinRaj22

asked on

Compare time stamps in MSSQL

1. I have to compare a timestamp field against a static (hardcoded time stamp) . Can u please advice.
eg Timestamp_fied > '2009-03-22 00:00:00.000'  sometin lik this but i don get the correct values.

2. I also would like to dynamically compare the same timestamp field againsnt the current timestamp minus n days
eg Timestamp_fied > current_timestamp-1 ,sometin lik this but i don get the correct values.
Avatar of Chris Luttrell
Chris Luttrell
Flag of United States of America image

If you are refering to a column with the DATA_TYPE of TIMESTAMP then it is not a date value.  See http://msdn.microsoft.com/en-us/library/ms182776(SQL.90).aspx for more details on TIMESTAMP.  Comparing your string to a column of type DATETIME works for me so your problem must be TIMESTAMP.
How about casting the static timestamp in question?
SELECT *
where timestamp_field > CAST('2009-03-22 00:00:00.000'  AS TIMESTAMP(0) FORMAT 'DD/MM/YYYYBHH:MI:SS');

Hi,
U can perform mathematical operations on Timesatmp in MSSQL.

Adding dates and subtracting dates can be achieved like simple addition.
< , > <= , >= and != can be use for comparinson also.

select Current_TIMESTAMP - 1
select Current_TIMESTAMP - 1 day
select Current_TIMESTAMP +  10 day
select Current_TIMESTAMP - 1 month
 
 
SELECT * FROM
NyTable WHERE MyDate > = GETDATE()
 
SELECT * FROM
NyTable WHERE MyDate > = Current_TIMESTAMP - 1 month

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of csenasa
csenasa
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of FranklinRaj22
FranklinRaj22

ASKER

ok
ok?  What does that mean after all this time?  Did one of the comments help you, if so please reward that answer.  If you still have problems or questions let us know so we can try to help.