[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1644
  • Last Modified:

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.
0
FranklinRaj22
Asked:
FranklinRaj22
1 Solution
 
Chris LuttrellSenior Database ArchitectCommented:
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
 
tl121000Commented:
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
 
csenasaCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
csenasaCommented:
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
 
FranklinRaj22Author Commented:
ok
0
 
Chris LuttrellSenior Database ArchitectCommented:
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now