SQL to add space to make the field 20 character long - How ?

mrchonginhk

Well-known Member
Joined
Dec 3, 2004
Messages
679
Suppose I have a field in the table called FName.
This field is of length that is 20 char max in data.
So some records are just 10 char, some 12, but max is 20.

I need to run a query so that FName becomes always 20 char long
For data with char less than 20 long, I need to insert number of spaces behind the FName in table so that it makes up to 20 char long in the output query.

What is the SQL to do this ?
For example
If the data is
"12345678901234567"
Then it should become
"12345678901234567 "
ie adding 3 space behind.

I must use SQL and not VB.

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Code:
Left([FName] & "                    ",20)
Note there are twenty spaces between the double quotes.
 
Upvote 0
There is a VBA Function Space() that works great here.

You could use:

Code:
 Left([FName] & Space(20),20)
 
Upvote 0
There is a <acronym title="visual basic for applications" style="border-width: 0px 0px 1px; border-bottom-style: dotted; border-bottom-color: rgb(0, 0, 0); cursor: help; color: rgb(51, 51, 51); background-color: rgb(250, 250, 250);">VBA</acronym> Function Space()
Interesting. I can't believe in over 12 years of Access programming I never stumbled upon that.
I guess you learn something new everyday!:)
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,510
Members
448,967
Latest member
screechyboy79

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top