CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2004
    Posts
    206

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

    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

    Code:
    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:

    Code:
    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

  2. #2
    Join Date
    Oct 2004
    Posts
    206

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

    My problem was how I declared @UtopiaOrderNumber.

    The following works fine:

    Code:
    DECLARE @UserID INT
    SET @UserID = 1
    
    DECLARE @UtopiaOrderNumber NVARCHAR(3)
    SET @UtopiaOrderNumber = (SELECT RIGHT(REPLICATE('0', 3) + CONVERT(NVARCHAR(3), @UserID), 3))
    
    PRINT @UtopiaOrderNumber

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured