SQL Server merge 2 DateTime fields

At work we need to merge a datetime field with no time element and a datetime field with no time element – there are two solutions on this stack overflow post depending on what is there in the original data. Thes easiest is the first solution. You can add the fields together as DateTime are stored as floating point values. I used the second as I could not guarantee the conditions required for the first:

SELECT DATEADD(day, 0, DATEDIFF(day, 0, your_date_column)) + DATEADD(day, 0 - DATEDIFF(day, 0, your_time_column), your_time_column) FROM your_table

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s