• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

Padding Columns?

Hello I need a function to pad columns (int and varchar).

select name +' '+ address+' '+city+' '+id from users;

should return:

Mike            88 evergreen terrace      halifax
Jason Jones      16 way ave            Halifax

In other words all columns should be x numbers of characters wide after being padded.

Any suggestions?
0
91mustang
Asked:
91mustang
  • 2
1 Solution
 
91mustangAuthor Commented:
oops formatting did not hold up...

The columns that make up the one field that gets returned should line up.
0
 
91mustangAuthor Commented:
mike   street   city   1
jon     street   city   1
0
 
rafranciscoCommented:
Try this:

SELECT Name + SPACE(80 - LEN(Name)) + Address + SPACE(100 - LEN(Address)) + ID
FROM Users

This assumes that you want Name to be 80 characters long and address to be 100 characters long.
0
 
geotigerCommented:

Rafrancisco's approach is right. Here is all the columns that you have listed assuming that you want them to be 50 chars width:

select name+space(50-len(name))+address+space(50-len(address))+city+space(50-len(city))+id
from users


0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now