Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi Friends,

Please solve this Query

SQL
create proc Sp_test1  
@date1 nvarchar(50),  
@date2 nvarchar(50)
as  
select distinct m.mcidno, CONVERT(VARCHAR(10),h.hdate,103)as hdate 
from Tbl_Machine m inner join Tbl_History h 
on m.mcidno=h.mcidno 
where h.hdate between @date1 and @date2 


Here in Tbl_History Table
hdate's datatype is datetime.

ex: 2013-06-12 00:00:00.000

when i execute
SQL
select CONVERT(varchar(10),hdate,103)as hdate from tbl_history it works fine.

it gives output like 12/06/2013


SQL
select distinct m.mcidno, CONVERT(VARCHAR(10),h.hdate,103)as hdate
from Tbl_Machine m inner join Tbl_History h
on m.mcidno=h.mcidno   It works fine


after giving where condition
Ex:
SQL
exec Sp_test1  '06/05/2013', '20/06/2013'

it shows error like Arithmetic overflow error converting expression to data type datetime


please solve this query..
Posted
Comments
kishore sharma 20-Jun-13 3:07am    
Hi try passing date value in this format
exec Sp_test1 '2013/06/05' , '2013/06/20' instead '06/05/2013', '20/06/2013'
VIP Venkatesan 20-Jun-13 4:10am    
ya. u are correct.. after changing the format it works fine..

thank you..

Use This convert(datetime, @date1, 103) if you are using SQL SERVER

Like in you case something like

reate proc Sp_test1
@date1 nvarchar(50),
@date2 nvarchar(50)
as
select distinct m.mcidno,convert(datetime,h.hdate, 103) as hdate
from Tbl_Machine m inner join Tbl_History h
on m.mcidno=h.mcidno
where h.hdate between convert(datetime,@date1, 103) and convert(datetime,@date2, 103)
 
Share this answer
 
Comments
VIP Venkatesan 20-Jun-13 4:09am    
thank you..
Are you sure the culture details are correct within SQL?

If the date format for the culture is wrong then it could be trying to find the 20th month of the year.

Try writing your params like this '20 Jun 2013' and see if you still get the problem.
 
Share this answer
 
Comments
VIP Venkatesan 20-Jun-13 4:11am    
ya.. thanks for your suggestion.
Hi Dear

Please change your 103 conversion Code to 120 , and increase varchar(10) to 20

as like this example :

select CONVERT(VARCHAR(19),GETDATE(),120)
 
Share this answer
 
change in sp
SQL
Alter proc Sp_test1
@date1 datetime,
@date2 datetime
as
select distinct m.mcidno, CONVERT(VARCHAR(10),h.hdate,103)as hdate
from Tbl_Machine m inner join Tbl_History h
on m.mcidno=h.mcidno
where h.hdate between @date1 and @date2

Execution
SQL
exec Sp_test1  '2013/05/06', '2013/06/20' --yyyy/MM-dd format

Happy Coding!
:)
 
Share this answer
 

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