SQL Date/Time Statements


/ Published in: SQL
Save to your folder(s)

SQL Server 2008 Date/Time Statements


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.