Null case with concatenation

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Null case with concatenation



I have the below situation in my project:



@num1 is an integer
1. when @num1 = null/' ' then output should be space(0)
2. else if it is single digit, concatenate with '0' in the front. If double digit, show as it is.



Example:


1. @num1 = null/'' output: space(0)
2. @num1 = '0' output:00
3. @num1 = '9' output:09
4. @num1 = '11' output = 11



My code:


declare @num1 int = null
select @num1,
CASE
WHEN cast(@num1 as varchar) = null THEN space(0)
ELSE RIGHT(CONCAT('00', CONVERT(VARCHAR,@num1)),2)
END as 'output'



This code works fine for examples 2,3,4. But when I set @num1 = null, the output still shows as '00'. desired output should be space(0)/space. Missing some conversion here. Any help?









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived