SQL Query

  • I'm trying to make a script where I can add leading zeros to numbers less than 7. I have it almost completed. My brain just doesn't seem to be functioning correctly.

    SELECT Med_Rec_Nbr

    FROM Primary_Summary

    where LEN(Med_Rec_Nbr) < 7

    That will select the numbers I need. I just help trying to put leading zeros up to 7 in front of my results. If anyone can help me I would so appreciate it.

  • Is the Med_Rec_Nbr a varchar? If it's an integer or other numeric type, you won't be able to add leading zeros.

    Here's an example:

    SELECT Nbr,

    RIGHT(REPLICATE('0',7)+Nbr,7)

    FROM (VALUES('1'),

    ('12'),

    ('123'),

    ('1234'),

    ('12345'),

    ('123456'),

    ('1234567'))x(Nbr);

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • I tend do use the RIGHT function. For example, we have something called "branches" which should be displayed as two digit integer values. Therefore, if I need then ensure branches 0-9 are two part i do:

    RIGHT('0' + CAST(B@ AS VARCHAR(2)),2) AS Branch --Note that B@ is an INTEGER within the Table, hence the CAST.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • declare @Primary_Summary table (Med_Rec_Nbr int)

    insert into @Primary_Summary (Med_Rec_Nbr) VALUES (1234567),(123456),(12345),(2345678)

    SELECT RIGHT(CONCAT('0000000',Med_Rec_Nbr),7)

    FROM @Primary_Summary

    where LEN(Med_Rec_Nbr) < 7

    Adi

    --------------------------------------------------------------
    To know how to ask questions and increase the chances of getting asnwers:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I forgot to mention Med_Rec_Nbr is a nvarchar. I had the cast in my original just trying to do a little more efficient. Thanks for all the helpful replies I did mark the one that worked best for me.

  • SELECT ps.Med_Rec_Nbr, REPLACE(STR(ps.Med_Rec_Nbr,7),' ','0')

    FROM @Primary_Summary AS ps

    This approach also makes it obvious if you got a number larger than expected, rather than truncating to 7 characters.

    Wes
    (A solid design is always preferable to a creative workaround)

  • whenriksen (12/1/2016)


    SELECT ps.Med_Rec_Nbr, REPLACE(STR(ps.Med_Rec_Nbr,7),' ','0')

    FROM @Primary_Summary AS ps

    This approach also makes it obvious if you got a number larger than expected, rather than truncating to 7 characters.

    It's also slower. It's up to 5 times slower than the other options presented.

    Quick performance test:

    SELECT TOP (1000000)

    ISNULL(CAST( ABS(CHECKSUM(NEWID())) % POWER(10,((ABS(CHECKSUM(NEWID()))%9)+1)) AS nvarchar(10)), N'') AS Med_Rec_Nbr

    INTO Primary_Summary

    FROM sys.all_columns a, sys.all_columns;

    --SET STATISTICS IO ON;

    GO

    DECLARE @Dummy nvarchar(10);

    PRINT '-------------------------------------';

    PRINT 'Dry Run';

    PRINT '-------------------------------------';

    SET STATISTICS TIME ON;

    SELECT @Dummy = Med_Rec_Nbr FROM Primary_Summary WHERE LEN(Med_Rec_Nbr) < 7;

    SET STATISTICS TIME OFF;

    PRINT '-------------------------------------';

    PRINT 'REPLACE(STR(ps.Med_Rec_Nbr,7),'' '',''0'')';

    PRINT '-------------------------------------';

    SET STATISTICS TIME ON;

    SELECT @Dummy = REPLACE(STR(Med_Rec_Nbr,7),' ','0') FROM Primary_Summary WHERE LEN(Med_Rec_Nbr) < 7;

    SET STATISTICS TIME OFF;

    PRINT '-------------------------------------';

    PRINT 'RIGHT(CONCAT(''0000000'',Med_Rec_Nbr),7)';

    PRINT '-------------------------------------';

    SET STATISTICS TIME ON;

    SELECT @Dummy = RIGHT(CONCAT('0000000',Med_Rec_Nbr),7) FROM Primary_Summary WHERE LEN(Med_Rec_Nbr) < 7;

    SET STATISTICS TIME OFF;

    PRINT '-------------------------------------';

    PRINT 'RIGHT(REPLICATE(''0'',7)+Nbr,7)';

    PRINT '-------------------------------------';

    SET STATISTICS TIME ON;

    SELECT @Dummy = RIGHT(REPLICATE('0',7)+Med_Rec_Nbr,7) FROM Primary_Summary WHERE LEN(Med_Rec_Nbr) < 7;

    SET STATISTICS TIME OFF;

    PRINT '-------------------------------------';

    --SET STATISTICS IO OFF;

    GO 5

    DROP TABLE Primary_Summary;

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Another option:

    SELECT @Dummy = CASE WHEN LEN( Med_Rec_Nbr ) < 7

    THEN RIGHT('000000' + Med_Rec_Nbr,7)

    ELSE Med_Rec_Nbr

    END

    FROM Primary_Summary

    Performance is very close to the other proposals, and it doesn't truncate values longer than 7.

    Edited: Forgot to remove Where clause

    Wes
    (A solid design is always preferable to a creative workaround)

  • whenriksen (12/1/2016)


    Another option:

    SELECT @Dummy = CASE WHEN LEN( Med_Rec_Nbr ) < 7

    THEN RIGHT('000000' + Med_Rec_Nbr,7)

    ELSE Med_Rec_Nbr

    END

    FROM Primary_Summary

    WHERE LEN( Med_Rec_Nbr ) < 7;

    Performance is very close to the other proposals, and it doesn't truncate values longer than 7.

    Why are you using a CASE when you have a WHERE?

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 9 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic. Login to reply