开发者

last day of month question

I have the following query which is giving the last day of month by passing month name and year but dont know how to get first day in the similar fashion.

declare @month int开发者_JAVA百科, @year int
select @month=1,@year=2011

select Convert(varchar(20),dateadd(year,@year-1900,dateadd(month,@month,0)-1), 105)


I don't have experience with SQL Server, but based on your code, I would guess the answer would be something like:

declare @month int, @year int
select @month=1,@year=2011

select Convert(varchar(20),dateadd(year,@year-1900,dateadd(month,@month-1,0)), 105)


Keeping it in the datetime domain, find 1st day of following month and then subtract one day

SELECT
    dateadd(day, -1, dateadd(year, @year-1900, dateadd(month, @month, 0)))

Almost anything with CAST or varchar is non-deterministic and isn't language/@DATEFORMAT safe

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜