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. Problem with SET and SELECT (to assign a local variable)

Problem with SET and SELECT (to assign a local variable)

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelpquestion
3 Posts 2 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.
  • D Offline
    D Offline
    dlarkin77
    wrote on last edited by
    #1

    Hi, I'm using SQL Server 2005. I have the following SQL which is supposed to pad an integer value with leading zeros. The problem is that it prints 0 instead of 001

    DECLARE @UserID INT
    SET @UserID = 1

    DECLARE @UtopiaOrderNumber NVARCHAR
    SET @UtopiaOrderNumber = (SELECT RIGHT(REPLICATE('0', 3) + CONVERT(NVARCHAR(3), @UserID), 3))

    PRINT @UtopiaOrderNumber -- prints 0 instead of 001

    The following SQL does select the correct value:

    DECLARE @UserID INT
    SET @UserID = 1

    SELECT RIGHT(REPLICATE('0', 3) + CONVERT(NVARCHAR(3), @UserID), 3) -- selects 001 as expected

    Can anyone let me know what I'm doing wrong? Thanks, dlarkin77

    C 1 Reply Last reply
    0
    • D dlarkin77

      Hi, I'm using SQL Server 2005. I have the following SQL which is supposed to pad an integer value with leading zeros. The problem is that it prints 0 instead of 001

      DECLARE @UserID INT
      SET @UserID = 1

      DECLARE @UtopiaOrderNumber NVARCHAR
      SET @UtopiaOrderNumber = (SELECT RIGHT(REPLICATE('0', 3) + CONVERT(NVARCHAR(3), @UserID), 3))

      PRINT @UtopiaOrderNumber -- prints 0 instead of 001

      The following SQL does select the correct value:

      DECLARE @UserID INT
      SET @UserID = 1

      SELECT RIGHT(REPLICATE('0', 3) + CONVERT(NVARCHAR(3), @UserID), 3) -- selects 001 as expected

      Can anyone let me know what I'm doing wrong? Thanks, dlarkin77

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

      You used a default for NVARCHAR the command should be

      DECLARE @UtopiaOrderNumber NVARCHAR(3)

      That will give you the output you expect.

      D 1 Reply Last reply
      0
      • C Corporal Agarn

        You used a default for NVARCHAR the command should be

        DECLARE @UtopiaOrderNumber NVARCHAR(3)

        That will give you the output you expect.

        D Offline
        D Offline
        dlarkin77
        wrote on last edited by
        #3

        That sorted it out alright. Thanks 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