Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TSQL to combine a date field and a time field

Tags:

tsql

Using logparser to import IIS logs to a db results in one column that has the date value and a second field for time:

2010-05-25 00:00:00.000

and

2010-01-01 11:11:58.000

I'd like to code an after insert trigger that combines the 2 fields.

like image 208
justSteve Avatar asked May 25 '10 11:05

justSteve


People also ask

How do I combine a date and a time column in Excel?

Excel stores date and time values as numbers in the back end. So, if you have dates in one column and time in another column, the easiest way to combine these and get the date and time in one single cell would be to simply add these two cells.

How do I combine two columns in SQL?

SELECT *, CONCAT(FIRSTNAME, LASTNAME) AS FIRSTNAME FROM demo_table; Output: Here, we can see that FIRSTNAME and LASTNAME is concatenated but there is no space between them, If you want to add space between the FIRSTNAME and LASTNAME then add space(' ') in CONCAT() function. This method will change the original table.

Which datatype is used for date and time?

The DATETIME data type stores an instant in time expressed as a calendar date and time of day. You select how precisely a DATETIME value is stored; its precision can range from a year to a fraction of a second.


3 Answers

You can just add the two values after casting them to DATE and TIME datatypes, if you're using SQL Server 2008 or later. Here's an example.

declare @datet datetime;
set @datet = GETDATE();

select 
    @datet, 
    cast(@datet as date), 
    cast(@datet as time);

select 
    cast(cast(@datet as date) as datetime), 
    cast(cast(@datet as time) as datetime), 
    cast(cast(@datet as date) as datetime) + cast(cast(@datet as time) as datetime);
like image 119
Dave Markle Avatar answered Nov 23 '22 19:11

Dave Markle


In case somebody else stumbles on this thread (or in case the original poster still can use this answer), look into the TO_TIMESTAMP ( date , time ) function in LogParser which lets you combine a date-only timestamp with a time-only timestamp into a full timestamp value... and saves you from having to convert in the db...

like image 37
reiniero Avatar answered Nov 23 '22 19:11

reiniero


Try this:

DECLARE @Date varchar(23)
       ,@Time varchar(23)
       ,@Both datetime

SELECT @Date='2010-05-25 00:00:00.000'
      ,@Time='2010-01-01 11:11:58.000'

SELECT @Both=LEFT(@Date,10)+' '+RIGHT(@Time,12)

SELECT @Both

OUTPUT:

-----------------------
2010-05-25 11:11:58.000

(1 row(s) affected)

Set based:

DECLARE @INSERTED table(RowID int, DateOf varchar(23), TimeOf varchar(23), DateTimeOf datetime)

INSERT @INSERTED VALUES (1,'2010-05-25 00:00:00.000','2010-01-01 11:11:58.000',null)
INSERT @INSERTED VALUES (2,'2010-04-05 00:00:00.000','2010-01-01 12:34:56.789',null)
INSERT @INSERTED VALUES (3,'2010-03-15 00:00:00.000','2010-01-01 01:01:01.000',null)


UPDATE @INSERTED
    SET DateTimeOf=LEFT(DateOf,10)+' '+RIGHT(TimeOf,12)

SELECT * FROM @INSERTED

OUTPUT:

RowID   DateOf                  TimeOf                  DateTimeOf
------- ----------------------- ----------------------- -----------------------
1       2010-05-25 00:00:00.000 2010-01-01 11:11:58.000 2010-05-25 11:11:58.000
2       2010-04-05 00:00:00.000 2010-01-01 12:34:56.789 2010-04-05 12:34:56.790
3       2010-03-15 00:00:00.000 2010-01-01 01:01:01.000 2010-03-15 01:01:01.000

(3 row(s) affected)
like image 26
KM. Avatar answered Nov 23 '22 19:11

KM.