Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 First day of last week in the month of May

Author  Topic 

kishoremcp
Starting Member

41 Posts

Posted - 2013-11-20 : 02:49:45
I want to have the first day of last week for the month of may using getdate()

example for this year is : 26th May

I want the format as 2013-05-26

also

i want to add 28 days for that in anther query

please help how to do that..

Thanks in advance...

Regards
Kishore

edit: moved to proper forum

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-20 : 03:58:44
[code]
SELECT DATEADD(wk,DATEDIFF(wk,0,DATEADD(mm,5,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)))-1,0) AS LastWeekOfMay
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

kishoremcp
Starting Member

41 Posts

Posted - 2013-11-20 : 04:23:45
Hi,
I am getting 20th may as the result where I need to have 26th may. please help and how to add 28 days to the query ?

Regards
Kishore
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-20 : 07:47:41
quote:
Originally posted by kishoremcp

Hi,
I am getting 20th may as the result where I need to have 26th may. please help and how to add 28 days to the query ?

Regards
Kishore



SELECT CASE WHEN MONTH(DATEADD(wk,DATEDIFF(wk,0,DATEADD(mm,5,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))),0)) = MONTH(DATEADD(mm,5,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))) THEN DATEADD(wk,DATEDIFF(wk,0,DATEADD(mm,5,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0)))-1,0) ELSE DATEADD(wk,DATEDIFF(wk,0,DATEADD(mm,5,DATEADD(yy,DATEDIFF(yy,0,GETDATE()),0))),0) END-1 AS LastWeekOfMay


to add 28 days just use DATEADD(dd,28,aboveexpression)

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -