Apr11

Date and Time in a SQL Statement (DateTime)  

Categories: SQL Server

To get the date and time for a datetime value in SQL Server, use the GetDate method.  It will return the current system date and time in the SQL Server standard internal format for datetime values.

This is a pretty useful function if you need to know the date/time in a select statement or a where clause.

SELECT GETDATE()

GO

Here is the result set:
————————-
April 10 2006   11:39    AM

(1 row(s) affected)

 
Share It:          
 
 

Links to this post

Comments

arti.manathkar  commented on  Friday, December 19, 2008  5:13 PM 

If i need only the date part of the datetime function , then which function is useful????

Dan  commented on  Friday, December 19, 2008  5:16 PM 

SQL doesn't provide any functions for what you are looking for. However, you can create a user defined function (udf) that you can call. In your case, the syntax would be:

create function DateOnly(@DateTime DateTime)

-- Returns @DateTime at midnight; i.e., it removes the time portion of a DateTime value.

returns datetime

as

begin

return dateadd(dd,0, datediff(dd,0,@DateTime))

end

go

Jeff Smith has an extensive post containing UDF's dealing with date time values that you may want to look at.

http://weblogs.sqlteam.com/jeffs/archive/2007/01/02/56079.aspx

Leave a comment





CAPTCHA Image Validation