Scott on Writing

Musings on technical writing...

SQL Tip: Displaying a Padded Number

A common requirement for reports is to left or right pad a number so that it conforms to a certain format or so that all the numbers in a column are aligned. Padding a string in VB or C# code is a cinch thanks to the String class's PadLeft and PadRight methods. But what if you need to apply the padding directly in your SQL SELECT query?

Microsoft T-SQL includes an STR function that takes as input a numeric value and returns a string. You can optionally provide a length, which indicates how many characters the resulting string contains, with the numeric value right-aligned. For example, the statement:

PRINT '|' + STR(1, 3) + '|'

Outputs:

|  1|

In other words, the STR function displays the numeric value - 1 - as a string of three characters. Because 1 only requires one character, the first two characters are spaces. If needed, you can use SQL's REPLACE function to replace the spaces with some alternate character. For example, the following left pads the passed in number with 0s.

PRINT REPLACE(STR(1, 3), ' ', '0')    -- Outputs 001

In general, you can left pad a number using the following pattern:

REPLACE(STR(numberToPrint, totalLength), ' ', 'characterToLeftPadWith')

And now a challenge for you, my dear reader: Can you come up with a general pattern for a T-SQL statement for right padding a number? The inputs would be numberToPrint, totalLength, and characterToRightPadWith.

posted on Sunday, January 18, 2009 8:10 AM

Feedback

# re: SQL Tip: Displaying a Padded Number 1/18/2009 10:44 AM dave@aucklandsql.com

REPLACE(CAST(numberToPrint AS CHAR(totalLength)), ' ', 'character ToRightPadWith')

# re: SQL Tip: Displaying a Padded Number 1/19/2009 3:50 AM Richard

I'm curious. Is this a problem you've run into and the only way to do this is in sql? Seems like this sort of presentation code should be handled client-side.

# re: SQL Tip: Displaying a Padded Number 1/19/2009 6:14 PM hello

perfect

# re: SQL Tip: Displaying a Padded Number 1/20/2009 7:40 AM Scott Mitchell

Richard, I agree that in theory this stuff should certainly be handled in the presentation layer. But theory and practice do not always intersect!

This particular client had a table with each record signifying some completed task, and each task had a "Certificate ID" that had a rather complex set of business rules as to how it was generated. This "Certificate ID" used to be a varchar field and the user creating/editing the record was responsible for typing it in. But this led to data entry errors. Since it was a read only field, I created a UDF that generated this "Certificate ID" given the unique identifier for a record from this table.

Now, why not do this all in the presentation layer? They had a number of reports coming straight out of the database and not being routed through the presentation layer (Excel data dumps via SSIS, for instance). So I created a UDF and off we went.

# re: SQL Tip: Displaying a Padded Number 1/20/2009 7:50 AM Kevin Priester

I have historically used the T-SQL STUFF function to accomplish this.

# re: SQL Tip: Displaying a Padded Number 1/23/2009 5:30 AM Jason Kohlhoff

I agree that this issue isn't strictly a presentation layer problem. If you're joining tables together using a column that doesn't have the required padding, then you have to add padding using T-SQL.

# re: SQL Tip: Displaying a Padded Number 1/23/2009 5:32 AM Jason Kohlhoff

This is typically what I use...

DECLARE @numberToPrint AS FLOAT
DECLARE @totalLength AS INT
DECLARE @characterToRightPadWith AS CHAR

SET @numberToPrint = 123.45
SET @totalLength = 10
SET @characterToRightPadWith = '0'

-- Right Padding
SELECT LEFT(CAST(@numberToPrint AS VARCHAR) + REPLICATE(@characterToRightPadWith, @totalLength), @totalLength)

-- Left Padding
SELECT RIGHT(REPLICATE(@characterToRightPadWith, @totalLength) + CAST(@numberToPrint AS VARCHAR), @totalLength)

# Scott on Writing - SQL Tip: Displaying a Padded Number 1/23/2009 3:45 PM DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

Title:  
Name:  
Url:
Protected by Clearscreen.SharpHIPEnter the code you see:
Comments   

My Links

Ads Via DevMavens

Archives

Post Categories

 

I am a Microsoft MVP for ASP.NET.
I am an ASPInsider.
<March 2010>
SMTWTFS
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910

Comment Stats

DayTotal% of Total
Sunday 2056.8%
Monday 42514.1%
Tuesday 51917.2%
Wednesday 55518.4%
Thursday 58019.2%
Friday 54718.1%
Saturday 1886.2%
Total 3019100.0%

Hour1Total% of Total
12:00 AM 782.6%
1:00 AM 812.7%
2:00 AM 682.3%
3:00 AM 822.7%
4:00 AM 692.3%
5:00 AM 1264.2%
6:00 AM 1183.9%
7:00 AM 1816.0%
8:00 AM 1926.4%
9:00 AM 1585.2%
10:00 AM 1886.2%
11:00 AM 1936.4%
12:00 PM 2016.7%
1:00 PM 1846.1%
2:00 PM 1695.6%
3:00 PM 1354.5%
4:00 PM 1153.8%
5:00 PM 1073.5%
6:00 PM 1013.3%
7:00 PM 1073.5%
8:00 PM 923.0%
9:00 PM 882.9%
10:00 PM 913.0%
11:00 PM 953.1%
Total 3019100.0%

Comments by Blog Entry Date/Time

Day Entry MadeAvg.Total
Sunday 4.97159
Monday 4.80384
Tuesday 4.04477
Wednesday 7.39680
Thursday 6.26676
Friday 5.07466
Saturday 4.78177
Total 5.403019

Hour1 Entry MadeAvg.Total
12:00 AM 5.2937
1:00 AM 1.002
5:00 AM 0.000
7:00 AM 3.8550
8:00 AM 3.72134
9:00 AM 6.06297
10:00 AM 5.63276
11:00 AM 4.22194
12:00 PM 6.16351
1:00 PM 3.09133
2:00 PM 4.89230
3:00 PM 7.64321
4:00 PM 4.00108
5:00 PM 6.07170
6:00 PM 4.64116
7:00 PM 8.95188
8:00 PM 8.63164
9:00 PM 5.00115
10:00 PM 6.31101
11:00 PM 4.5732
Total 5.403019

Learn More About Comment Stats
1 - All times GMT -8...


Blog Stats

Favorite Web Sites

My Books

My MSDN Articles