Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 158169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:38:02+00:00 2026-05-11T10:38:02+00:00

What is the best practice for right-justifying a numeric in TSQL? I have to

  • 0

What is the best practice for right-justifying a numeric in TSQL?

I have to format a fixed length extract file and need to have the Numeric fields right justified. (I am using SQL Server 2005)

I found this, which seems pretty straight forward.

right('            '+convert(varchar(20),a.num),12) 

Here is the full Select statement

select     a.num,     fixed_number =         right('            '+convert(varchar(20),a.num),12) from     (     --Test Data     select num = 2400.00    union all     select num = 385.00 union all     select num = 123454.34     ) a  Results:  num        fixed_number  ---------- ------------  2400.00         2400.00 385.00           385.00 123454.34     123454.34  (3 row(s) affected) 

I am asking this question because I found this line of code at work, which appears INSANELY complex (It is also removing the decimal and zero filling)

CAST(REPLACE(REPLICATE('0', 12 - LEN(REPLACE(CAST(CONVERT(DECIMAL(10,2),@DD8DBAMT) AS VARCHAR),'.','')))  + CAST(CONVERT(DECIMAL(10,2),@DD8DBAMT) AS VARCHAR),'.','') AS VARCHAR(12)) 

Updated:

Daniel Pratt’s idea of using a function got me looking at SQL# (which we own). It has a function called PadLeft, which surprisingly enough had the same parameters and functionality as Daniel Pratt’s fn_PadRight function defined in his answer below.

Here is how to use SQL# function:

DECLARE @F6D2 AS DECIMAL(8,2) SET @F6D2 = 0 SQL#.String_PadLeft(@F6D2,9,' ') SQL#.String_PadLeft(123.400,9,' ') SQL#.String_PadLeft('abc',9,' ') 

It can take both numbers and strings.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T10:38:03+00:00Added an answer on May 11, 2026 at 10:38 am

    The only thing I can suggest to help with the ‘insane complexity’ is to encapsulate it in one or more functions. Here’s a somewhat modified version of something we’re using:

    CREATE FUNCTION [dbo].[fn_PadRight] (     @Value nvarchar(4000)     ,@NewLength int     ,@PadChar nchar(1) = ' ' ) RETURNS nvarchar(4000) AS BEGIN     DECLARE @ValueLength int     SET @ValueLength = LEN(@Value)      IF (@NewLength > @ValueLength) BEGIN         SET @Value = @Value + REPLICATE(@PadChar, @NewLength - @ValueLength)     END      RETURN @Value END GO  CREATE FUNCTION [dbo].[fn_FormatAmountDE] (     @Value money ) RETURNS nvarchar(4000) AS BEGIN     RETURN [dbo].[fn_PadRight](REPLACE(CAST(@Value AS varchar), '.', ''), 12, '0') END GO 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is best practice to stream multiple files in one ASIHTTPRequest? Right now, for
The 'best practice' (as I see it) to atomically create a new file, is
I'm asking about the best practice widely used in C++ projects. I need to
In C# I always use PascalCase for properties (best practice, right?), for databases i
Possible Duplicate: C# member variable initialization; best practice? Which is the right way to
What would be best practice in check for set get/post/request? right now i am
I want to know if there is a siverlight client notification best practice, right
Is there a best practice around lazily loading modes when encountering a relevant file
Best practice is to use unique ivs, but what is unique? Is it unique
Short best practice question: If an object A is injected into another object B,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.