|
-
May 13th, 2010, 08:38 AM
#1
[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
-
May 13th, 2010, 11:24 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|