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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Variable 1

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello in the statement below I have 2 variables that I set manually, I need to execute this statement up to present month and year.

How could I accomplish this without manually setting the year and month each time, can anyone explain what I need to do or help. Thank you for taking time.


DECLARE @ReleaseMonth varchar(10)
DECLARE @ReleaseYear varchar(10)
SET @ReleaseMonth ='January'
SET @ReleaseYear = '2000'

INSERT INTO MATRIX



SELECT @ReleaseMonth AS'Month_Released'
,@ReleaseYear AS 'Year_Released'
,COUNT(AR.Offender_Number)TotalOffendersReleased
,SUM(LengthOfStay) AS [TotalDays]
,SUM(CONVERT(DECIMAL(10,2),LengthOfStay))/Count(AR.Offender_Number)AS [Average Length of Stay]


FROM Admission_Release AR INNER JOIN
Offenders O ON AR.Offender_Number = O.Offender_Number
WHERE ReleaseMonth= @ReleaseMonth AND ReleaseYear=@ReleaseYear

AND AR.Release_Age >= 18
 
If you want to set the name of the month to be the name of current month and year to be the current year

DECLARE @ReleaseMonth varchar(10)
DECLARE @ReleaseYear char(4)
SET @ReleaseMonth = datename(month, getdate())
SET @ReleaseYear = cast(year(getdate()) as char(4))

PluralSight Learning Library
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top