[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 879
  • Last Modified:

SQL - Pad blank spaces to beginning of string

Hello,

I have a column called name in table person.

I would like to pad the front of the string with blank spaces up to 12 characters in length:

'John' becomes '        John'
'Johnson' becomes '     Johnson'


Note that there are only 12 total characters in length.  No name will ever be over 12 characters.

Thanks!
0
robthomas09
Asked:
robthomas09
  • 2
2 Solutions
 
Brendt HessSenior DBACommented:
SELECT Right(Space(12) + name, 12)
FROM person

will do what you need.
0
 
robthomas09Author Commented:
would that look like

update person
set name = right(space(12) + name, 12)

Thanks
0
 
Rajkumar GsSoftware EngineerCommented:
This query will add space to left side. Total characters will not exceed 12 characters

Raj
select space(12 - len(urColumn)) + urColumn
from urTable

Open in new window

0
 
Rajkumar GsSoftware EngineerCommented:
Update query will be

Raj
update person set name =  space(12 - len(name)) + name

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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