|
|
| |

How do I return the first day of the month for the given date?
Answer:
To return the first day of the month for the given date, you can use
the FirstMonthDay user-defined function.
Syntax
FirstMonthDay (date)
Arguments
date
Is the datetime value.
Return Types
datetime
The function's text:
CREATE FUNCTION dbo.FirstMonthDay
( @Date datetime )
RETURNS datetime
AS
BEGIN
RETURN (CAST(STR(MONTH(@Date))+'/'+STR(01)+'/'+STR(YEAR(@Date)) AS DateTime))
END
GO
|
Example
Returns the first day for the '06/15/99' date:
SELECT dbo.FirstMonthDay('06/15/99')
GO
|
Here is the result set:
------------------------------------------------------
1999-06-01 00:00:00.000
(1 row(s) affected)
|
Note. The user-defined functions are only available with SQL Server 2000,
SQL Server 7.0 does not support user-defined functions.
See this article for more datetime user-defined functions:
Date and Time User-Defined Functions
|
|
|