sqlserver中 First day 和 Last day的实现 --LAST_DAY select cast(convert(varchar(7),dateadd(mm,1,'2012-02-04'),120) + '-01' as datetime) - 1 "LAST_DAY"; select DATEADD(mm, DATEDIFF(mm, 0, '2012-02-04')+1, 0)-1 "LAST_DAY"; --First Day select cast(convert(varchar(7),'...
-1, GETDATE() - 2)) ), DATEADD(m, -1, GETDATE() - 1))) , @LastDayOfLastMonth DATETIME = CONVERT(DATE, DATEADD(d, -( DAY(GETDATE()) ), GETDATE())) SELECT 'First day of last month' AS Title, CONVERT(VARCHAR, @FirstDayOfLastMonth , 120) AS [ODBC] ...
SELECT DATEADD (dd, -1, DATEADD(mm, DATEDIFF(mm, 0, GETDATE()) + 2, 0)) In SQL Server 2012 and lateryou can use EOMONTH Function to Get First and Last Day of a Month in SQL Server: Here is an example how you can get thelast day of the month you specify, using EOMONTH func...
The third variant in getting the last day of the month for the given input date simply simplifies the code by putting the step in getting the first day of the month into a separate function, called here as[dbo].[ufn_GetFirstDayOfMonth]. ...
In SQL Server, this is how I usually get to the last day of the month relative to an arbitrary point in time: select dateadd(day,-day(dateadd(month,1,current_timestamp)) , dateadd(month,1,current_timestamp) ) In a nutshell: From your reference point-in-time, Add 1 month, Then,...
This can be done in a single expression. I have used CROSS APPLY here to eliminate duplication ...
can useCONVERTwith style6to get a date from your text, then useEOMONTHto get the last day of...
If you want a more general and simple solution:
SQL Server 7.0 and SQL Server 2000 Date modified: Email: dtvam@idia.com Examples: List the 'First day of the week','Last day of the week','Last day of the month' and 'First day of the month' on valid date input ***DECLARE @Date datetime SET @Date = getdate() SELECT DATEADD(d...
can useCONVERTwith style6to get a date from your text, then useEOMONTHto get the last day of...