1

I have a character string and for reporting/alignment purpose I need to insert a space after each character. Unfortunately I will have to do it in a SQL or I can write format function.

e.g. "abcd123" to be converted it to "a b c d 1 2 3 ".

Since it's for a considerable number of rows I was wondering how optimized it will be to parse each character and build a new string each time?

Any help is appreciated.

Thanks

2
  • I'm curious to know what the reporting reasons are. Commented Dec 18, 2009 at 14:11
  • Thanks Xinus something similar first came to my mind. But what I have read is MySQL does not have that function. I came across a User defined library on a website but dont know how to use that and will that mean I need to copy the libs to customer site? Any help you can provide about it is much appreciated. Commented Dec 18, 2009 at 14:33

3 Answers 3

3

Here's a quick & dirty mysql function which solves your problem:

delimiter ||
DROP FUNCTION IF EXISTS concat_whitespace||
CREATE FUNCTION concat_whitespace( x longtext) RETURNS longtext
LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA 
BEGIN
DECLARE len INT UNSIGNED;
DECLARE erg LONGTEXT;
SET len = LENGTH(x);
REPEAT
SET erg = CONCAT_WS(' ',SUBSTRING(x,len,1),erg);
SET len = len - 1;
UNTIL len < 1 END REPEAT;
return erg;
END;
||

Now try this:

Select concat_whitespace("abcd123")

which returns "a b c d 1 2 3"

This function basically loops for each character and concats a space. Direction is from end to beginning, which saves a counter variable.

I havn't done any performance test, fur sure you somehow could optimize this..

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks RngTng for breaking the ice for me. I think I can take it forward.
@BT.: If you were able to improve this function, I'd love to see the results.
2

I would not recommend storing unnecessary spaces in SQL. This will only waste space.

How about writing a custom retrieval method that will return all strings from your DB with spaces after each character?

3 Comments

Yeah thanks for the advice. Actually the resultant string will not be stored into the database but will be used on a report. If I were to write a function that takes a string value as a parameter and puts the space in each of them. Can it be done?
It sure can. What language are you generating this report with?
Its a proprietary reporting tool that offers virtually no support to format/manipulate the string retrieved from database. Hence the value returned by a query has to have a space between characters. The end report has a rigid format and alignment issues. I know it sucks cause it's a legacy system. Don't have a luxury to have the string processed by a high level language before display. Thanks guys for the time, efforts and answers.
0
update tablename set comment= trim(concat(substr(comment, 1,1),space(1),substr(comment, 2,1),space(1),substr(comment, 3,1),space(1),substr(comment, 4,1),space(1),substr(comment, 5,1),space(1),substr(comment, 6,1),substr(comment, 7)));

here comment is column name

1 Comment

What if the lengths are not consistent?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.