Got sql to pad 0's from: Formatting Numbers by padding with leading zeros in SQL Server
What I want to do is get a users order history from our Navigator database tables. We have a process that puts the orders from the website tables into the the navigator tables (because the orders need to be formatted for the nav table).
I want to query the website tables to get the orders from a logged in user using their user id:
SELECT OrderID FROM db1.zm.dbo.zOrder WHERE AccountID = 631180
(this returns multiple order id's)
db1
is the server, zm
is database.
The OrderID's returned are formatted like 4565
, 5675
, ect. I want to insert them into a temp table like: Z0004565
with a Z and enough leading 0's to hit 7 digits for the number.
How do I modifiy the select statement to do so? Or can I.
SQL for padded 0's: SELECT REPLICATE('0', 7-LEN(4665)) + 4665
SQL Following Comments:
DECLARE @OrderNumTable table (orderNum varchar(20))
INSERT INTO @OrderNumTable EXEC( SELECT (''Z'' + REPLICATE(''0'', 7-len(OrderID)) + OrderID)FROM db1.zm.dbo.zOrder WHERE AccountID = 631180