Date to string

  • I'm using this

    CONVERT(VARCHAR, DATEPART(YEAR, CONVERT(DATETIME, Cal.startDate))) +'-'+ CONVERT(VARCHAR, DATEPART(YEAR, CONVERT(DATETIME, Cal.endDate))) + ' School Year'

    and I was wondering if there's a better way or variable type to convert the datetime back to a string.

    The issue at hand is I'm importing an inconsistant date format from raw data, hense the CONVERT(DATETIME, Cal.endDate) and not substring. Then I need only the year, but once I do a "DatePart", I'm getting an integer, so I have to convert back. Would using Convert(char...) be better than varchar?

    In a nutshell, is there a better way or better use of my conversions to get what I want. This works just fine, but I don't care about 'fine' if someone knows something that I don't.

    Thanks 🙂

  • If you use DateName instead of DatePart, for the year part of your calculation, it will be a string already, and you won't need to convert it to varchar.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • That shrunk my code down a ton! Thanks much 😀

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply