Question : SQL Code for Date Concatenating

This is a date formatting question - probably using "convert".

Using SQL code directly in Query Analyser, I'd like to write some SQL to join the date & time of a record in one field, where the date and time for a record is currently stored in two separate fields.

The date is in this format: 2007-01-08 00:00:00.000
The time is in this format: 1899-12-30 09:38:00.000

So, the end result would be in a format that can be sorted easily in Excel.

Thanks for your help!

Answer : SQL Code for Date Concatenating

date : 2007-01-08 00:00:00.000
time : 1899-12-30 09:38:00.000

those correspond to date style 121 (see here: http://www.sql-server-helper.com/tips/date-formats.aspx)

What you want is to basically take the first 10 characters of the date field and concatenate the last 11 characters from the time field:
substring(date as char, 1, 10) + ' ' + substring(time as char, 12, 23)

date as char: convert(varchar(25), date_field, 121)
time as char: convert(varchar(25), time_field, 121)

So try something like
substring(convert(varchar(25), date_field, 121), 1, 10) + ' ' + substring(convert(varchar(25), time_field, 121), 12, 23)
Random Solutions  
 
programming4us programming4us