Click here to Skip to main content
15,899,313 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi,
i am getting an error while writing a stored procedure that takes date as input value
and returns time in hours.i have used to extract for hours substring on a datetime column.
"error is cannot convert date/time from string"

the sample query is:

SQL
ALTER PROCEDURE [dbo].[Sp_GetTotalOrders]  
@DateFrom  NVARCHAR(50)
AS
Declare @curDate nvarchar(50);
BEGIN
set @curDate = @DateFrom + %;
    select count(a.Id) TotalCount, SUBSTRING(ShipDate,11, 3) ShipTime from
ShipmentDetail a inner join Items b on a.Id = b.ShipmentID 
where ShipDate like @curDate
Group by SUBSTRING(ShipDate,11, 3) order by ShipTime
Posted
Updated 6-Aug-12 2:08am
v2
Comments
Santhosh Kumar Jayaraman 6-Aug-12 8:04am    
what is the column type for shipdate? varchar or datetime?

when comparing two fields datatype should be same
if 'Shipdate' having 'Date/Time' field then @DateFrom & @curDate should have same datatype

Happy Coding!
:)
 
Share this answer
 
Comments
Madhugundi 8-Aug-12 1:11am    
Thanks
Aarti Meswania 8-Aug-12 1:12am    
welcome :)
I think your shipdate column is datetime datatype and not varchar..
So you have to convert.

SQL
ALTER PROCEDURE [dbo].[Sp_GetTotalOrders]  
@DateFrom  NVARCHAR(50)
AS
Declare @curDate nvarchar(50);
BEGIN
set @curDate = @DateFrom + %;
    select count(a.Id) TotalCount, Convert(Time,Shipdate) ShipTime from
ShipmentDetail a inner join Items b on a.Id = b.ShipmentID 
where CONVERT(DATE,shipdate,101) like @curDate
Group by Convert(Time,Shipdate) order by ShipTime



This will still give you error because of group by. You have to add columns in select statement to group by. Wht i tried above is getting time from datetime column where date is something.
 
Share this answer
 
Comments
Madhugundi 8-Aug-12 1:13am    
Thanks
Santhosh Kumar Jayaraman 8-Aug-12 1:22am    
welcome.is that working?
Hi AshishChaudha

You can't use datetime value for substring function. Substring only accept string value, so you need to covert datetime to string. for example

select substring(convert(nvarchar(20),ShipDate,20),12,8)

it will return 17:59:33.

If you have any query mail me.

Thanks and Regards
Arun vasu
CARE - IT
OTTAPALAM, KERALA.
arunsneha@outlook.com
 
Share this answer
 
Comments
Madhugundi 8-Aug-12 1:13am    
Thanks

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