SQL 当前月份和年份的特定日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15142316/
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 13:57:58  来源:igfitidea点击:

Specific day of current month and year

sqlsql-servertsqldatetime

提问by Sk1X1

I have problem with return of specific day of current month and year. I need for example 15th day. Until now I used in FB/IB existing function:

我在返回当前月份和年份的特定日期时遇到问题。我需要例如第 15 天。到目前为止,我在 FB/IB 现有功能中使用:

IB_EncodeDate(EXTRACT(YEAR FROM Current_Date),EXTRACT(Month FROM Current_Date),15)

Does it exist a simply way to convert this for MSSQL database?

是否存在将其转换为 MSSQL 数据库的简单方法?

edit. I need output in OLE format (41,348 by example) to compare date with another date. I compare date from database with 15th day of current month.

编辑。我需要 OLE 格式的输出(例如 41,348)以将日期与另一个日期进行比较。我将数据库中的日期与当月的第 15 天进行比较。

回答by Aaron Bertrand

For the 15th day of current month:

对于当月的第 15 天:

SELECT DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0));

To get the silly OLE representation based on this "magic" date, 1899-12-30:

要根据这个“神奇”日期 1899-12-30 获得愚蠢的 OLE 表示:

SELECT DATEDIFF(DAY, -2, DATEADD(DAY, 14, 
  DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)));

Answer (on March 11th, when I updated this answer for the changed requirement):

答案(3 月 11 日,当我针对更改后的要求更新此答案时):

-----
41348

回答by Lamak

So, you have a date, and want to return the 15th day of the same month?. Well, assuming SQL Server 2008, you could do this:

那么,您有一个日期,并且想要返回同月的第 15 天?好吧,假设 SQL Server 2008,你可以这样做:

SELECT CONVERT(DATE,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)

For Previous versions of SQL Server:

对于以前版本的 SQL Server:

SELECT CONVERT(DATETIME,CONVERT(VARCHAR(6),GETDATE(),112)+'15',112)

回答by Joe Johnston

This seems like a quick answer.

这似乎是一个快速的答案。

declare @OLEDate int 
declare @currentDate as datetime
set @currentDate = DATEADD(DAY, 14, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))
set @OLEDate = convert(float, @currentdate) 
-- PRINT @OLEDate 

based on Aaron Bertrand's answer and your need for the integer conversion

基于 Aaron Bertrand 的回答以及您对整数转换的需求

回答by Saikh Rakif

To get 10th day of current day

获取当天的第 10 天

declare @cur_month int,@cur_yr int,@tenth_dt date
    set @cur_month=month(getdate())
    set @cur_yr=YEAR(getdate())
    set @tenth_dt=convert(date,'10/'+convert(varchar(5),@cur_month)+'/'+convert(varchar(5),@cur_yr),103)
    select @tenth_dt

回答by muhmud

Current_Datein SQL Server would be getdate().

Current_Date在 SQL Server 中将是getdate().

To get the 15th day in OLE Automation format, try:

要获得 OLE 自动化格式的第 15 天,请尝试:

select datediff(day, '18991230', dateadd(day, -day(getdate()) + 15, getdate()))

回答by Kaf

Not sure if you after Dayor Date. This gives both dayOfWeek and specificDate for any culture

不确定你是在Day还是Date. 这为任何文化提供了 dayOfWeek 和 specificDate

declare @myDay int = 15

select convert(date,myday) specificDate, datename(dw,myday) dayOfWeek
from (
   select convert(varchar(6),getdate(),112) + convert(varchar, @myDay) myday
) x

Fiddle Demo Here

小提琴演示在这里

| SPECIFICDATE | DAYOFWEEK |
----------------------------
|   2013-02-15 |    Friday |