[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Compare time stamps in MSSQL

Posted on 2009-03-30
7
Medium Priority
?
1,590 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how the fundamental information of how to create a table.

649 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