如何使用SQL Server创建当月的上个月的第一天 [英] How to create the first day of last month in current year using SQL server

查看:69
本文介绍了如何使用SQL Server创建当月的上个月的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



如何使用sql server创建当月的上个月的第一天。

例如,< br $>




输入= 2016-04-21

预期产量= 2016-12-01



请给我一个解决方案。



提前感谢。



我尝试了什么:



如何使用sql server创建当月的上个月的第一天

Hi all,

How to create the first day of last month in current year using sql server.
for instance,


Input = 2016-04-21
Expected Output = 2016-12-01

please drop me a solution for this.

thanks in advance.

What I have tried:

How to create the first day of last month in current year using sql server

推荐答案

这实际上非常简单。



使用YEAR(dateVar)获取年份。然后Month将始终为12,而day将始终为1.
This is actually pretty easy.

Use YEAR(dateVar) to get the Year. Then Month will always be 12 and day will always be 1.


declare @dt date
set @dt = '2016-04-21' 
SELECT CONVERT(datetime,  CONVERT (varchar(20), YEAR( @dt)   ) + '-12-01' )





当年,



for current year,

SELECT CONVERT(datetime,  CONVERT (varchar(20), YEAR( GETDATE())   ) + '-12-01' )


这篇关于如何使用SQL Server创建当月的上个月的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆