简体   繁体   中英

How to select SQL date by keeping Month and day and changing Year to current?

I have a Stored Proc in SQL Server that I am working on updated and trying to come up with a way to change the year in a date (only the year).

Example: Date is 01/05/2010 and at selection, I need the date to 01/05/2017.

You could use datefromparts , if your SQL Server version is >=2012

SELECT 
    CONVERT(VARCHAR(12),
            DATEFROMPARTS(DATEPART(YEAR, GETDATE()),
                          DATEPART(MONTH, '01/05/2010'),
                          DATEPART(DAY, '01/05/2010')), 103)

要选择当前年份,

SELECT DateAdd(YEAR, YEAR(GETDATE())-YEAR(date_field), date_field) FROM TableName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM