Technical Article

Date without Time (corrected)

,

Some time you need to know only Date without Time.
This is simple function that allows you truncate datetime field to date only.
This function can help you to construct dynamic T-SQL for select date period.

P.S. Thanks for ispaleny!

Conversion only for real not enough! It is necessary convert to real that correct errors near the last instant of the day.

CREATE FUNCTION DateOnly_sdt
  (@DateAndTime AS smalldatetime)
RETURNS smalldatetime
AS
/*****************************************
 * return date without time smalldatetime
 *****************************************/BEGIN
  RETURN CAST(ROUND(CAST(@DateAndTime AS float),0,1) AS smalldatetime)
END

CREATE FUNCTION DateOnly_dt
  (@DateAndTime AS datetime)
RETURNS datetime
AS
/*****************************************
 * return date without time for datetime
 *****************************************/BEGIN
  RETURN CAST(ROUND(CAST(@DateAndTime AS float),0,1) AS datetime)
END

Rate

4 (1)

Share

Share

Rate

4 (1)