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:
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
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
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
1 comment:
Great ...Good work
Post a Comment