Adding a Time and a Date and then Converting to Timestamp

This is really a few separate issues, but I had to deal with them together, I'll just put them both on this post.

First, I needed to add a time to a date in SQL Server, and then convert the resulting DATETIME to a timestamp. The last line in the select statement below is really doing two things. It essentially converts a string (the date concatenated to the time) into a DATETIME, and then uses the DATEDIFF function to convert to a timestamp.

 

DECLARE @sometime TIME = '2PM'
DECLARE @somedate DATE = '2012-3-23'

SELECT
@sometime as SOMETIME,
@somedate as SOMEDATE,
DATEDIFF(second,{d '1970-01-01'}, CONVERT(DATETIME, CONVERT(VARCHAR(10),@somedate) + ' ' + CONVERT(VARCHAR(5),@sometime))) AS TIMESTAMP