aelor
aelor

Reputation: 11126

Mysql print repeated characters based on length

What I want to do is basically print "xxxx" for "asda" or "xxxxxx" for "forget". The number of characters should be same.

what I have tried in sql

Suppose I run

select length("adsa")

I get 4

and when I run this :

select repeat('x',  4);

I get xxxx

Now when I try doing this :

select repeat('x',  select length("adsa"));

It throws error. Where am I going wrong ?

Upvotes: 0

Views: 354

Answers (2)

Gordon Linoff
Gordon Linoff

Reputation: 1271003

You don't need the select at all in the second argument:

select repeat('x', length('adsa'));

I would also recommend using single quotes for all string constants.

Upvotes: 1

davek
davek

Reputation: 22925

This works for me:

select repeat('x',  (select length("adsa")));

just wrap your last select in brackets.

If you are working against a table you can leave out the extra brackets:

select repeat('x',  length(my_col))
from my_table

Upvotes: 1

Related Questions