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`
Related
I have a column in my table that contains 10-digit hts codes (0000.00.0000). Some of the values do not have the full stop points (0000000000). How can I add the full stop points to all the rows that do not have them?
Edit
The column type is VARCHAR
I want to update all rows where full stop is not present.
I would remove the full stops from all these columns using REPLACE() as part of the update, then you can apply some simple logic using a CONCAT() LEFT(), RIGHT() and SUBSTRING()
to change the simple 0000000000 into 0000.00.0000 like this, rather than trying to identify only the columns without the dots
UPDATE table
set column = CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
);
Test it using a select so you do no damage
SELECT some_identifying_column,
CONCAT(
LEFT(REPLACE(column, '.', ''),4),
'.' ,
SUBSTRING(REPLACE(column, '.', ''),5,2),
'.',
RIGHT(REPLACE(column, '.', ''),4)
) as justtesting;
Another approach using insert comes to mind. As others already mentioned, it's a good idea to remove the full stops before inserting them in the 5th and 8th position in the string
select *, insert(insert(replace(hts,'.',''),5,0,'.'),8,0,'.')
from t;
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 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>
I am trying to make an update on a DB replacing old characters for new ones, my problem is that at the middle of that string I have some characters I have to remove but I dont know the length or the type, I just know when the sub string starts and when it ends, for exmaple
my-string=XXXXXXXX-endstring
so, Id like to make it like this
new-string-endstring
I know my-string value, but dont know the sub string in the middle XXXXX
is there any way to do it?
Thanks.
Update fields which begin with my-string- AND end with -endstring:
UPDATE table_name
SET column_name = 'new-string-endstring'
WHERE column_name LIKE 'my-string-%'
AND column_name LIKE '%-endstring';
If you use PHP you could do the following:
1) Make a SELECT of all rows that have the my-string text inside the column value
SELECT * FROM table_name WHERE column_name LIKE '%my-string%'
the % acts as a wildcard
2) Loop through all these result rows and use PHP's str_replace to replace my-string for new_string, and then use MySQL's UPDATE to update the new value of the column in the DB
You can use SUBSTRING_INDEX to get the required string and use CONCAT to append new string.
Update TableA
Set columnA = CONCAT( 'newstring' , SUBSTRING_INDEX(columnA, '-', -1) )
I would like to concatenate column names in a way that the first part of the column name is a string and the second part is a number which is the result of another query.
For example:
SELECT CONCAT('column', mytable.mycolumn) FROM table ...
Can this be done in some way. This way it doesn't give me errors but I don't get the expected result and it seems the concatenation doesn't work.
I previously said that this couldn't be done, but I was wrong. I ended up needing something like this myself so I looked around, and discovered that server-side prepared statements let you build and execute arbitrary SQL statements from strings.
Here is an example I just did to prove the concept:
set #query := (
select concat(
"select",
group_concat(concat("\n 1 as ", column_name) separator ','),
"\nfrom dual")
from information_schema.columns
where table_name = 'columns')
;
prepare s1 from #query
;
execute s1
;
deallocate prepare s1
;
If the number of columns is fixed, then a non-dynamic approach could be:
select
case mytable.mycolumn
when 1 then column1 -- or: when 'a' then columna
when 2 then column2
when ...
else ...
end as my_semi_dynamic_column
from ...
I don't believe you can do this with CONCAT() and CONCAT_WS(). I'd recommend using the langauge you are working with the create the field names. Doing it this way would be pretty scary, depending on where the data in the database came from.
I would suggest looking at information_schema. The following code is untested but should theoretically work. Obviously replace your table name with an appropriate table name or link to information_schema.tables and use the table_type in your where clause
select concat('column', column_name) from information_schema.columns where table_name ='your table name'