larrydavid
Programmer
Hello,
I'm about to pull out my last grey hair (of which I have none left but I'll find one to pull out). I have this stored procedure that is comparing dates, one that is a varchar in one table and one that is an int (lc.Current_Year) in another. I've been able to CAST varchars to ints in the past but for some reason (which I cannot figure out) my substring function is not liking this CAST. I'm getting way more records returned than I should so I know the WHERE join isn't working. If anyone can put me out of my misery and show me what I'm overlooking I would greatly appreciate it.
Thanks,
Larry
I'm about to pull out my last grey hair (of which I have none left but I'll find one to pull out). I have this stored procedure that is comparing dates, one that is a varchar in one table and one that is an int (lc.Current_Year) in another. I've been able to CAST varchars to ints in the past but for some reason (which I cannot figure out) my substring function is not liking this CAST. I'm getting way more records returned than I should so I know the WHERE join isn't working. If anyone can put me out of my misery and show me what I'm overlooking I would greatly appreciate it.
Thanks,
Larry
Code:
ALTER PROCEDURE [dbo].udsp_Test_Detail
@tin AS varchar(9),
@msa AS varchar(50),
@currentYear AS int,
@currentMonth AS int
AS
BEGIN
SET NOCOUNT ON;
SELECT DISTINCT
lc.database_msa AS [MSA - CC],
tc.COMPID AS TIN,
lc.COMPANY,
tc.CLAIMNO AS CLAIM,
tc.RECNUM,
lc.Current_Year,
lc.Current_Month,
lc.Current_MonthName,
lc.Average_Cost
FROM
TEST_METRICS_LINE_CHART lc INNER JOIN
TEST_CLAIMS tc ON lc.tin9 = tc.PROVID
WHERE
lc.tin = '123456789' AND
lc.database_msa = 'TEST_LOCATION_1' AND
lc.Current_Year = CAST(substring(tc.PDDATE, 1,4) AS INT) AND
lc.Current_Month = CAST(substring(tc.PDDATE, 6,1) AS INT)
END
Code:
Here is some output:
TEST_LOCATION_1 951683892 COMPANY1 600518120 12347249 2012 1 Jan 2012 24401.25 11807.43 0.483886 6 4066.875 1967.905 0.00 0.00 0 0.00 NULL 100.00 0.00 NULL
TEST_LOCATION_1 951683892 COMPANY1 600519190 12347250 2012 1 Jan 2012 24401.25 11807.43 0.483886 6 4066.875 1967.905 0.00 0.00 0 0.00 NULL 100.00 0.00 0
TEST_LOCATION_1 951683892 COMPANY1 600521751 12346853 2011 9 Sep 2011 18999.99 8946.62 0.470875 11 1727.2718 813.3291 26.666 0.00 0 0.00 NULL 100.00 0.00 NULL
TEST_LOCATION_1 951683892 COMPANY1 600534247 12346389 2011 5 May 2011 49736.53 9911.57 0.199281 14 3552.6093 707.9693 17.64 0.00 0 0.00 NULL 100.00 0.00 NULL
TEST_LOCATION_1 951683892 COMPANY1 600534247 12346390 2011 5 May 2011 49736.53 9911.57 0.199281 14 3552.6093 707.9693 17.64 0.00 0 0.00 NULL 100.00 0.00 NULL
TEST_LOCATION_1 951683892 COMPANY1 600587419 12346228 2011 3 Mar 2011 8409.16 3328.11 0.395772 5 1681.832 665.622 14.28 0.00 0 1679.90 NULL 100.00 0.00 NULL
TEST_LOCATION_1 951683892 COMPANY1 600609595 12346822 2011 9 Sep 2011 18999.99 8946.62 0.470875 11 1727.2718 813.3291 26.66 0.00 0 0.00 NULL 100.00 0.00 NULL