Neal Waterstreet

  • Home
  • About
  • Book Reviews
    • 2018 Reading List
    • 2017 Reading List
    • 2016 Reading List
    • 2015 Reading List
    • 2014 Reading List
    • 2013 Reading List
  • Projects
    • OBi200
    • DS716+ memory upgrade
    • Cord cutting
  • Presentations
  • Contact

Archives for October 19, 2011

Problem with a MTD query

October 19, 2011 by neal Leave a Comment

I was running a query today and was not able to SUM by the current month. I checked the code and didn’t see any issues there, but when I looked at the variables it was pretty obvious.

DECLARE @targetdate VARCHAR(8)
DECLARE @previousdate VARCHAR(8)
DECLARE @currentmonth INT
DECLARE @currentday INT

SET @targetdate = CONVERT(VARCHAR(8), GETDATE() – 1, 112)
SET @previousdate = CONVERT(VARCHAR(8), GETDATE() – 2, 112)
SET @currentmonth = ( 10 * CAST(DATEPART(Year, GETDATE() – 1) AS INT) ) + CAST(DATEPART(Month, GETDATE() – 1) AS INT)
SET @currentday = CAST(DATEPART(Day, GETDATE() – 1) AS INT)

Which results in:

20111018
20111017
20120
18

I was setting @currentmonth incorrectly. The correct value is:

SET @currentmonth = ( 100 * CAST(DATEPART(Year, GETDATE() – 1) AS INT) ) + CAST(DATEPART(Month, GETDATE() – 1) AS INT)

Which gives the correct date/month

20111018
20111017
201110
18

Big difference!

Filed Under: Stuff

October 2011
M T W T F S S
 12
3456789
10111213141516
17181920212223
24252627282930
31  
« Mar   Nov »

Blogroll

  • Atlanta MDF
  • Atlanta Microsoft Business Intelligence Group
  • Atlanta Modern Excel User Group
  • Chris Webb's BI Blog
  • codegumbo
  • Lance England
  • PowerPivotPro
  • Prologika
  • SQLBI

Tags

#SQLSatATLBI Aereo at Atlanta Microsoft BI Azure Azure DataFest Backups Batch Certification changeset Chicago Conference CrashPlan Data Modeling Data Quality DAX Deployment DVR Entity-based staging Error Messages Excel Filter Goals Headphones Healthcare Leaf Tables MDM MDS MDS Add-in for Excel Oops! Something went wrong PASS Healthcare VC Power BI Power Pivot review Roku SQLSaturday SQL Saturday 800 SQLSaturday Atlanta BI SSIS Streaming Subscription View Time Training Validation Version Flag

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2025 · Minimum Pro Theme on Genesis Framework · WordPress · Log in