Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Padding Function

Posted on 2005-04-12
4
Medium Priority
?
1,384 Views
Last Modified: 2008-10-07
Hello all,

Kind of new at this SQL stuff and I'm needing to pad some values when I run a query.  Does anyone know of a good way to pad values in a query via a function or something?  Also I need the pad to be with 0's with my data right justified. (0's on the left)

Thanks,
BBS
0
Comment
Question by:thcit
4 Comments
 
LVL 23

Expert Comment

by:adathelad
ID: 13763582
e.g.
To pad a value to 4 digits:

DECLARE @intNumber INTEGER
SET @intNumber = 1

SELECT RIGHT('0000' + CAST(@intNumber AS VARCHAR), 4)
0
 
LVL 10

Accepted Solution

by:
rob_lorentz earned 400 total points
ID: 13763599


replace(str(yourNumber, 8), ' ', '0')

this will create a string 8 chars long left padded with 0s
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 13763644
CREATE FUNCTION PadLeft(@text varchar(1000), @fillchar char(1), @totallength int)
RETURNS varchar(1000)
AS
WHILE LEN(@text) < @totallength
     SET @text = @fillchar + @text
RETURN @text
END
0
 
LVL 3

Author Comment

by:thcit
ID: 13763674
Thanks Rob, that was exactly what I needed.  Functional and lightweight.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question