Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,


declare @date varchar(10)
declare @d datetime
declare @hour varchar(2)
declare @minute varchar(2)
declare @final varchar(20)
set @date='10/14/2014'
set @hour='16'
set @minute='03'
set @final=(@date+' '+@hour+':'+@minute+':00:000')
set @d =CONVERT(DATETIME, @final)
print @final
print @d


it showing error to convert

plz help

Thanks
srishti
Posted

Hi, you should really be specifying the date format you are using when converting to date type.

If you add the following line before your variable declaration section:

SQL
SET DATEFORMAT MDY


Then have a read over the msdn documentation for SET DATEFORMAT (Transact-SQL) statement.

Hope it helps.
 
Share this answer
 
Change your script to this
SQL
declare @date varchar(10)
 declare @d datetime
 declare @hour varchar(2)
 declare @minute varchar(2)
 declare @final varchar(20)
 set @date='10/14/2014'
 set @hour='16'
 set @minute='03'
 set @final=(@date+' '+@hour+':'+@minute+':00')
 set @d =CONVERT(DATETIME, @final)
 print @final
 print @d


My only question is why are you doing date manipulation with VARCHAR?
 
Share this answer
 
you can try something like this,

SQL
--Date
declare @date varchar(10)
declare @d DATE
set @date='10/14/2014'
set @d = CONVERT(DATE, @date, 101)	--101 the formate mm/dd/yyyy
--print @d

--Time
declare @time varchar(30)
declare @t TIME
declare @hour varchar(2)
declare @minute varchar(2)
set @hour='16'
set @minute='03'
set @time=(@hour+':'+@minute+':00:000') 
set @t = CONVERT(TIME, @time, 114)	-- 114 thr formate hh:mi:ss:mmm(24h)
--print @t


print @date +' ' +@time			--string in total
print CAST(@d AS DATETIME)+ @t	--datetime in total


to know about Datetime formates
http://msdn.microsoft.com/en-us/library/ms187928.aspx[^]
http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx[^]
 
Share this answer
 
hi srishti_

here is solution

declare @date varchar(10)
declare @d datetime
declare @hour varchar(2)
declare @minute varchar(2)
declare @final varchar(50)
set @date='10/14/2014'
set @hour='16'
set @minute='03'
set @final=(@date+' '+@hour+':'+@minute+':00:000')
set @d =CONVERT(DATETIME, @final)
print @final
print @d


i changed from declare @final varchar(20)to declare @final varchar(50)

pls check it is working fine now
 
Share this answer
 
Comments
Vinay Mistry 21-Aug-14 9:13am    
Please provide comments that after changing declare @final varchar(20)to declare @final varchar(50) your query has been solved or not?
srishti_ 21-Aug-14 9:14am    
its working fine with varchar(16) but it showinw output like
'Oct 14 2014 4:03PM' and i want output like '10/14/2014 16:03:00:000' how can it be
changed the @final variable size to 50
 
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