I want to use separator for group_concat() function in mysql.But I want to change the separator dynamically.I mean the separator value is coming from a table and that is different for every row.
I couldn't found any solution for that please help me.
Finally got a solution.
Let's say we have some MySQL routine. We need to GROUP_CONCAT some value with special SEPARATOR (default is ','). But SEPARATOR isn't static, it's got from another table, for ex. from some "settings" table.
DECLARE url_delimiter VARCHAR(255);
SELECT catalog_url_delimiter
INTO url_delimiter
FROM settings;
We can't use variable as SEPARATOR parameter directly:
-- doesn't work
SELECT GROUP_CONCAT(`some_table`.id SEPARATOR url_delimiter)
FROM <some query>
But we can use some dummy separator and replace it with valid as below:
SELECT
REPLACE(
GROUP_CONCAT(`some_table`.id SEPARATOR 'some-tricky-dummy-separator'),
'some-tricky-dummy-separator',
url_delimiter
)
FROM <some query>
Related
Because there are '-' in front of every string in every row, I wanna remove them and make every string data upper at the same time.
I tried the script below, but it didn't work. The ‘-‘ still existed.
My script:
SELECT DISTINCT TRIM(LEADING '-' FROM UPPER(breed)) AS 'upper_breed'
FROM dogs
ORDER BY breed;
Thanks
Your issue is within the ORDER BY clause: there's no "breed" column in your SELECT clause. You should rather use "upper_breed":
SELECT DISTINCT TRIM(LEADING '-' FROM UPPER(breed)) AS 'upper_breed'
FROM dogs
ORDER BY 'upper_breed';
Check the demo here.
Note: avoid using quotes for column names, or if you want to, use backticks (like upper_breed).
As I am working my SQL, for example if I have
select
concat(author_fname, ' ', author_lname)..
....
from...
The question that I have is when I do multiple queries, I have to type that concat command over and over again.
Is there a way that I can set a global variable let's say
set
#full_name =(
select
concat(author_fname, ' ',
author_lname)
from
books
);
Now I only need to do:
select full_name
from books;
But my set command does not work, I don't think declare is the way to go, is there a way to tackle this problem? Thank you so much!
You can use a view:
create view v_books as
select concat(author_fname, ' ', author_lname) as full_name, b.*
from books b;
You will see the column if you select from the view.
Or use a generated column if you want it when selecting directly from the table:
alter table books add full_name varchar(255) generated always as
(concat(author_fname, ' ', author_lname));
I have a string that looks like this.
111,222,333,444,555
how can i add back ticks on each data? I will use it for insert columns.
I tried this one
SELECT
CONCAT('''', REPLACE('111,222,333,444,555', ',', ''','''), '''') AS second
FROM dual;
but it does not support back ticks.
Heres what Im trying to do. I pass a set of string on a stored procedure and the use that as my column when i am trying to insert a data in a table
Try this:
SELECT
CONCAT('`', REPLACE('111,222,333,444,555', ',', '`,`'), '`') AS second
FROM dual;
Output:
second
`111`,`222`,`333`,`444`,`555`
How to remove all spaces between a column field?. The spaces occur in the middle of the text so trim won't work and also replace is not working.
my code is
UPDATE temp_emp t1, master_employee t2
SET t1.lm= t2.emp_id
where REPLACE(t1.lm, ' ', '') = REPLACE(CONCAT(t2.first_name,'',t2.last_name), ' ', '');
for example when i run the query ,
select REPLACE(lm, ' ', '') AS concat from temp_emp1
i get the output as follows
concat
----------------------------------------
rick joe
james cole
albert Th
i want the output to be ;like this
concat
----------------------------------------
rickjoe
jamescole
albertTh
Without knowing the table structures and data, it is difficult for me to follow what you are doing. However, to accomplish the ouput of two concatenated columns is very straightforward.
Assume you have a table master_employee with just two columns and you want to output the FIRST and LAST names concatenated with no spaces in between. You simply use the function concat()for MySQL:
SELECT CONCAT(first_name, last_name)
from master_employee;
In Oracle, the concatenation is two pipes (||):
SELECT first_name || last_name
from master_employee;
Hope this helps.
If you want to update the existing column which has multiple spaces into one then this update query will be helpful:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', ' ');
If you don't want any spaces then this should work:
UPDATE your_table SET column_that_you_want_to_change=
REGEXP_REPLACE(column_that_you_want_to_change, '[[:space:]]+', '');
my raw query look something like this-
UPDATE main,category,sub_category
SET main.biz_keyword = (category.category','sub_category.sub_cat_name','main.biz_keyword)
so the result something like main.biz_keyword='Doctor,General Physician,Physician'
I know this is wrong query but you got the Idea what I am looking for,
So my question is that I can do this by single query?
You might want to have a look at using CONCAT_WS(separator,str1,str2,...)
CONCAT_WS() stands for Concatenate With Separator and is a special
form of CONCAT(). The first argument is the separator for the rest of
the arguments. The separator is added between the strings to be
concatenated.
is this something you want to achieve?
Update TableName
set biz_keyword = category.category + ',' + sub_category.sub_cat_name + ',' + main.biz_keyword
Maybe you're looking for something like this?
UPDATE
main
SET
biz_keyword = CONCAT_WS(', ',
(SELECT category FROM category WHERE ... ),
(SELECT sub_cat_name FROM sub_category WHERE ... ),
biz_keyword)