Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using date variables in sql statements

Status
Not open for further replies.

gmgenova

Technical User
Mar 13, 2015
9
US
I am an SQL novice. I am going to be putting some of my access queries in macros so I need to automate some of the dates I have in my SQL statements so no user intervention is required. I have researched but have not found what I need yet.

Here is one of my SQL statements. I just need to have a date variable that will take the place of my hard coded date - "201807". In this case, it is YYYYMM. (MM is the previous month). Any help is appreciated. Thank you!!

SELECT DISTINCT [dbo_RX Reporting].[Plan Name], [dbo_RX Reporting].[Therapeutic Class], Sum([dbo_RX Reporting].Price) AS SumOfPrice, [dbo_RX Reporting].Period
FROM [dbo_RX Reporting]
GROUP BY [dbo_RX Reporting].[Plan Name], [dbo_RX Reporting].[Therapeutic Class], [dbo_RX Reporting].Period, [dbo_RX Reporting].[Plan Name]
HAVING ((([dbo_RX Reporting].[Therapeutic Class]) Like "*Analgesics*") AND (([dbo_RX Reporting].Period)="201807") AND (([dbo_RX Reporting].[Plan Name])="Alabama"));
 
Since my query is in Access, I ended up doing this to get the date variable I needed:

AND (([dbo_RX Reporting].Period)=Format(DateAdd("m", -1, Date()), "yyyymm"))
 
BTW “201807” is not a date in database or code terms. It is a year-month string. And your database table clearly represents it as a Reporting period. You used the Format() function to use a date calculation to return a string.

So technically, you’re looking for a reporting period string variable.

Skip,
[sub]
[glasses]Just traded in my OLD subtlety...
for a NUance![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top