如何从日期名称中获取日期?

时间:2018-12-13 08:21:24

标签: sql-server

如何获取特定日期的日期?就像我有星期四或月份数字一样?

例如,如果我给出12,我想获得本月第12天的日期。或者,如果我输入“ Sun”或“ Sat”,是否可以获取这些日期的日期?

4 个答案:

答案 0 :(得分:3)

DATEFROMPARTS函数可以从日,月和年构造日期。

DATEPARTS执行相反的操作-为您提供日期的日,月,年,小时等。或者,您可以使用YEARMONTHDAY之类的功能。

您可以解构GETDATE函数返回的值,并构造所需的任何日期。例如,下面是如何获取当月第12天的日期:

select DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 12)

将“ Sun”或“ Sat”转换为日期要困难一些。首先,它们不是确定性的。如果今天是星期五,则在世界某些地区,“本周的星期日”是指“下一个星期日”,而在其他地区则是“最后一个星期日”。您应该基于DATEPART(dw, GETDATE())返回的值(这将为您提供星期几)实现自己的逻辑。

答案 1 :(得分:1)

查找当前月份的工作日

DECLARE @daynumber INT = 12

SELECT datename(weekday, dateadd(d, @daynumber - 1, getdate()))

查找给定工作日当前月份的日期

DECLARE @dayname char(3) = 'sat'

;WITH CTE as
(
  SELECt TOP 
    (datediff(D, eomonth(getdate(), -1),eomonth(getdate())))
    dateadd(d,row_number()over(ORDER BY 1/0),
      eomonth(getdate(),-1))date
  FROM
    (values(1),(2),(3),(4),(5),(6))x(x),
    (values(1),(2),(3),(4),(5),(6))y(x)
)
SELECT day(date) monthday, date
FROM CTE
WHERE left(datename(weekday, date),3) = @dayname

答案 2 :(得分:0)

select sysdatetime(); --2018-12-13 16:29:56.0560574

---If I give 12 for instance I want to get the date of 12th day of this month.
declare @numDate int = 12;
select dateadd(m, datediff(m,0,getdate()),@numDate - 1 ); --2018-12-12 00:00:00.000

--Or if I give 'Sun' or 'Sat' is it possible to get the dates of these days ?
declare @text nvarchar(20) = 'Sunday';
declare @dateStart date =  dateadd(month, datediff(month, 0, sysdatetime()), 0),
    @days int =( select (DAY(dateadd(dd,-1,DATEADD(m,1,cast(2018 as varchar(4)) + '-' + cast(12 as varchar(2)) +'-01')))));
declare @dateEnd date = DATEADD(day,@days-1,@dateStart);
;WITH CTE (Dates,EndDate) AS
(
    SELECT @dateStart AS Dates,@dateEnd AS EndDate
    UNION ALL
    SELECT DATEADD(day,1,Dates),EndDate
    FROM CTE 
    WHERE DATEADD(day,1,Dates) <= EndDate
)
SELECT CTE.Dates, DATENAME(DW, CTE.Dates)
FROM CTE
where DATENAME(DW, CTE.Dates)  = @text;

结果:

Dates,Day
2018/12/2,Sunday
2018/12/9,Sunday
2018/12/16,Sunday
2018/12/23,Sunday
2018/12/30,Sunday

答案 3 :(得分:0)

-- Here is how to get week day name to week day number
DECLARE @T TABLE (Dow INT, NameOfDay VARCHAR(15), ShortName CHAR(3));

WITH Days AS
(
    SELECT TOP 7
        ROW_NUMBER() OVER(PARTITION BY object_id ORDER BY object_id) AS RowNo
    FROM
        sys.all_columns
)
INSERT INTO @T
SELECT
    RowNo,
    DATENAME(WEEKDAY, RowNo - 1),
    LEFT(DATENAME(WEEKDAY, RowNo - 1), 3)
FROM
    Days

SELECT
    *
FROM
    @T;

-- Here is how to get start of period
SELECT
    DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0) AS StartOfDay,
    DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AS StartOfWeek,
    DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS StartOfMonth,
    DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) AS StartOfYear;

-- An example
WITH
StartPeriods AS
(
    SELECT DATEADD(WEEK, DATEDIFF(WEEK, 0, GETDATE()), 0) AS StartOfWeek
),
SelectedDay AS
(
    SELECT
        Dow - 1 AS Dow,
        (SELECT StartOfWeek FROM StartPeriods) AS StartOfWeek
    FROM
        @T
    WHERE
        ShortName = 'Wed'
)
SELECT
    DATEADD(DAY, Dow, StartOfWeek)
FROM
    SelectedDay;