Popular Posts

Saturday, June 30, 2012

SQL Server Date and Time data types

SQL Server 2008 has introduced the new Date and Time data types. Now assigning a date to time data type will return only the time part of the datetime and the Date will return the Date part of the datetime ...


DECLARE @datetime AS DATETIME = getdate();
DECLARE @date AS DATE = @datetime;
DECLARE @time AS TIME = @datetime;

SELECT @date, @datetime, @time

Result is:

2012-07-01 2012-07-01 10:40:06.403 10:40:06.4030000

Or alternatively:

SELECTCONVERT(TIME,GETDATE()) AS HourMinuteSecond,CONVERT(DATE,GETDATE()AS DateOnly
GO



Or to get only the HH:MM then as follow:


DECLARE @time AS TIME(0) = @datetime; -- With zero precision so returns hh:mm







'via Blog this'

No comments:

Post a Comment