Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics GP (Archived)

Sql Query

Posted on by

Dear,

I have a column  on an sql query  and I want to add either one or two zeros before the numbers in my SQL query result depending on length of the value. 
Column results have a max of 7 digit numbers in it but at times it will only have 5, 6 or 7. 
So when a number result is 12345 I want it to show up as 0012345, or when it's 123456 I want it to show as 0123456, or if it's 1234567 then nothing changes.How can we achieve this in Microsoft Sql Server.

RomRyan

*This post is locked for comments

  • Verified answer
    Mariano Gomez Profile Picture
    Mariano Gomez 26,225 on at
    RE: Sql Query

    Here you go:

    declare @t table (SomeNumber int);
    insert into @t values(12345);
    insert into @t values(123456);
    insert into @t values(1234567);
    
    select SUBSTRING('0000000', 1, 7 - LEN(CONVERT(varchar(20), SomeNumber))) + CONVERT(varchar(20), SomeNumber) from @t; 
    


Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans