question

user-1178 (google) avatar image
user-1178 (google) asked

Is there a way to add a date and time together to get the datetime value in SQL?

I have a date field, and a separate time field, that I want to concatenate or add together in SQL. For example, if my date field has 01-01-2010 and my time field has 12:00:00, I want to combine them or convert them into a datetime value of 01-01-01 12:00:00. Is this possible?

sql-server-2008datetimeconverttime
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Squirrel 1 avatar image
Squirrel 1 answered

Yes. Try

declare @date datetime,            
    @timestr    varchar(10)            
            
select  @date = '2010-04-01',            
    @timestr    = '12:34:56'            
            
select  @date + ' ' + ' ' + @timestr            
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.