Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 merge date and time

Author  Topic 

asif372
Posting Yak Master

100 Posts

Posted - 2013-01-27 : 03:53:49
I have Data like this
------Date-------------------Time
2012-12-21 00:00:00.000 -----23:05
2012-12-21 00:00:00.000 -----23:05
2012-12-21 00:00:00.000 -----23:07
2012-12-21 00:00:00.000 -----23:08
2012-12-22 00:00:00.000 -----01:07
2012-12-22 00:00:00.000 -----06:59

i want data like this
DateTime
2012-12-21 23:05:00.000
2012-12-21 23:05:00.000
2012-12-21 23:07:00.000
2012-12-21 23:08:00.000
2012-12-22 01:07:00.000
2012-12-22 06:59:00.000

how can it be possible
thanks in advance

shenulal
Starting Member

11 Posts

Posted - 2013-01-27 : 04:55:02
select cast(CONVERT(varchar(10),'2012-12-21 00:00:00.000',101) + ' ' + '23:05' as datetime)


Just replace the values with your field name

Hope this will help.

regards,
shenulal
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-27 : 13:54:08
DATEADD(minute,DATEDIFF(minute,0,[Time]),[Date])

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-27 : 13:54:45
see logic here

http://visakhm.blogspot.in/2012/07/generate-datetime-values-from-integers.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

shan007
Starting Member

17 Posts

Posted - 2013-01-27 : 14:13:16
Simplest solution, see below:

declare @date datetime
declare @time time

select @date='2012-12-21 00:00:00.000'
select @time='23:05'

select CAST(@date+@time as datetime)

output:
2012-12-21 23:05:00.000

==============================
I'm here to learn new things everyday..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-27 : 14:20:47
quote:
Originally posted by shan007

Simplest solution, see below:

declare @date datetime
declare @time time

select @date='2012-12-21 00:00:00.000'
select @time='23:05'

select CAST(@date+@time as datetime)

output:
2012-12-21 23:05:00.000

==============================
I'm here to learn new things everyday..


will work fine so long as original fields are of date/datetime and time datatypes. If varchar the format has to be consistent and unambiguos otherwise it will break

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

shan007
Starting Member

17 Posts

Posted - 2013-01-27 : 15:07:00
Yes, of course, I believe the field types used by asif is date/datetime time field. If they are varchar then he has to cast them date/time before concatenation..

==============================
I'm here to learn new things everyday..
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-27 : 23:53:31
quote:
Originally posted by shan007

Yes, of course, I believe the field types used by asif is date/datetime time field. If they are varchar then he has to cast them date/time before concatenation..

==============================
I'm here to learn new things everyday..


Nope still missing my point

The cast will not work fine so long as date values are inconsistent or have an ambiguos format
Read this

http://visakhm.blogspot.in/2011/12/why-iso-format-is-recommended-while.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -