×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

formatting

formatting

formatting

(OP)
I want to format results in my query.
I want the length to be 6.
If its 5 in length I want to add a zero at the end
If the value is 4 in length I want to add a leading zero and a zero at the end.

Does some have an example of this.
Thanks

Select number from table

results "number":
2534
2521
15556
25111

I want the result to be:
025340
025210
155560
251110

RE: formatting

Isn't it a job for your front end?

CODE

DECLARE @test TABLE (results int)
INSERT INTO @Test VALUES (2534)
INSERT INTO @Test VALUES (2521)
INSERT INTO @Test VALUES (15556)
INSERT INTO @Test VALUES (25111)

SELECT RIGHT('000000'+LEFT(CAST(Results*10000 as varchar(10)),5),6) FROM @Test 
But this code is valid if you don't have 6 or more digits value.

Borislav Borissov
VFP9 SP2, SQL Server

RE: formatting

(OP)
This is for a backend process. This seems to work but not for values that are 5 in length

example 25111

need it to be: 251110

RE: formatting

How about?

CODE

;WITH t AS (
SELECT Number, CAST(Number AS VARCHAR) String
  FROM (VALUES (2534), (2521), (15556), (25111), (123456)) v(Number)
)

SELECT *,
       CASE LEN(String)
	  WHEN 4 THEN '0' + String + '0'
	  WHEN 5 THEN String + '0'
	  ELSE String
       END FormattedString
  FROM t 

RE: formatting

>This is for a backend process
Where does this end in? In a report? In generated data?
A report IS a frontend thing, even when reporting services are used. Output formatting is a feature there, which doesn't need the crappy string functionalities T-SQL only offers.
And if this data ends in more generated data in a backend, the need to format it that way would just delay to anywhere it will be shown or printed.

Besides left AND right padding is very unusual.

Bye, Olaf.



RE: formatting

Numbers, in relational databases, do not have leading zeros. When you add leading zeros, you are creating a string or character or varchar variable. what's wrong with DaveInIowa's solution? Seems straightforward and accurate.

==================================
advanced cognitive capabilities and other marketing buzzwords explained with sarcastic simplicity


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close