How to display empty when value of paramters is null?

ahmed salah 3,216 Reputation points
2020-10-21T13:16:18.057+00:00

I work on SQL server 2012 the value

 DECLARE @Company_ID VARCHAR(450)='1006274'  
    DECLARE @PackageDrawingNumber VARCHAR(450)=NULL  
    DECLARE @PartOrderSuffix VARCHAR(450)='N/A'  
    DECLARE @CountOfPCBConnectedPin VARCHAR(450)='N/A'  
  
      
    DECLARE @DKDrawingNumber VARCHAR(MAX);  
    IF @PartOrderSuffix='N/A'  
    SET @PartOrderSuffix=''  
    IF @CountOfPCBConnectedPin='N/A'  
    SET @CountOfPCBConnectedPin=''  

SET @DKDrawingNumber=isnull(@mathieu.company _ID,' ')+isnull('~ '+isnull(@PackageDrawingNumber,''),'') + isnull('~ '+@PartOrderSuffix,'')+isnull('~ '+@CountOfPCBConnectedPin,'')
SELECT @DKDrawingNumber

desired result
1006274

but it display wrong result as below
1006274~ ~ ~

i need when parameters is null not display ~
so how to do that

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,639 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,620 questions
{count} votes

4 answers

Sort by: Most helpful
  1. 21986829 481 Reputation points
    2020-10-21T13:29:19.06+00:00

    DECLARE @mathieu.company _ID VARCHAR(450)='1006274'
    DECLARE @PackageDrawingNumber VARCHAR(450)=NULL
    DECLARE @PartOrderSuffix VARCHAR(450)='N/A'
    DECLARE @CountOfPCBConnectedPin VARCHAR(450)='N/A'

     DECLARE @DKDrawingNumber VARCHAR(MAX);  
     IF @PartOrderSuffix='N/A'  
     SET @PartOrderSuffix=''  
     IF @CountOfPCBConnectedPin='N/A'  
    

    SET @DKDrawingNumber = isnull( @mathieu.company _ID, ' ')
    SET @DKDrawingNumber = @DKDrawingNumber
    + IIF( ISNULL(@PackageDrawingNumber, '') = '', '', '~ ' + @PackageDrawingNumber)
    + IIF( ISNULL(@PartOrderSuffix, '') = '', '', '~ ' + @PartOrderSuffix)
    + IIF( ISNULL(@CountOfPCBConnectedPin, '') = '', '', '~ ' + @CountOfPCBConnectedPin)

    -- + isnull('~ '+isnull(@PackageDrawingNumber,''),'') + isnull('~ '+@PartOrderSuffix,'')
    SELECT @DKDrawingNumber

    0 comments No comments

  2. Viorel 116.6K Reputation points
    2020-10-21T13:55:14.783+00:00

    If CONCAT is available and @mathieu.company _ID is never null, then try this approach too:

    set @DKDrawingNumber = CONCAT(  
        @Company_ID,   
        '~' + @PackageDrawingNumber,   
        '~' + nullif(@PartOrderSuffix, 'N/A'),   
        '~' + nullif(@CountOfPCBConnectedPin, 'N/A'))  
    
    0 comments No comments

  3. Stefan Hoffmann 621 Reputation points
    2020-10-21T14:00:30.273+00:00

    Use ISNULL() and NULLIF(). Concat the values with the spacer before testing for NULL. And the last trick is to add the spacer before the value, Then you can use STUFF() to remove the first spacer (assumption at least on part has a value and you run CONCAT_NULL_YIELDS_NULL ). E.g.

    DECLARE @Company_ID VARCHAR(450) = '1006274';  
    DECLARE @PackageDrawingNumber VARCHAR(450) = NULL;  
    DECLARE @PartOrderSuffix VARCHAR(450) = 'N/A';  
    DECLARE @CountOfPCBConnectedPin VARCHAR(450) = 'N/A';  
      
    SELECT ISNULL(@Company_ID, ' ') +   
    	   ISNULL('~ ' + ISNULL(@PackageDrawingNumber, ''), '') +   
    	   ISNULL('~ ' + @PartOrderSuffix, '') +   
    	   ISNULL('~ ' + @CountOfPCBConnectedPin, '');  
      
    SELECT STUFF(  
               ISNULL('-' + @Company_ID, '') +   
    		   ISNULL('-' + @PackageDrawingNumber, '') +   
    		   ISNULL('-' + NULLIF(@PartOrderSuffix, 'N/A'), '') +   
    		   ISNULL('-' + NULLIF(@CountOfPCBConnectedPin, 'N/A'), '') ,  
               1 , 1 , '');  
    

    34016-image.png

    0 comments No comments

  4. EchoLiu-MSFT 14,581 Reputation points
    2020-10-22T01:45:04.983+00:00

    Hi @ahmed salah

    Please refer to:

    DECLARE @Company_ID VARCHAR(450)='1006274'  
    DECLARE @PackageDrawingNumber VARCHAR(450)=null  
    DECLARE @PartOrderSuffix VARCHAR(450)='N/A'  
    DECLARE @CountOfPCBConnectedPin VARCHAR(450)='N/A'  
                              
    DECLARE @DKDrawingNumber VARCHAR(MAX);  
    IF @PartOrderSuffix='N/A'  
    SET @PartOrderSuffix=''  
    IF @CountOfPCBConnectedPin='N/A'  
    SET @CountOfPCBConnectedPin=''  
    SET @DKDrawingNumber=isnull(@Company_ID,' ')  
          +IIF(@PackageDrawingNumber is not null and @PackageDrawingNumber<>'','~ '+@PackageDrawingNumber ,'')  
          +IIF(@PartOrderSuffix is not null and @PartOrderSuffix<>'','~ '+@PartOrderSuffix,'')  
          +IIF(@CountOfPCBConnectedPin is not null and @CountOfPCBConnectedPin<>'','~ '+@CountOfPCBConnectedPin ,'')  
    SELECT @DKDrawingNumber  
    

    33998-image.png

    For more details, please refer to:
    Logical Functions - IIF (Transact-SQL)

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.