Wrox Programmer Forums
|
SQL Server 2005 General discussion of SQL Server *2005* version only.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 8th, 2009, 08:01 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add Left Padding

Hi,

I would like to kbnow how to update a field to add a left padding of 5 spaces. I have a column with numbers in it eg:

F1

2345
1256
1569

I need to have it as:

F1
2345
1256
1569

Thank you

Greg
 
Old September 8th, 2009, 10:20 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

*IF* the field is a *NUMERIC* data type (INTEGER, REAL, DECIMAL, etc.) then the answer is simple: YOU CAN'T. Numbers are stored in an *internal* format that has no cognizance of you we silly humans want to see them displayed.

You would have to store the data a strings, in a VARCHAR or, more likely, CHAR field if you wanted to have padding.
 
Old September 8th, 2009, 10:53 PM
Authorized User
 
Join Date: Jul 2006
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. The field is CHAR.
Thanks
 
Old September 8th, 2009, 11:14 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

I have to tell you that storing formatted data in a database is absolutely one of the very worst things you can do to a database. Stop and think about what such left padding will do to simple WHERE clauses.

Why do you need to do this?
__________________
--Jeff Moden
 
Old September 9th, 2009, 02:23 AM
Authorized User
 
Join Date: Sep 2009
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I agree. You should format the data after you retrieve it from the database. How are your presenting the data?
 
Old September 9th, 2009, 05:51 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Well, I agree that the padding is a BAD thing almost all the time. Very seldom is there a reason to do padding in the DB. But....

If you MUST do it:
Code:
UPDATE yourtable SET yourfield = RIGHT('     ' + yourfield, 5)





Similar Threads
Thread Thread Starter Forum Replies Last Post
LEFT vs LEFT OUTER joxa83 SQL Server 2005 3 September 18th, 2008 03:13 AM
"Padding" a value in XSL francislang XSLT 1 September 3rd, 2004 07:56 AM
Add padding zero to year Genuine Beginning PHP 2 August 6th, 2004 07:09 AM
padding problem anshul HTML Code Clinic 5 July 24th, 2004 11:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.