Yet another approach (not sure how you want to handle numbers greater than 99):
DECLARE @numbers AS TABLE(Val INT)
INSERT INTO @numbers VALUES(1)
INSERT INTO @numbers VALUES(2)
INSERT INTO @numbers VALUES(10)
INSERT INTO @numbers VALUES(11)
INSERT INTO @numbers VALUES(98)
INSERT INTO @numbers VALUES(99)
INSERT INTO @numbers VALUES(100)
SELECT REPLACE(STR(Val, 2), ' ', '0') FROM @numbers
Explanation : The LPAD function Left-pad the string with 3rd parameter( i.e 0 in given example) up to length given in 2nd parameter ( i.e 2 in given example)
more example
SELECT LPAD('hello', 10, '*');
Result : *****hello
DECLARE @table AS TABLE(n INT)
INSERT INTO @table
VALUES(1),(3),(5),(9),(10),(50),(99),(100)
Select Case When n<10 Then '0'+Cast(n as varchar(10)) Else Cast(n as varchar(10)) End From @table
9条答案
按热度按时间bbmckpt71#
So long as n is strictly less than 100, you can use
In SQL Server 2012, you can also use FORMAT, and the following will work and also give you '100' for 100.
iqxoj9l92#
Yet another approach (not sure how you want to handle numbers greater than 99):
sbtkgmzw3#
bq9c1y664#
%2d
needs to translated to2 digit
format in sql serverTry this
Result :-
In order to check for numbers between
0 to 100
usecase
statementbejyjqdl5#
you can use LPAD function of sql
Try this
Explanation : The LPAD function Left-pad the string with 3rd parameter( i.e 0 in given example) up to length given in 2nd parameter ( i.e 2 in given example)
more example
f8rj6qna6#
Try this
Eg:
Output:
0050
yzckvree7#
Do this:
w8ntj3qf8#
This would smart way to solve your problem...
nfzehxib9#