Sql

Mallorie W.

asked • 04/04/24

How do you create a dynamic date out of a hardcoded date? Can someone please help me identify what is missing?

Hardcoded date is: 12-31-2023. I am trying to make this date dynamic so that it runs for the current year starting with 12-31-2023.


These are the notes I have on this:

-- Look at the previous month, and get the last day of year before that

-- Get the previous month, take the year and month. Find the last day of year from year before that



-- So let's say it's January 2024, the previous month is December 2023

-- The last day of year is December 31, 2023. We want one year before that

-- So our result should be December 31, 2022.



-- Let's say it's February 2024. the previous month is January 2024

-- The last day of the year is December 31, 2024. We want one year before that

-- So our result should be December 31, 2023.


This is what I've gotten so far but something is missing because it is not pulling the correct number of lines.

EventFromDt > DATEADD(DD, -1, DATEADD(YY,DATEDIFF(YY,0,DATEADD(YEAR,0,DATEADD(MM, -1,DATEADD(MM, -1,EOMONTH(GETDATE()))))),0))


Entire Query:

/****** Script for SelectTopNRows command from SSMS ******/
SELECT distinct [EventId]
,[EventFromDt] as 'Outreach From Date'
,[EventCloseDt] as 'Outreach Close Date'
,[EventSummaryTxt] as 'Outreach Program'
,[IssuerMemberId]
,[BirthDt] as 'Birth Date'
,[FirstNameTxt] as 'First Name'
,[LastNameTxt] as 'Last Name'
,[GenderTxt] as 'Gender'
,[AffiliationGroupCd] 'Master Group Number'
,[AffiliationGroupTxt] 'Master Group Name'
,cc.[GroupId]
,cc.[GroupLevel1Id]
,gp.GroupLevel1NameTxt as 'Group Name'
,[EventReviewTypeTxt] as 'Case Management Condition'
,[DiagnosisCd1]
,[DiagnosisTxt1]
,[DiagnosisCd2]
,[DiagnosisTxt2]
,[DiagnosisCd3]
,[DiagnosisTxt3]

FROM [edw].[vwMedicalManagementCaseMgmtandOutreachSummary] cc

left join edw.DimGroup gp
on gp.groupid = cc.groupid
and gp.GroupLevel1Id = cc.GroupLevel1Id
and gp.GroupLevel2Id = cc.GroupLevel2Id
and gp.GroupLevel3Id = cc.GroupLevel3Id

where AffiliationGroupCd = '10838'
and EventFromDt > '2023-12-31' --this will need updated to '2023-12-31' after 2/1/24
or EventCloseDt = NULL
and validind = 'Y'

1 Expert Answer

By:

Jeremy D. answered • 04/11/24

Tutor
New to Wyzant

Fortune 30 Principal Data Engineer

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.