Click here to Skip to main content
15,902,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi friends,
i am getting an Error in Sql server for my following Query:
SQL
DECLARE @intCOunt as INT
select  @intCOunt = COUNT(*)
from dbo.tbl_PO
where CAST (SUBSTRING(PONumber,6,2)+ '/01/'+ SUBSTRING(PONumber,9,4)  as DateTime)
BETWEEN
CAST ('04/01/'+  CAST(YEAR(GETDATE()) as varchar)  as DateTime)
AND
CAST ('03/31/'+ CAST((YEAR(GETDATE()) + 1) as varchar)  as DateTime)


Error:Conversion failed when converting date and/or time from character string.


Pl help

regards,
Aamir
Posted
Updated 22-Jul-12 20:04pm
v2
Comments
Santhosh Kumar Jayaraman 23-Jul-12 1:58am    
can you show us ur sample value in PONumber column?
Christian Graus 23-Jul-12 2:00am    
He will probably show you a valid sample, there's probably only one row that is not valid.
aamir07 23-Jul-12 2:07am    
PONumber value is like
Example:'DSCL-07-12/Kit01'

07-12 is Month-Year
DSCL is a Fixed string so does Kit
only the 01 value gets change

Your error means what it says. The subtext is 'what sort of person stores strings when they mean to store dates ?' It's telling you that the substring you're grabbing, does not work out to be a valid date for every value it's pulling from the DB.
 
Share this answer
 
Check the PONumber value and try to figure out what the problem could be - if you are using a front end, try debugging through your code to check the value of PoNumber.
 
Share this answer
 
check 'PONumber' field

SUBSTRING(PONumber,6,2) is expressing months not date
this should be return value between 1 to 12 only.

so,check results of SUBSTRING(PONumber,6,2) like given below

SQL
select
cast(SUBSTRING(PONumber,6,2) as int)
from dbo.tbl_PO
where cast(SUBSTRING(PONumber,6,2) as  int)>12 or cast(SUBSTRING(PONumber,6,2) as  int)=0


Happy coding!
:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900