Posted By

msstar on 10/09/12


Tagged

sql


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

johnw


SQL Date/Time Statements


 / Published in: SQL
 

SQL Server 2008 Date/Time Statements

  1. -- FIRST DAY OF MONTH
  2. SELECT DATEADD(MM,DATEDIFF(MM,0,GETDATE()),0)
  3.  
  4. -- LAST DAY OF MONTH
  5. SELECT DATEADD(MS,-3,DATEADD(MM,DATEDIFF(MM,0,GETDATE())+1,0))
  6.  
  7. -- GET UTC TIME DIFFERENCE
  8. DATEDIFF(DAY, GETUTCDATE(), GETDATE());
  9.  
  10. -- GET 12 HOURS TIME FORMAT (i.e 8:07PM)
  11. SELECT SUBSTRING(CONVERT(VARCHAR,GETDATE(),100),12,8)
  12.  
  13. -- GET 24 HOURS TIME FORMAT (i.e 23:37)
  14. SELECT CONVERT(VARCHAR(5),GETDATE(),108)
  15.  
  16. -- GET TIME DIFFERENCE BETWEEN 2 TIMES
  17. SELECT DATEDIFF(HH,TIME1,TIME2)
  18.  
  19. -- SEARCH IF MORE THAN ONE RECORD EXISTS
  20. SELECT CLIENT FROM PROP_X_CLIENT_JOB
  21. GROUP BY CLIENT
  22. HAVING COUNT(*) > 1
  23.  
  24. -- GET CURRENT DATE ONLY DATE PART
  25. SELECT DATEADD(DD,0,DATEDIFF(DD,0,GETDATE()))

Report this snippet  

You need to login to post a comment.