Repeated Characters in SQL
February 2, 2005 Hey, Ted
Using SQL, how do I fill a 128-byte character field with asterisks? Please don’t tell me I have to key every one of those puppies into my SQL command.
–Jack
I’ve got good news. Use the SPACE function to generate 128 spaces. Use the TRANSLATE function to convert the spaces into asterisks.
insert into somefile (somefield) values (translate(space(128),'*',' '))
Or use the REPLACE function to convert the spaces into asterisks.
insert into somefile (somefield) values (replace(space(128),' ','*'))
I haven’t run any speed tests, but TRANSLATE seems to run faster than REPLACE.
If you had wanted to fill your field with a literal longer than one character, you would have had to use REPLACE. The following SQL command repeats the word VOID, followed by one blank, throughout the field.
insert into somefile (somefield) values (replace(space(128),' ','VOID '))
Notice there are five blanks between the first two apostrophes and one blank after VOID.
–Ted