SQL 如何获取代表一个月第一天的日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:14:22  来源:igfitidea点击:

How to get date representing the first day of a month?

sqlsql-serverdatetimedate

提问by DaveDev

I need functionality in a script that will enable me to insert dates into a table.

我需要脚本中的功能,使我能够将日期插入表中。

What SQL do I need to insert a date of the format

我需要什么 SQL 来插入格式的日期

01/08/2010 00:00:00

where the date is the first day of the current month. What do I need to change order that I can specify the month value? Thanks

其中日期是当月的第一天。我需要更改什么才能指定月份值?谢谢

回答by Icemanind

The best and easiest way to do this is to use:

最好和最简单的方法是使用:

SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)

Just replace GETDATE() with whatever date you need.

只需将 GETDATE() 替换为您需要的任何日期。

回答by jmoreno

The accepted answer works, and may be faster, but SQL 2012 and above have a more easily understood method:

接受的答案有效,并且可能更快,但 SQL 2012 及更高版本有一个更容易理解的方法:

SELECT cast(format(GETDATE(), 'yyyy-MM-01') as Date)

回答by Joe Stefanelli

select cast(cast(datepart(year,getdate()) as char(4)) 
+ '/' 
+ cast(datepart(month,getdate()) as char(2))
+ '/01' as datetime)

回答by iliketocode

Here is a very simple way to do this (using SQL 2012 or later)

这是一个非常简单的方法(使用 SQL 2012 或更高版本)

datefromparts(year(getdate()),month(getdate()),1)

you can also easily get the last day of the month using

您还可以使用

eomonth(getdate())

回答by Beth

SELECT DATEADD(day,1-DATEpart(day, GETDATE()),GETDATE())

SELECT DATEADD(day,1-DATEpart(day, GETDATE()),GETDATE())

回答by BlackICE

i think normally converts string to MM/DD/YY HH:mm:ss, you would need to use 08/01/2010 00:00:00

我认为通常将字符串转换为 MM/DD/YY HH:mm:ss,您需要使用 08/01/2010 00:00:00

Sorry, misunderstood the question, looking to see if you can change the order for strings.

抱歉,误解了这个问题,想看看您是否可以更改字符串的顺序。

This may be what you want:

这可能是你想要的:

declare @test as date

select @test = CONVERT(date, '01/08/2010 00:00:00', 103)
select convert(varchar(15), @test, 106)

回答by AllenG

Modified from this link. This will return as string, but you can modify as needed to return your datetime data type.

从此链接修改。这将作为字符串返回,但您可以根据需要修改以返回您的日期时间数据类型。

SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(GetDate())-1),GetDate()),101) 

回答by Quassnoi

SELECT  CAST(FLOOR(CAST(DATEADD(d, 1 - DAY(GETDATE()), GETDATE()) AS FLOAT)) AS DATETIME)

回答by Todd181

SELECT DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0)

This worked perfectly. I actually added a Case statement. Thanks for the post:

这工作得很好。我实际上添加了一个 Case 语句。感谢您的帖子:

SELECT Case(DATEADD(m, DATEDIFF(m, 0, GETDATE()), 0) as Date)

回答by Nicholas Thomson

Get First Day of Last Month

获取上个月的第一天

Select ADDDATE(LAST_DAY(ADDDATE(now(), INTERVAL -2 MONTH)), INTERVAL 1 DAY);

Get Last Day of Last Month

获取上个月的最后一天

Select LAST_DAY(ADDDATE(now(), INTERVAL -1 MONTH));