patternsqlMinor
Transform a number in symbols in a MySql SELECT
Viewed 0 times
numbermysqltransformselectsymbols
Problem
I want to know if is possible to transform a numeric return (INT column) in a sequence of symbols direct in a MySQL SELECT.
My table is like below:
I want a SQL SELECT like
Is it possible?
I can do it programmatically, but this is not my intention, I want some built-in (or even custom) function that can achieve this result direct in the SELECT statement.
My table is like below:
| ID | category |
|-----------|----------|
| product_1 | 5 |
| product_2 | 4 |
| product_3 | 3 |I want a SQL SELECT like
SELECT ID, some_mysql_fancy_func(category,'*') AS symbol_category FROM MyTable that could returns the following:| ID | symbol_category |
|-----------|-----------------|
| product_1 | ***** |
| product_2 | **** |
| product_3 | *** |Is it possible?
I can do it programmatically, but this is not my intention, I want some built-in (or even custom) function that can achieve this result direct in the SELECT statement.
Solution
Maybe use
Granted, this wouldn't work for 0, or you'd have to replace single * with a NULL value. But an idea at least.
LPAD or RPAD?CREATE TEMPORARY TABLE tt (category INT UNSIGNED);
INSERT INTO tt(category)
VALUES
(5);
select lpad('*',category,'*')
from tt;Granted, this wouldn't work for 0, or you'd have to replace single * with a NULL value. But an idea at least.
Code Snippets
CREATE TEMPORARY TABLE tt (category INT UNSIGNED);
INSERT INTO tt(category)
VALUES
(5);
select lpad('*',category,'*')
from tt;Context
StackExchange Database Administrators Q#173868, answer score: 4
Revisions (0)
No revisions yet.