April 26, 2022 at 8:20 pm
I'm scratching my head over this. I'm sure it's something simple, but I'm stumped. I have the following T-SQL:
DECLARE @MAX_naam AS varchar(MAX) = (SELECT SUBSTRING(MAX(naam),LEN(MAX(naam))-1,LEN(MAX(naam))) FROM incident); -- = 22
DECLARE @maxnaam_length AS int = LEN(@MAX_naam);
DECLARE @num_zeroes AS int = 5 - @maxnaam_length;
DECLARE @zeroes AS varchar =
CASE WHEN @num_zeroes = 0 THEN ''
WHEN @num_zeroes = 1 THEN '0'
WHEN @num_zeroes = 2 THEN '00'
WHEN @num_zeroes = 3 THEN '000'
ELSE '' END;
select @zeroes
@MAX_naam is currently 22. @maxnaam_length is, correctly 2, and @num_zeroes is 3. I've verified each of these. However, despite expecting @zeroes to be '000', it comes out as '0'. Why?
April 26, 2022 at 8:32 pm
DECLARE @zeroes AS varchar
If you declare a variable or parameter as varchar w/o specifying length -- e.g., varchar(5) -- it is implicitly declared as varchar(1), and can only contain one character.
April 26, 2022 at 8:46 pm
OK, you're right. However, when I change my code to DECLARE @zeroes AS varchar(5) I get the same result. @zeroes = '0'
April 26, 2022 at 8:54 pm
I don't - I get '000' if @max_naam = '22'. Does @MAX_naam really need to be varchar(max)?
DECLARE @MAX_naam AS varchar(MAX) = '22'
DECLARE @maxnaam_length AS int = LEN(@MAX_naam);
DECLARE @num_zeroes AS int = 5 - @maxnaam_length;
SELECT @MAX_naam AS MAX_naam,@maxnaam_length AS maxnaam_length, @num_zeroes AS num_zeroes;
DECLARE @zeroes AS VARCHAR(5) = CASE WHEN @num_zeroes = 0 THEN ''
WHEN @num_zeroes = 1 THEN '0'
WHEN @num_zeroes = 2 THEN '00'
WHEN @num_zeroes = 3 THEN '000'
ELSE '' END;
select @zeroes
What does this return?
SELECT MAX(naam) AS max_naam, SUBSTRING(MAX(naam),LEN(MAX(naam))-1,LEN(MAX(naam))) AS max_naam_len FROM incident
April 26, 2022 at 9:30 pm
Rather than a CASE, use the built-in REPLICATE function:
DECLARE @MAX_naam AS varchar(MAX)
SET @MAX_naam = 22 --(SELECT SUBSTRING(MAX(naam),LEN(MAX(naam))-1,LEN(MAX(naam))) FROM incident); -- = 22
DECLARE @maxnaam_length AS int = LEN(@MAX_naam);
DECLARE @num_zeroes AS int = 5 - @maxnaam_length;
DECLARE @zeroes varchar(5) = REPLICATE('0', @num_zeroes)
SELECT @zeroes
SQL DBA,SQL Server MVP(07, 08, 09) "It's a dog-eat-dog world, and I'm wearing Milk-Bone underwear." "Norm", on "Cheers". Also from "Cheers", from "Carla": "You need to know 3 things about Tortelli men: Tortelli men draw women like flies; Tortelli men treat women like flies; Tortelli men's brains are in their flies".
April 27, 2022 at 12:05 am
Are you just trying to LPAD zeros for a total length of 5? If so, this will make your life easier...
DECLARE @SomeInt INT = 22;
SELECT RIGHT(CONCAT('00000',@SomeInt),5)
;
Note that @SomeInt in the formula could actually be a column name from a table.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 27, 2022 at 6:12 pm
Oh bless you, Scott Pletcher, this works wonderfully.
April 28, 2022 at 4:16 am
Oh bless you, Scott Pletcher, this works wonderfully.
I have a question just out of curiosity... what are you going to do with the zeroes once you have the correct number of zeros returned as a string?
--Jeff Moden
Change is inevitable... Change for the better is not.
April 28, 2022 at 2:28 pm
DECLARE @naam AS varchar(20) = 'I' + RIGHT(CAST(DATEPART(yy,GETDATE()) AS varchar),2) + RIGHT('0'+CAST(DATEPART(mm,GETDATE()) AS varchar),2) + '-' + @zeroes + @MAX_naam;
April 28, 2022 at 6:58 pm
This should give you the same result as your complicated calculation
SELECT @naam = 'I' + CONVERT(char(4), GETDATE(), 12) + '-' + RIGHT('00000' + @MAX_naam, 5)
April 28, 2022 at 7:13 pm
DesNorton, your solution is so much simpler. Thank you very much!
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply