0

I am very new to SQL and I need help trying to figure something out for work.

So I have a table called Acxiom and one of the fields is called Account_ID. Account ID's are all unique but they either start with 07 or 10 and are supposed to be 9 characters long. For some reason, the ERP system that we pull data from does not include the leading zero in its warehouse. I need to link the Acxiom table to another table with Account ID acting as the primary key. I can't do that unless I add the leading zero.

Right now is an example of what the table looks like:

79570233
79574812
79575113
103885860
103834660
102836484

I need it to look like this:

079570233
079574812
079575113
103885860
103834660
102836484

I genuinely appreciate everyone's help!

Kristijan Iliev
  • 4,901
  • 10
  • 28
  • 47
Drew
  • 171
  • 1
  • 3
  • 11
  • 1
    possible duplicate of [Pad a string with leading zeros so its 3 characters long in SQL Server 2008](http://stackoverflow.com/questions/16760900/pad-a-string-with-leading-zeros-so-its-3-characters-long-in-sql-server-2008) – Hart CO Sep 17 '15 at 14:46

1 Answers1

0
DECLARE @pad INT = 9;

SELECT RIGHT(REPLICATE('0', @pad) + Account_ID, @pad)
FROM Acxiom;

or if column is number type then cast:

DECLARE @pad INT = 9;

SELECT RIGHT(REPLICATE('0', @pad) + CAST(Account_ID AS NVARCHAR(10)), @pad)
FROM Acxiom;

or without replicate:

SELECT RIGHT('000000000' + Account_ID, 9)
FROM Acxiom;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275