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.

6
  • I'm guessing you are using sql-server? Have you seen the DATEADD function? Jun 4, 2015 at 19:32
  • yes, sql-server, How to use DATEADD in this case? Thanks. Jun 4, 2015 at 19:36
  • Do you want your query to always return June 16th of the current year? Jun 4, 2015 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, 2015 at 19:50
  • 3
    select dateadd(year, year(getdate())-2015, '20150616'); Jun 5, 2015 at 5:22

1 Answer 1

1

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

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

1
  • 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. Jun 4, 2015 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.