1

I have to get the current year with June the 16th as a date.

For example, if I run the query now I want to get 2015-06-16.

I tried the following:

CONVERT(DATETIME,cast( (cast(YEAR(GetDate()) AS VARCHAR(4))+'-06-16 00:00:00.000' as varchar (24) ),102)

but it didn't work.

  • I'm guessing you are using sql-server? Have you seen the DATEADD function? – James Anderson Jun 4 '15 at 19:32
  • yes, sql-server, How to use DATEADD in this case? Thanks. – Jackie G Ma Jun 4 '15 at 19:36
  • Do you want your query to always return June 16th of the current year? – James Anderson Jun 4 '15 at 19:39
  • 3
    If you are using SQL server 2012 then you can use DateFromParts(Year(GetDate()), 6, 16). Check the link for further info : msdn.microsoft.com/en-us/library/hh213228.aspx – SelmanAY Jun 4 '15 at 19:50
  • 3
    select dateadd(year, year(getdate())-2015, '20150616'); – Mikael Eriksson Jun 5 '15 at 5:22
1

The following T-SQL gives you the result you need.

SELECT CONVERT(DATETIME, CONVERT(VARCHAR(4), YEAR(GetDate()))+'-06-16')

  • Also: SELECT CONVERT(date, CONVERT(VARCHAR(4), GETDATE(), 112)+'0616'); (this will also work for dates before the year 1000). Your expression will need left padding with 0s to work for such dates. – ypercubeᵀᴹ Jun 4 '15 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.