How to select parts of string in MySQL 5.x - mysql

I have a varchar(255) field within a source table and the following contents:
50339 My great example
2020002 Next ID but different title
202020 Here we go
Now I am processing the data and do an insert select query on it. From this field I would need the INT number at the beginning of the field. IT IS followed by 2 spaces and a text with var length, this text is what I need as well but for another field. In General I want to to put text and ID in two fields which are now in one.
I tried to grab it like this:
SELECT STATUS REGEXP '^(/d{6,8}) ' FROM products_test WHERE STATUS is not null
But then I learned that in MySQL 5.x there are no regexp within the SELECT statement.
How could I seperate those values within a single select statment, so I can use it in my INSERT SELECT?
From the correct solution of user slaakso, resulted another related problem since somtimes the STATUS field is empty which then results in only one insert, but in case there is a value I split it into two fields. So the count does not match.
My case statement with his solution somehow contains a syntax problem:
CASE STATUS WHEN ''
THEN(
NULL,
NULL
)
ELSE(
cast(STATUS as unsigned),
substring(STATUS, locate(' ', STATUS)+3)
)
END

You can do following. Note that you need to treat the columns separately:
select
if(ifnull(status, '')!='', cast(status as unsigned), null),
if(ifnull(status, '')!='', substring(status, locate(' ', status)+2), null)
from products_test;
See db-fiddle

Related

SQL Select if substring occurs then copy until substring else keep original

I have a database with TV Guide data, and in my description field (VARCHAR) sometimes i have a '|' where behind it is the rating. I used to check this in php, before converting it all to XML, but i would like to do this in SQL.
So if i have this string:
This is the description | rating pg-13
Then i want to keep the
This is the description
but if there is no '|' i want the whole string.
I tried using substring, but can't get it to work.
My query now is:
SELECT *, SUBSTRING(`long_description`, 1, POSITION('|' IN `long_description`)) FROM `programs` WHERE station_id = 1
this works only one way - this gives me the string before the '|' but if there is no '|' it gives an empty column.
Based on the use of backticks, you might be using MySQL. If so, substring_index() does exactly what you want:
select substring_index(long_description, '|', 1)
How about this:
SELECT
*,
IF(long_description LIKE '%|%',
SUBSTRING(`long_description`,
1,
POSITION('|' IN `long_description`)),
long_description)
FROM
`programs`
WHERE
station_id = 1
The IF clause basically just checks if you have a | in the field and applies your routine when this is true. Else it will simply return the complete long_description value.

How to sort the string on the basis of numbers?

I am working on the sql query in which I want to sort the string on the basis of numbers.
I have one column (Column Name is Name) table in which there are multiple fields. On using ORDER BY NAME, it prints in the following way:
hello_world
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
hello_world4
hello_world5
For the above query, I have used ORDER BY NAME; but it doesn't seem to print on the basis of numbers.
Problem Statement:
I am wondering what sql query I need to write or what changes I need to make in my sql query above so that it prints everything on the basis of numbers, the o/p should be this:
hello_world
hello_world4
hello_world5
hello_world10
hello_world11
hello_world12
hello_world13
hello_world14
hello_world15
you want a numeric ordering, then you need to create a numeric value to order on.
currently you have strings.
if the pattern is true, then you can use a combination of string manipulation to trim off the first characters, which should leave only numbers, then use TO_NUMBER() to convert for the ordering
something like
select name
from mytable
order by to_number( replace( name, 'hello_world','' ))
I think the simplest solution for this particular case (where all the values have the same prefix) is:
order by length(name), name
Try this:
SELECT name,
CASE WHEN REGEXP_INSTR(name, '[0-9]') = 0 THEN 0
ELSE CAST(SUBSTR(name, REGEXP_INSTR(name, '[0-9]')) AS INT)
END AS progressive
FROM my_table
ORDER BY progressive;
we can order it using replace and cast methods.
I tried the following query
select Name, cast(REPLACE(Name, 'hello_world', '') as UNSIGNED ) as repl from Users order by repl;
To generage sample data
CREATE TABLE Users (
Name varchar(255) NOT NULL
);
insert into Users(Name) values
('hello_world'),
('hello_world4'),
('hello_world5'),
('hello_world10'),
('hello_world11'),
('hello_world12'),
('hello_world13'),
('hello_world14'),
('hello_world15')
;
EDIT
query without replaced column,
select City from Persons order by cast(REPLACE(City, 'hello_world', '') as UNSIGNED );
Though the question is about mysql.
I tried in sql server.
create table #t1 (id varchar(100));
insert into #t1 (id) values ('Pq1'),('pq3'),('pq2')
select * from #t
order by
CAST(SUBSTRING(id + '0', PATINDEX('%[0-9]%', id + '0'), LEN(id + '0')) AS INT)

Join two columns of the same table ignoring the null value. MySql

I'm trying to join two columns of the same table but, if there are null values (in my case they are in the second column), I want to take anyway the row that interests me and, rather than putting the null value, I would put ''. The columns that I want to join are Surname and Name. In other words, I tried to use :
SELECT CONCAT(CSurname, ' ', CName)
FROM Client;
In this way if I have a valid value for surname and a null value for name I obtain null. I use MySql, thanks.
If you want to avoid the problem with a leading space, then the easiest way is probably CONCAT_WS():
SELECT CONCAT_WS(' ', CSurname, CName)
FROM Client;
Unlike most other functions, CONCAT_WS() ignores NULL values (except for the separator), greatly simplifying this logic -- particularly when you are working with more than two columns.
Without it, the equivalent logic could be expressed as:
SELECT CONCAT(COALESCE(CONCAT(CSurname, ' '), ''), COALESCE(CName, ''))
Try the ifnull function
SELECT CONCAT(CSurname, ' ', IFNULL(CName,'')) FROM Client;
I don't have a local mysql installation to try it out but the IFNULL function should achieve what you need.

I want to insert a value multiple time in a same field without disturbing the previous data

eg : field name = User_id
Value=abc later i want to insert xyz without disturbing abc Value= abc,xyz i want to insert efg without disturbing abc then Value= abc,xyz,efg and so on
i want to seperating each value by using ","(comma). can any one help me out
In MySQL you could often refer to the value of a column just by using the column name. And to concatenate strings with a separator there's a nifty function called concat_ws (concat with separator).
In your case the code would look something like
UPDATE YourTable
SET Value = CONCAT_WS(',', Value, 'cde')
WHERE User_id = 123;
Good Luck!
MySQL CONCAT_WS() function is used to join two or more strings with separator. The separator specified in the first argument is added between two strings. The separator itself can be a string. If the separator is NULL the result is NULL.
Click hear for more information

Complicated SELECT

Maybe I'm going in the wrong direction with this, so I'd love some pointers on how to approach this differently.
I have multiple MySql database tables with 50+ TEXT fields (categories) each (reason, problem, cause, disposition ... etc.), some (most) of these are null, some aren't, depending on the item they are describing.
I want to convert these to a 'topic', 'text' table instead. for example
item, reason , problem , cause , disposition
0001, null , broken widget, unknown , null
0002, returned, null , customer unhappy, in transit
to
item, topic , text
0001, problem , broken widget
0001, cause , unknown
0002, reason , returned
0002, cause , customer unhappy
0002, disposition, in transit
So, I'm fiddling with a select query, this is what I got so far:
SELECT item, problem AS 'text', 'problem' AS 'topic' FROM my_table
WHERE problem IS NOT NULL;
how would I go about adding the next category? Like
SELECT item, cause AS 'text', 'cause' AS 'topic' FROM my_table
WHERE cause IS NOT NULL;
1: how would I add that to the previous select statement so that it would cover both 'cause' and 'problem'?
2: since there are so many categories, is there a way to avoid the manual labor here and iterate through field names somehow?
You need to be very careful about using single quotes in queries. A simple rule: only use single quotes for string and date constants. Otherwise, the escape character is the backtick -- and if you name columns and tables correctly, you don't need that.
So this query:
SELECT item, cause AS 'text', 'cause' AS 'topic'
FROM my_table
WHERE cause IS NOT NULL;
Should be:
SELECT item, cause AS text, 'cause' AS topic
FROM my_table
WHERE cause IS NOT NULL;
Then, you can combine these using union all:
SELECT item, problem AS text, 'problem' AS topic FROM my_table WHERE problem IS NOT NULL UNION ALL
SELECT item, cause AS text, 'cause' AS topic FROM my_table WHERE cause IS NOT NULL
And, you can keep adding subqueries for all the columns you want to include this way.
EDIT:
If you want to do this with information_schema.columns, you can use replace:
select replace('SELECT item, <colname> AS text, ''<colname>'' AS topic FROM my_table WHERE problem IS NOT NULL UNION ALL',
'<colname>', column_name)
from information_schema.columns c
where table_name = XXX and
column_name <> 'item';
You can use UNION ALL:
SELECT item, problem AS text, 'problem' AS topic
FROM my_table
WHERE problem IS NOT NULL
UNION ALL
SELECT item, cause AS text, 'cause' AS topic
FROM my_table
WHERE cause IS NOT NULL;
To save some time building the query you could use INFORMATION_SCHEMA to list the columns:
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='yourdatabasename'
AND `TABLE_NAME`='my_table';
Either concatenate the query with the COLUMN_NAME or just paste the list of column names into Excel and build the query there and copy/down.