Get Only Date from DateTime
SELECT DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
There is also a few useful UDF(User Defined Function) in Syteline
ConvDate (@pInputDate DateType, @pFormat NVARCHAR(10))
Sample
SELECT ConvDate(GetDate(), ‘MM/DD/YYYY’)
Another useful one, DayEndOf
ALTER FUNCTION [dbo].[DayEndOf] (
@Date DATETIME) RETURNS DATETIME
AS
BEGIN
– This function takes the input date and extends the time portion forward
— to the last possible moment of that day, just before midnight of the next day.
— A null in yields a null out. It is useful for doing
— high-range comparisons where only the day matters and offers an alternative to the
— DATEDIFF built-in. The first 4 bytes of a datetime field are the date and
— the second four the time.
RETURN case when @Date = CONVERT(DATETIME, ‘9999-12-31 23:59:59.998’, 121) then @Dateelse dateadd(ms, -2, dbo.MidnightOf( dateadd(day, 1, @Date) ) )
end
END
Recent Comments