Solved

Compare time stamps in MSSQL

Posted on 2009-03-30
7
1,250 Views
Last Modified: 2012-05-06
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.
0
Comment
Question by:FranklinRaj22
7 Comments
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 24024095
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.
0
 
LVL 9

Expert Comment

by:tl121000
ID: 24024137
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');

0
 
LVL 3

Expert Comment

by:csenasa
ID: 24025897
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

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 3

Accepted Solution

by:
csenasa earned 500 total points
ID: 24025918
Hi
U can use system functions like Dateadd() and DateDiff()
SELECT DATEADD(day, 3, GETDATE());
 

GO

SELECT DATEADD(day, 3, DueDate)

FROM Sales.SalesOrderHeader;

GO
 

SELECT interval = DATEDIFF(month,  'Nov 30 2001' , GETDATE())
 

GO

SELECT interval = DATEDIFF(month, DueDate, 'Nov 30 2001')

FROM Sales.SalesOrderHeade

Open in new window

0
 

Author Comment

by:FranklinRaj22
ID: 25390192
ok
0
 
LVL 26

Expert Comment

by:Chris Luttrell
ID: 25390235
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.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now