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. sql to convert an int to varchar

sql to convert an int to varchar

Scheduled Pinned Locked Moved Database
questiondatabasetoolstutorial
5 Posts 3 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.
  • F Offline
    F Offline
    Fang Ming
    wrote on last edited by
    #1

    newbie's question: how to convert an int to varchar using sql script,such as 1 ->'01' ? thanks

    S 1 Reply Last reply
    0
    • F Fang Ming

      newbie's question: how to convert an int to varchar using sql script,such as 1 ->'01' ? thanks

      S Offline
      S Offline
      subai
      wrote on last edited by
      #2

      i think cast & convert are easiest, declare @x int set @x = 1000; declare @y nvarchar(10) set @y = cast(@x as nvarchar) print @y set @y = convert(nvarchar,@x) print @y


      I Wish the Life Had CTRL-Z

      F 1 Reply Last reply
      0
      • S subai

        i think cast & convert are easiest, declare @x int set @x = 1000; declare @y nvarchar(10) set @y = cast(@x as nvarchar) print @y set @y = convert(nvarchar,@x) print @y


        I Wish the Life Had CTRL-Z

        F Offline
        F Offline
        Fang Ming
        wrote on last edited by
        #3

        ooh. i m sorry i mean how can i convert that to varchar with leading zero: 10 -> 10 0~9 -> '00' ~ '09' and it will be used in a " insert ... select ... from ..." statement. i have a table that containts a datetime column. now i want to transform the data in this to another i have to convert datetime to varchar such as : '2007-09-29 08:05'(datetime) -> '08:05' (varchar) my poor statement is "convert( varchar, datepart( hour , [DateTime] ) ) +':' +convert( varchar, datepart( minute , [DateTime]) ) " but the result is '8:5' what my boss don't want help me. thanks a lot.

        P 1 Reply Last reply
        0
        • F Fang Ming

          ooh. i m sorry i mean how can i convert that to varchar with leading zero: 10 -> 10 0~9 -> '00' ~ '09' and it will be used in a " insert ... select ... from ..." statement. i have a table that containts a datetime column. now i want to transform the data in this to another i have to convert datetime to varchar such as : '2007-09-29 08:05'(datetime) -> '08:05' (varchar) my poor statement is "convert( varchar, datepart( hour , [DateTime] ) ) +':' +convert( varchar, datepart( minute , [DateTime]) ) " but the result is '8:5' what my boss don't want help me. thanks a lot.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          How about convert(varchar(5),getdate(),8)

          F 1 Reply Last reply
          0
          • P PIEBALDconsult

            How about convert(varchar(5),getdate(),8)

            F Offline
            F Offline
            Fang Ming
            wrote on last edited by
            #5

            Great! thank you very much!

            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