How to create a Date in SQL Server given the Day, Month and Year as Integers

后端 未结 7 774
误落风尘
误落风尘 2020-12-05 12:22

FOR Example if I have:

DECLARE @Day int = 25
DECLARE @Month int  = 10 
DECLARE @Year int = 2016

I want to return

2016-10-2         


        
相关标签:
7条回答
  • 2020-12-05 13:29

    In SQL Server 2012+, you can use datefromparts():

    select datefromparts(@year, @month, @day)
    

    In earlier versions, you can cast a string. Here is one method:

    select cast(cast(@year*10000 + @month*100 + @day as varchar(255)) as date)
    
    0 讨论(0)
提交回复
热议问题