Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Date Convertion

Date Convertion

Scheduled Pinned Locked Moved Database
databasetutorialquestion
11 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yesu prakash
    wrote on last edited by
    #1

    Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

    T B T J P 5 Replies Last reply
    0
    • Y yesu prakash

      Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

      T Offline
      T Offline
      Thats Aragon
      wrote on last edited by
      #2

      You can do something like below.

      DECLARE @Input as varchar(10) = '2011-11-06'
      select case when datepart(day,@Input) <= 9 then
      cast('0' + cast(datepart(day,@Input) as varchar(1)) as varchar(2))
      else cast(datepart(day,@Input) as varchar(2)) end AS NewDate

      Regards

      1 Reply Last reply
      0
      • Y yesu prakash

        Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        If you have always same format of datetime yyyy-MM-dd then you can use substring e.g select substring('2011-11-06',9,2)


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

        C 1 Reply Last reply
        0
        • B Blue_Boy

          If you have always same format of datetime yyyy-MM-dd then you can use substring e.g select substring('2011-11-06',9,2)


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post. www.cacttus.com

          C Offline
          C Offline
          Corporal Agarn
          wrote on last edited by
          #4

          If they are using a date column, I think this would be better select substring(Cast(@mydate as varchar(10)),9,2)

          1 Reply Last reply
          0
          • Y yesu prakash

            Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

            T Offline
            T Offline
            Tim Carmichael
            wrote on last edited by
            #5

            How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)

            modified on Tuesday, June 7, 2011 3:49 PM

            L Y T 3 Replies Last reply
            0
            • T Tim Carmichael

              How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)

              modified on Tuesday, June 7, 2011 3:49 PM

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              left? :doh:

              Luc Pattyn [My Articles] Nil Volentibus Arduum

              The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
              Please use <PRE> tags for code snippets, they improve readability.
              CP Vanity has been updated to V2.3

              modified on Friday, June 10, 2011 8:41 PM

              T 1 Reply Last reply
              0
              • L Luc Pattyn

                left? :doh:

                Luc Pattyn [My Articles] Nil Volentibus Arduum

                The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
                Please use <PRE> tags for code snippets, they improve readability.
                CP Vanity has been updated to V2.3

                modified on Friday, June 10, 2011 8:41 PM

                T Offline
                T Offline
                Tim Carmichael
                wrote on last edited by
                #7

                Good catch... fixed. It DID produce the correct answer, but that was happenstance, not design.

                1 Reply Last reply
                0
                • Y yesu prakash

                  Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

                  J Offline
                  J Offline
                  jschell
                  wrote on last edited by
                  #8

                  I suspect that you are trying to solve a problem which you have not stated. You asked about the solution that you think will solve the problem but if you stated the actual problem a better solution might exist.

                  1 Reply Last reply
                  0
                  • T Tim Carmichael

                    How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)

                    modified on Tuesday, June 7, 2011 3:49 PM

                    Y Offline
                    Y Offline
                    yesu prakash
                    wrote on last edited by
                    #9

                    gr8...exactly what i required.. thankyou

                    1 Reply Last reply
                    0
                    • T Tim Carmichael

                      How about: (corrected... thanks Luc) select right('0' + convert(varchar(2),datepart(day,'2011-11-06')), 2)

                      modified on Tuesday, June 7, 2011 3:49 PM

                      T Offline
                      T Offline
                      Thats Aragon
                      wrote on last edited by
                      #10

                      Good call :) Have my 5.

                      Regards

                      1 Reply Last reply
                      0
                      • Y yesu prakash

                        Hi, When i execute the query select convert(varchar(2),datepart(day,'2011-11-06')) the result i get is 6, i need to get the answer as 06 if only one digit, if 2 then no need of 0 in the first place. How to get that? Regards, YPKI

                        P Offline
                        P Offline
                        Prasanta_Prince
                        wrote on last edited by
                        #11

                        SELECT CONVERT(VARCHAR(10), GETDATE(), 105)</pre>
                        Place your date inplace of Gatedate()

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups