Is there a way to get dates with custom formats in SQL Server?

后端 未结 2 1980
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-26 21:55

In Oracle, you can use:

SELECT to_char(sysdate, \'yyyy-mm\') FROM dual;

to show just the year and month portion of a date. Additionally, you ca

2条回答
  •  梦毁少年i
    2021-01-26 22:39

    Currently the best option is to have a CLR function that is a wrapper to .NET's DateTime.ToString(string format).

    If you don't want a separate function, you can build the required string from pieces:

    YEAR(@d) + '-' + MONTH(@d) + '-' + DAY(@d)
    

    As for the definite solution, there will be a formatting function in the next version of SQL Server.

提交回复
热议问题