Ralph Varjabedian
Coding is a systematic art

Separating Time only or Date only from DateTime variable of MS Sql Server

Monday, August 03, 2009 12:43 PM

Hello all, I wanted to write some filters on a query I had and I needed to compare some datetime variables, but only the time part of the datetime and not the full datetime value. I realized there is no built in function in MS Sql Server to isolate or separate the time field out of a datetime value or the date field out of a datetime value. I googled some solutions on the internet and non of them gave me the quick simple solution I needed so I decided to approach the problem with a simple solution. I wrote two functions called GetTimeOnly and GetDateOnly and they serve a simple purpose, to isolate the time or date out of a datetime value.

Here is the sql code for them.

create function GetTimeOnly(@val datetime)
returns datetime
begin
    IF (@val is null)
	return null
    return cast(
        cast(datepart(hh,@val) as varchar(2)) + ':' + 
        cast(datepart(mi,@val) as varchar(2)) + ':' + 
        cast(datepart(ss,@val) as varchar(2)) + '.' + 
        cast(datepart(ms,@val) as varchar(4))
            as datetime)
end

 

create function GetDateOnly(@val datetime)
returns datetime
begin
    if (@val is null)
	return null
    return cast(
        cast(datepart(yyyy,@val) as varchar(4)) + '-' + 
        cast(datepart(mm,@val) as varchar(2)) + '-' + 
        cast(datepart(dd,@val) as varchar(2))  
            as datetime)
end
go

Its usage is simple and straight forward, instead of using the datetime variable directly in an sql query, just put it in the function that you need, for example:

select dbo.GetTimeOnly(getdate()) as TimeOnly, dbo.GetDateOnly(getdate()) as DateOnly
Now using functions like DateDiff or comparing with “>” and “<” becomes very easy.

Feedback

# re: Separating Time only or Date only from DateTime variable of MS Sql Server

SQL Server 2008 already have 2 new types : Date and Time. So you better invest in these! 8/5/2009 5:04 PM | Joseph Ghassan

# re: Separating Time only or Date only from DateTime variable of MS Sql Server

Hi Joseph. Having two seperate data types Date and Time still does not solve all your problems :) You may want a field where you want to actually save both Date and time (hence the DateTime field) however you would still have cases where you only want to compare date only or time only, like in filters or views, so you still need a couple of functions like the ones I wrote. 9/20/2009 2:27 AM | Ralph Varjabedian

# re: Separating Time only or Date only from DateTime variable of MS Sql Server

How to add comments to my table for further reference?As we aware, in oracle there is a provision to add comments to a table.COMMENT ON TABLE Employee IS ‘This is a table for Employee.Is there any way to add comments in SQL Sevrer?? 12/12/2009 10:24 AM | http://www.888casinno.com

# re: Separating Time only or Date only from DateTime variable of MS Sql Server

Although off topic :) but here you go:

google sp_addextendedproperty, you can do that on tables and on columns and on other objects as well. here is a quick link on the topic:
http://www.sqlmonster.com/Uwe/Forum.aspx/sql-server-programming/46899/sp-addextendedproperty 12/22/2009 1:16 PM | Ralph Varjabedian

# re: Separating Time only or Date only from DateTime variable of MS Sql Server

Having two seperate data types Date and Time still does not solve all your problems :) You may want a field where you want to actually save both Date and time (hence the DateTime field) however you would still have cases where you only want to compare date only or time only, like in filters or views, 12/22/2009 7:34 PM | Web Hosting

Post a comment





 

Please add 2 and 6 and type the answer here: