Convert and Substring

Ysa8989 41 Reputation points
2020-09-12T16:09:38.377+00:00

Hello

I am trying to convert the birth of date into the listed format

mm-dd-yyyy

I am using convert( varchar birth_date, 110) and it works but I have a hard time to extract the month & date to create a string

I need the month & day like 0925

Please help!!!

Developer technologies Transact-SQL
{count} votes

5 answers

Sort by: Most helpful
  1. Anonymous
    2020-09-12T16:30:47.9+00:00
    declare @birth_date date = '20200912'  
      
    select   
    	@birth_date  
    	, format(month(FORMAT( @birth_date, 'MM/dd/yyyy')), '00') + format(day(FORMAT( @birth_date, 'MM/dd/yyyy')), '00')  
    
    
      
    
    0 comments No comments

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2020-09-12T17:12:31.343+00:00

    Here is another solution:

    declare @birth_date date = '20200912'  
    SELECT right(convert(char(8), @birth_date, 112), 4)  
    

    A disadvantage with the format function is that is implemented in the CLR and the context switch incurs on overhead which can be costly when there many rows.

    0 comments No comments

  3. Jingyang Li 5,896 Reputation points Volunteer Moderator
    2020-09-12T17:21:57.907+00:00
    declare @bdate date = '20000925'
    
    Select Convert(varchar(10),@bdate,101) --mm/dd/yyyy
    , Convert(varchar(10),@bdate,110) --mm-dd-yyyy
    
    
    ,format(@bdate,'MM-dd-yyyy') --mm-dd-yyyy
    
    ,format(@bdate,'MMdd') --MMdd
    , Right(Convert(varchar(8),@bdate,112),4) --MMdd
    ,Concat(Format(Month(@bdate),'00'),Format(Day(@bdate),'00')) --MMdd
    ,Right('00'+Cast(Month(@bdate) as varchar(2)),2)+Right('00'+Cast(Day(@bdate) as varchar(2)),2)  --MMdd
    

  4. Anonymous
    2020-09-12T17:23:22.34+00:00

    Therefore, if FORMAT is less performing we can minimize its invocation.
    Thinking about it better, it can be done!

    declare @birth_date date = '20200912'
    
     select 
         @birth_date
         , format(@birth_date, 'MMdd')
    

    Thanks to Erland for the food for thought.

    0 comments No comments

  5. EchoLiu-MSFT 14,621 Reputation points
    2020-09-14T05:56:13.44+00:00

    Hi @Ysa8989

    You can use format function if you need a specific date format
    (FORMAT (Transact-SQL)):

        declare @birth_date date = '20200925'  
        select convert( varchar(15), @birth_date, 110) birthdate,format(@birth_date,'MMdd')  
    

    24422-image.png

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Best Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    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

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.