Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Argument data type datetime is invalid for argument 1 of substring function.

$
0
0

Hi,

I have this query - works fine -

;WITH cte AS 
(
select * from (SELECT 
distinct pehPErcontrol
,case left(substring(pehPErcontrol,5,len(pehPErcontrol)),2)
 when '01' then 'January' 
 when '02' then 'February' 
 when '03' then 'March' 
 when '04' then 'April'
  when '05' then 'May' 
  when '06' then 'June' 
  when '07' then 'July' 
  when '08' then 'August' 
  when '09' then 'September' 
  when '10' then 'October' 
  when '11' then 'November' 
  when '12' then 'December' 
 end as [UltiMonth],
rtrim(eepNameLast) + 
',  ' + rtrim(eepNameFirst) + 
' ' + coalesce(substring(eepNameMiddle,1,1) + '.', '') as Name, 
eepNameLast AS [Last Name],
IsNull(eepNameSuffix,'') AS [Suffix],
eepNameFirst AS [First Name],
IsNull(eepNameMiddle,'') AS [Middle Name],
pehCurAmt AS [Current Amount], 
pehCurHrs AS [Current Hours], 
pehCoID AS [Company ID], 
pehEEID AS [EE ID], 
pehEmpNo AS [Emp No], 
pehLocation AS [Location], 
pehJobCode AS [Job Code], 
pehOrgLvl1 AS [Org Level 1],    
pehOrgLvl2 AS [Org Level 2], 
pehOrgLvl3 AS [Org Level 3], 
pehOrgLvl4 AS [Org Level 4], 
pehPayGroup AS [Pay Group], 
pehProject AS [Project], 
pehShfShiftAmt AS [Shift Amount],
pehearncode AS [Earn Code],
pehIsVoided AS [IS Voided],
pehIsVoidingRecord AS [Voiding Record],
pehIsOvertime AS [Is Overtime]
FROM EmpPers JOIN pearhist ph ON ph.pehEEID = eepEEID 
join  WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] 
	ON [orglevel] = pehOrgLvl2) t 
right outer join  
	WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] ob 
		on t.[UltiMonth] = ob.[month] and orglevel = [org level 2]
where pehPerControl > '201301011' 
AND [EARN CODE] = '0002'
AND [IS Voided] <> 'Y'
AND [Voiding Record] <> 'Y' 
AND [Is Overtime] = 'Y' 
AND  [org level 2] like '%ZSW'
--AND [ULTIMONTH] = 'FEBRUARY'
--ORDER BY pehPerControl
)
SELECT *
	,SUM([Current Amount]) 
		OVER (PARTITION BY [Emp No],[UltiMonth]) AS [Monthly Amount]
FROM cte

Turns out I used the wrong field (pehPerControl) which was formatted like - 201301111

I need to now use 'pehPaydate' (becasue there can be more than one) which is formatted like - 2013-01-13 00:00:00.000

So, If I change the columns in my query to use 'pehPaydate' now I get an error:

Argument data type datetime is invalid for argument 1 of substring function.

;WITH cte AS 
(
select * from (SELECT 
distinct pehpaydate
,case left(substring(pehpaydate,6,len(pehpaydate)),2)
 when '01' then 'January' 
 when '02' then 'February' 
 when '03' then 'March' 
 when '04' then 'April'
  when '05' then 'May' 
  when '06' then 'June' 
  when '07' then 'July' 
  when '08' then 'August' 
  when '09' then 'September' 
  when '10' then 'October' 
  when '11' then 'November' 
  when '12' then 'December' 
 end as [UltiMonth],
rtrim(eepNameLast) + 
',  ' + rtrim(eepNameFirst) + 
' ' + coalesce(substring(eepNameMiddle,1,1) + '.', '') as Name, 
eepNameLast AS [Last Name],
IsNull(eepNameSuffix,'') AS [Suffix],
eepNameFirst AS [First Name],
IsNull(eepNameMiddle,'') AS [Middle Name],
pehCurAmt AS [Current Amount], 
pehCurHrs AS [Current Hours], 
pehCoID AS [Company ID], 
pehEEID AS [EE ID], 
pehEmpNo AS [Emp No], 
pehLocation AS [Location], 
pehJobCode AS [Job Code], 
pehOrgLvl1 AS [Org Level 1],    
pehOrgLvl2 AS [Org Level 2], 
pehOrgLvl3 AS [Org Level 3], 
pehOrgLvl4 AS [Org Level 4], 
pehPayGroup AS [Pay Group], 
pehProject AS [Project], 
pehShfShiftAmt AS [Shift Amount],
pehearncode AS [Earn Code],
pehIsVoided AS [IS Voided],
pehIsVoidingRecord AS [Voiding Record],
pehIsOvertime AS [Is Overtime]
FROM EmpPers JOIN pearhist ph ON ph.pehEEID = eepEEID 
join  WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] 
	ON [orglevel] = pehOrgLvl2) t 
right outer join  
	WSISQL4.DASHBOARD.DBO.[OVERTIME_BUDGET_2013] ob 
		on t.[UltiMonth] = ob.[month] and orglevel = [org level 2]
where pehpaydate > '01/01/2013' 
AND [EARN CODE] = '0002'
AND [IS Voided] <> 'Y'
AND [Voiding Record] <> 'Y' 
AND [Is Overtime] = 'Y' 
AND  [org level 2] like '%ZSW'
--AND [ULTIMONTH] = 'FEBRUARY'
--ORDER BY pehpaydate
)
SELECT *
	,SUM([Current Amount]) 
		OVER (PARTITION BY [Emp No],[UltiMonth]) AS [Monthly Amount]
FROM cte

I have changed the substring length but that didn't help. Thought it would an easy change. Think I need to do something like :

case left(substring(cast(pehpaydate as varchar),(pehpaydate,5,len(pehpaydate)),2)


qeqw





Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>