Posted By

indra on 08/24/10


Tagged

sqlserver


Versions (?)

Find First and Last Day of Current Month


 / Published in: SQL
 

  1. --SQL SERVER � Query to Find First and Last Day of Current Month
  2. --May 13, 2007 by pinaldave
  3.  
  4. --Following query will run respective to today�s date. It will return Last Day of Previous Month, First Day of Current Month, Today, Last Day of Previous Month and First Day of Next Month respective to current month.
  5.  
  6. DECLARE @mydate DATETIME
  7. SELECT @mydate = GETDATE()
  8.  
  9. SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)),@mydate),101) ,'Last Day of Previous Month'
  10. UNION
  11.  
  12. SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101) AS Date_Value, 'First Day of Current Month' AS Date_Type
  13. UNION
  14.  
  15. SELECT CONVERT(VARCHAR(25),@mydate,101) AS Date_Value, 'Today' AS Date_Type
  16. UNION
  17.  
  18. SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),101) ,'Last Day of Current Month'
  19. UNION
  20.  
  21. SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,1,@mydate)),101) , 'First Day of Next Month'
  22.  
  23. --GO
  24. --Reference : Pinal Dave (http://blog.SQLAuthority.com)

Report this snippet  

You need to login to post a comment.