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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Merge Select

Author  Topic 

kappa02
Yak Posting Veteran

65 Posts

Posted - 2014-10-17 : 12:32:58
Is there a way which I can take the three statements below and merge them into one? ma


select @twomnthmaildate = STUFF(CONVERT(varchar(12),DATEADD(month,2,getdate()),107),1,3,DATENAME(MM,DATEADD(month,2,getdate())))
select @maildate = @twomnthmaildate -7
SELECT @maildate = DATENAME(MM, @maildate) + RIGHT(CONVERT(VARCHAR(12), @maildate, 107), 9)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-10-17 : 12:41:08
I just did a copy/paste. I'm not sure that it makes code sense to put them into one when it makes it harder to read. Make sure you add a good comment to your code.

SELECT @maildate = DATENAME(MM, STUFF(CONVERT(varchar(12),DATEADD(month,2,getdate()),107),1,3,DATENAME(MM,DATEADD(month,2,getdate()))) -7) + RIGHT(CONVERT(VARCHAR(12), STUFF(CONVERT(varchar(12),DATEADD(month,2,getdate()),107),1,3,DATENAME(MM,DATEADD(month,2,getdate()))) -7, 107), 9)

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

ScottPletcher
Aged Yak Warrior

550 Posts

Posted - 2014-10-17 : 12:42:02
[code]
SELECT DATENAME(month, maildate) + RIGHT(CONVERT(VARCHAR(12), maildate, 107), 9)
FROM (SELECT DATEADD(DAY, -7, DATEADD(month, 2, getdate())) AS maildate) AS x
[/code]
Go to Top of Page
   

- Advertisement -