Concatenate Year and Month

Bone_12 361 Reputation points
2022-12-07T11:34:14.127+00:00

Hi,

I have 2 date fields, surv_year (int) and surv_month (text).

Example outputs:
surv_year: 2022
surv_month: January

surv_year: 2022
surv_month: August

Is there anyway to concatenate the 2 and change the month from a text to a number and have my final output as YYYYMM with my example above showing 202201 and 202208?

Developer technologies | Transact-SQL
SQL Server | Other
{count} votes

Accepted answer
  1. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2022-12-08T03:00:39.533+00:00
    Select  Format(try_cast(concat_ws(' ',surv_year,surv_month,'1') as date),'yyyyMM') [yyyyMM]  
     from yourtable   
    
    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2022-12-07T13:49:42.18+00:00

    Hi @Bone_12 ,

    Please try the following solution.
    It will work starting from SQL Server 2017 onwards due to dependency on the CONCAT_WS() function.

    SQL

    -- DDL and sample data population, start  
    DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, surv_year int, surv_month VARCHAR(20));  
    INSERT @tbl (surv_year, surv_month) VALUES  
    (2022, 'January'),  
    (2022, 'August');  
    -- DDL and sample data population, end  
      
    SELECT t.*  
    	, realDate  
    	, result = FORMAT(realDate, 'yyyyMM')  
    FROM @tbl AS t  
    	CROSS APPLY (SELECT TRY_CAST(CONCAT_WS('-', surv_year, surv_month, '01') AS DATE)) AS t1(realDate);  
    

    Output

    +----+-----------+------------+------------+--------+  
    | ID | surv_year | surv_month |  realDate  | result |  
    +----+-----------+------------+------------+--------+  
    |  1 |      2022 | January    | 2022-01-01 | 202201 |  
    |  2 |      2022 | August     | 2022-08-01 | 202208 |  
    +----+-----------+------------+------------+--------+  
    
    1 person found this answer helpful.
    0 comments No comments

  2. Olaf Helper 47,436 Reputation points
    2022-12-07T12:33:52.053+00:00

    Create a mapping table "month name" => "month number" for your query.

    0 comments No comments

  3. NikoXu-msft 1,916 Reputation points
    2022-12-08T05:47:31.617+00:00

    Hi @Bone_12 ,

    Try this:

    create  TABLE tb (ID INT IDENTITY PRIMARY KEY, surv_year int, surv_month VARCHAR(20));  
     INSERT into tb (surv_year, surv_month) VALUES  
     (2022, 'January'),  
     (2022, 'August'),  
     (2022,'December');  
      
    select *,CONCAT([surv_year] , month(surv_month+'1,1')) as [date]  from tb  
    

    Best regards,
    Niko

    ----------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Resh1309 1 Reputation point
    2022-12-09T18:21:58.12+00:00

    You can do it with a concat:

    SELECT YEAR(getdate()) || MONTH(getdate()) AS 'yyyymm'

    OR

    SELECT YEAR(getdate()) || '-' || MONTH(getdate()) AS 'yyyy-mm'

    Replace the getdate() with your own date field.

    Mark the answer if it worked! ;-)


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.