开发者

How to create dates from date components in SQL (T-SQL)?

How can I construct native date data type values in SQL (T-SQL)?

I've added some examples, but please provide your own. My examples assume that the month and year are being stored (or are readily available) as integer values, but maybe your example will assume that the day and the month (or wh开发者_运维知识库atever) are stored as text. I can't see the future; surprise me.


SELECT DATEFROMPARTS(@Year, @Month, @Day)

(From SQL Server 2012)


Why, with input data as strings one of the most obvious (and therefore hardly surprising, sorry) solutions would be:

SELECT
  mydate = CAST([year] + RIGHT('0' + [month], 2) + '01' AS datetime)
                                           /* or 'AS date' in SQL Server 2008+ */
FROM (
  SELECT [month] = '2',  [year] = '2011' UNION ALL
  SELECT [month] = '03', [year] = '2011' UNION ALL
  SELECT [month] = '5',  [year] = '2011' UNION ALL
  SELECT [month] = '12', [year] = '2011' UNION ALL
  SELECT [month] = '8',  [year] = '2084' UNION ALL
  SELECT [month] = '1',  [year] = '1940'
) x;


The following code shows how to create date values from year and month (integer) values:

SELECT  DATEADD(
            month,
            DATEDIFF( month, 0, GETDATE() )
                + x.[month]
                - MONTH( GETDATE() ),
            DATEADD(
                year,
                DATEDIFF( year, 0, GETDATE() )
                    + x.[year]
                    - YEAR( GETDATE() ),
                0 ) )
FROM (  SELECT [month] = 2, [year] = 2011
        UNION ALL
        SELECT [month] = 3, [year] = 2011
    ) x;


Date values from year, month, AND day (integer) values, though maybe the inputs should be sanitized first:

SELECT  DATEADD(
            day,
            x.[day] - DAY(0),
            DATEADD(
                month,
                x.[month] - MONTH(0),
                DATEADD(
                    year,
                    x.[year] - YEAR(0),
                    0 ) ) )
FROM (  SELECT [month] = 2, [year] = 2011, [day] = 14
        UNION ALL
        SELECT [month] = 3, [year] = 2011, [day] = 2
        UNION ALL
        SELECT [month] = 5, [year] = 2011, [day] = 1
        UNION ALL
        SELECT [month] = 7, [year] = 2011, [day] = 0
        UNION ALL
        SELECT [month] = 8, [year] = 2084, [day] = 40
        UNION ALL
        SELECT [month] = 1, [year] = 1940, [day] = -6
    ) x;


More example code to create date values from year and month (integer) values, but even simpler than some other example code:

SELECT  DATEADD(
            month,
            x.[month] - MONTH(0),
            DATEADD(
                year,
                x.[year] - YEAR(0),
                0 ) )
FROM (  SELECT [month] = 2, [year] = 2011
        UNION ALL
        SELECT [month] = 3, [year] = 2011
        UNION ALL
        SELECT [month] = 5, [year] = 2011
        UNION ALL
        SELECT [month] = 7, [year] = 2011
        UNION ALL
        SELECT [month] = 8, [year] = 2084
        UNION ALL
        SELECT [month] = 1, [year] = 1940
    ) x;
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜