March 06, 2011

SQL Server: Insert Date and Time in Separate Columns

The DateTime datatype in SQL Server is used to store date values along with time. If there is a need to store date and times values in separate columns, you can store Date values in the Datetime column and Time values in either the char datatype or the time datatype (Sql Server 2008)

SQL Server 2005

Consider the following data which uses datetime datatype, to store date and char datatype,
to store the time values.Suppose you want to find out the date and time values that fall
in the year 2009. Here’s the query for the same:

store datetime sql

Here’s the same query to try out:

declare @t table(date datetime, time char(8))
insert into @t
select '20101019','20:02:27' union all
select '20090122','12:19:20' union all
select '20110407','04:59:38'

select * from @t
where date+time>='20090101' and date+time<'20100101'

The condition date+time will make a datetime value and is checked in the range Jan 1st
of 2009 to Jan 1st 2010

date time

SQL Server 2008

SQL server 2008 supports the Time datatype, which can be used to store the time values. Here’s the same query, but with the time datatype

time col sql

declare @t table(date datetime, timecol time)
insert into @t
select '20101019','20:02:27' union all
select '20090122','12:19:20' union all
select '20110407','04:59:38'

select * from @t
where date+timecol>='20090101' and date+timecol<'20100101'

The query works same as the example code given above for version 2005

date time

Did you like this post?
kick it on
subscribe via rss subscribe via e-mail
print this post follow me on twitter

About The Author

Madhivanan,an MSc computer Science graduate from Chennai-India, works as a works as a Lead Subject Matter Expert at a company that simplifies BIG data. He started his career as a developer working with Visual Basic 6.0, SQL Server 2000 and Crystal Report 8. As years went by, he started working more on writing queries in SQL Server. He now has good level of knowledge in SQLServer, Oracle, MySQL and PostgreSQL as well. He is also one of the leading posters at and a moderator at His T-sql blog is at



1 Response to "SQL Server: Insert Date and Time in Separate Columns"
  1. SK said...
    October 22, 2014 at 6:26 AM

    Great ...Good work


Copyright © 2009-2016 All Rights Reserved for by Suprotim Agarwal | Terms and Conditions