SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 DateAdd()
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

junior6202
Starting Member

USA
20 Posts

Posted - 08/21/2014 :  13:51:39  Show Profile  Reply with Quote
So I am recreating some tables in Sql Server that originate in Access. I am having a bit of a problem converting the Dateadd Access Syntax to TSQL. Here is the query in Access and what i have so far in TSQL. I tried several Datediff functions but nothing seems to work. Thanks in advance for your help.

ACCESS:

SELECT Sales_ItemMonthly.ITEMNMBR, Sum(nz([QtySold],0)) AS SumOfQtySold, Sum(nz([Value],0)) AS SumOfValue
FROM Sales_ItemMonthly
WHERE (((Sales_ItemMonthly.Month) Between DateAdd("yyyy",-1,MthFirstday(Date())) And (mthfirstday(Date())-1)))
GROUP BY Sales_ItemMonthly.ITEMNMBR;



TSQL:

SELECT ITEMNMBR, ISNULL(SUM(QtySold), 0) AS SumOfQtySold, ISNULL(SUM(Value), 0) AS SumOfValue, Month
FROM cgi_master.dbo.Sales_ItemMonthly
GROUP BY ITEMNMBR, Month

tkizer
Almighty SQL Goddess

USA
37142 Posts

Posted - 08/21/2014 :  14:16:43  Show Profile  Visit tkizer's Homepage  Reply with Quote
between DATEADD(year, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) and DATEADD(day, -1, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))

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

junior6202
Starting Member

USA
20 Posts

Posted - 08/21/2014 :  14:31:18  Show Profile  Reply with Quote
Thank you very much Tara. It worked perfectly.
Go to Top of Page

tkizer
Almighty SQL Goddess

USA
37142 Posts

Posted - 08/21/2014 :  14:39:38  Show Profile  Visit tkizer's Homepage  Reply with Quote


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

SwePeso
Patron Saint of Lost Yaks

Sweden
30265 Posts

Posted - 08/21/2014 :  16:20:37  Show Profile  Visit SwePeso's Homepage  Reply with Quote
Slightly shorter
WHERE	Sales_ItemMonthly.Month >= DATEADD(MONTH, DATEDIFF(MONTH, '19010101', GETDATE()), '19000101')
	AND Sales_ItemMonthly.Month < DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')



Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000