?
Solved

Compare time stamps in MSSQL

Posted on 2009-03-30
7
Medium Priority
?
1,490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 27

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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 3

Accepted Solution

by:
csenasa earned 2000 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 27

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Suggested Courses

777 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