首页  |  新闻  |  天气  |  联系我们  |  管理登陆 
逸飞和逸翔 家庭百事 科技纵横 家庭影集 网络文摘 音乐&艺术 友情链结
Business
中国瓷器
Computer/Internet
ASP/VB
SQL server
FLASH
Home Network
IIS SERVER
photoshop
search engine
Perl
General Problem Fix
Powerpoint
Router/Switch/Hub
Excel
FTP
.NET
Internet Security
Cloud Computing
GPG
PHP
语义搜索(semantic search)
股票
Glossaries
IPHONE
Books
 
Send to printer
By pinaldave

Following query will find the last day of the month. Query also take care of Leap Year.

Script:

DECLARE @date DATETIME
SET @date='2008-02-03'
SELECT DATEADD(dd, -DAY(DATEADD(m,1,@date)), DATEADD(m,1,@date))
            AS LastDayOfMonth
GO
DECLARE @date DATETIME
    SET @date='2007-02-03'
SELECT DATEADD(dd, -DAY(DATEADD(m,1,@date)), DATEADD(m,1,@date))
            AS LastDayOfMonth
GO
 

Following query will run respective to today’s date. It will return Last Day of Previous Month, First Day of Current Month, Today, Last Day of Previous Month and First Day of Next Month respective to current month.

DECLARE @mydate DATETIME
SELECT @mydate = GETDATE
()
SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)),@mydate),101
) ,
'Last Day of Previous Month'
UNION
SELECT
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(@mydate)-1),@mydate),101) AS Date_Value
,
'First Day of Current Month' AS
Date_Type
UNION
SELECT
CONVERT(VARCHAR(25),@mydate,101) AS Date_Value, 'Today' AS
Date_Type
UNION
SELECT
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))),DATEADD(mm,1,@mydate)),101
) ,
'Last Day of Current Month'
UNION
SELECT
CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,@mydate))-1),DATEADD(mm,1,@mydate)),101
) ,
'First Day of Next Month'
GO

back to top