Author |
Topic |
bvanyangu
Starting Member
20 Posts |
Posted - 2014-06-21 : 03:24:59
|
Msg 137, Level 15, State 2, Line 2Must declare the scalar variable "@ReportDate".I am Confused on what I need to do. I tried google, but don't exactly what i need to do or how do I declare that variable.Select Distinct 'Age' DemographicGroup, CASE WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) < 25 THEN '<=25', WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 25 and 35 THEN '26-35' WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 36 and 45 THEN '36-45' WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) > 45 THEN '46+' END as DemoGroup WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) < 25 THEN 1 WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 25 and 35 THEN WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 36 and 45 THEN 3 WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) > 45 THEN 4 END as DemoGroupOrderInto #Demographic From BorrowerDim_StagingUNIONSelect Distinct 'Marital Status',MaritalStatus,1From BorrowerDim_StagingUNIONSelect Distinct 'Race',Race,1From BorrowerDim_StagingUNIONSelect Distinct 'Sex',Sex,1From BorrowerDim_StagingSELECT *,DemographicGroup + ' - ' + DemoGroup DemoGrouplabel FROM #Demographic WHERE DemographicGroup IN @DemoCategory ORDER BY DemographicGroup, DemoGroupOrder Desc |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-06-21 : 04:57:43
|
you need to declare it on the top and assign it some valueDECLARE @DemoCategory varchar(5000)SET @DemoCategory = <put some comma separated value here>Select Distinct 'Age' DemographicGroup,CASE WHEN DATEDIFF(YY,[Date of Birth],@ReportDate) < 25 THEN '<=25',WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 25 and 35 THEN '26-35'WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 36 and 45 THEN '36-45' WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) > 45 THEN '46+' END as DemoGroupWHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) < 25 THEN 1WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 25 and 35 THEN WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) between 36 and 45 THEN 3WHEN DATEDIFF(YY,[DATE of Birth],@ReportDate) > 45 THEN 4 END as DemoGroupOrderInto #Demographic From BorrowerDim_StagingUNIONSelect Distinct 'Marital Status',MaritalStatus,1From BorrowerDim_StagingUNIONSelect Distinct 'Race',Race,1From BorrowerDim_StagingUNIONSelect Distinct 'Sex',Sex,1From BorrowerDim_StagingSELECT *,DemographicGroup + ' - ' + DemoGroup DemoGrouplabelFROM #DemographicWHERE ',' + @DemoCategory + ',' LIKE '%,' + DemographicGroup + ',%'ORDER BY DemographicGroup, DemoGroupOrder Desc A much better option would be to replace LIKE statement with thisWHERE DemographicGroup IN (SELECT Val FROM bo.ParseValues(@DemoCategory,',')) which would require string parsing UDF as belowhttp://visakhm.blogspot.in/2010/02/parsing-delimited-string.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|