Suppose you have two columns of character datatype that stores date and time separately and you want to combine both of these columns and convert it to a valid datetime. You can use the following methods:
Consider these variables
declare @date char(8), @time char(8)
select @date='20101001',@time ='12:10:47'
select cast(@date+' '+@time as datetime)
In the above method both of them are combined to form a datetime and converted to datetime datatype
![Concat Char Date and Time Concat Char Date and Time](http://lh6.ggpht.com/-88lAuR67HRQ/TmbUZiKh19I/AAAAAAAAAPU/2U2dJ3mhsMU/image%25255B4%25255D.png?imgmax=800)
Method 2 : Convert date value into datetime and append time part to it
declare @date char(8), @time char(8)
select @date='20101001',@time ='12:10:47'
select cast(@date as datetime)+@time
In the above method, date value is converted to datetime datatype and time value is
added to it.
![Concat Char Date and Time Concat Char Date and Time](http://lh3.ggpht.com/-odE7IVl8BfU/TmbUadf33aI/AAAAAAAAAPY/DpAYw6LuVdA/image%25255B8%25255D.png?imgmax=800)
3 comments:
We've only just got date and time separately - now you want to put them back together again?
We've only just got date and time separately - now you want to put them back together again?
Post a Comment