Split DATE formatted 'YYYY-MM-DD' into Concatenated YEAR and MO field

  • Hello, I have a date field in the DATE in the format of 'YYYY-MM-DD' and how it is displayed, and I'm trying to split our Year and Month and concatenate into 'YYYYMM' format. I've tried DATEPART, DATEADD and  other variations and I'm not quite getting the correct result or error.

  • It is incorrect to talk about a format for a date column itself.  A date is always stored in a binary form with no display format associated with it.
    SELECT convert(varchar(6),SYSDATETIME(),112);

  • Bill Talada - Friday, June 30, 2017 10:20 AM

    It is incorrect to talk about a format for a date column itself.  A date is always stored in a binary form with no display format associated with it.
    SELECT convert(varchar(6),SYSDATETIME(),112);

    where would i insert the date field in question to convert, and would I want to name it 'as colname'?

  • SELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;

  • What is the actual data type of your "date field"?

  • Bill Talada - Friday, June 30, 2017 10:33 AM

    SELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;

    LEFT(CONVERT(char(8), datecolname, 112), 6) AS YearMo

    This does it for me, and so  does the  above

    Thank  you

  • quinn.jay - Friday, June 30, 2017 10:41 AM

    Bill Talada - Friday, June 30, 2017 10:33 AM

    SELECT t.name, convert(varchar(6),t.create_date,112) AS MyDateCol FROM sys.tables t;

    LEFT(CONVERT(char(8), datecolname, 112), 6) AS YearMo

    This does it for me, and so  does the  above

    Thank  you

    You don't need the LEFT if you convert to a 6-chars long string. 😉

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 7 posts - 1 through 6 (of 6 total)

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