web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics GP (Archived)

Sql Query

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Suggested answer
    MG-16101311-0 Profile Picture
    26,225 on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics GP (Archived)

#1
mtabor Profile Picture

mtabor 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans