get month and year from my column

RAVI 1,076 Reputation points
2022-12-06T07:40:12.173+00:00

Hello

This Is My Column Which Is Varchar Datatype

267653-image.png

I Want OutPut Like This Month-Year From Above Data

267627-image.png

Thanking You

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

2 answers

Sort by: Most helpful
  1. LiHongMSFT-4306 31,566 Reputation points
    2022-12-06T08:33:44.09+00:00

    Hi @RAVI
    Try this:

    DECLARE @Test table(Order_Recieved_Month VARCHAR(20))  
    INSERT INTO @Test VALUES ('01-09-2022 00:00'),('01-10-2022 00:00')  
    --1  
    SELECT LEFT(DATENAME(MONTH,FORMAT(CAST(Order_Recieved_Month AS DATE),'dd/MM/yyyy')),3)+'-'+RIGHT(DATENAME(YEAR,Order_Recieved_Month),2) AS mm_yy  
    FROM @Test  
    --2  
    SELECT FORMAT(CONVERT(date, Order_Recieved_Month,103),'MMM-yy') AS mm_yy  
    FROM @Test  
    

    Refer to this blog for more examples of FORMAT: Format SQL Server Dates with FORMAT Function

    Output:
    267677-image.png

    Best regards,
    LiHong


    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

  2. Tom Cooper 8,481 Reputation points
    2022-12-06T08:47:14.66+00:00

    Another way

    Create Table Sample(Order_Recieved_Month varchar(20));  
    Insert Sample(Order_Recieved_Month) Values  
    ('01-09-2022 00:00'),  
    ('09-02-2022 00:00');  
      
    Select Order_Recieved_Month, Stuff(Convert(char(12), Convert(date, Order_Recieved_Month, 103), 107), 4, 7, '-')  
    From Sample;  
      
    

    Tom

    0 comments No comments

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.