sql select string between two strings in a column - mysql

I have a table for example - TABLE. In the table is column name - CUSTOME_FIELDS. in this column I have data like this:
{"6":"Name of company","1":"11111111","2":"564974195","4":"","5":"","3":""}
I need to take - Name of the company - and give it to new column - NEW_COLUMN.
How can I do that? I tried something like this:
SELECT SUBSTRING(CUSTOME_FIELDS, CHARINDEX('"6":"', CUSTOME_FIELDS), CHARINDEX('","1"',CUSTOME_FIELDS)) FROM TABLE
but it doesn't work.

Just in case your MySQL version is 5.7 or higher, and because the data appears to be in JSON format, you could try your luck with json_extract:
SELECT TRIM(BOTH '"' FROM json_extract(CUSTOME_FIELDS, '$."6"')) AS name FROM your_table;
Demo link

If you're storing JSON data in your database, use MySQL 5.7 or better and use a JSON column type. This means you can easily extract data:
SELECT JSON_EXTRACT(json_data, '$."6"') FROM mytable;

Depends on how your actual data ALL look like:
If your data all look like above example, then you can cut string from {"6":" and next "," :
SELECT substring_index(
substring_index(CUSTOME_FIELD, '{"6":"', -1),
'","',
1
)

Related

search values using like clause on alias column

I have table which contain comma separated string I want to perform like query on 'name' column but 'name' is comma separated so it will not retrieve data easily so I am using replace to eliminate comma and than perform like query on alias column ,but It is not working.is there any way to perform like query on comma separated string
Table:
id name
i school,education
mysql query :
SELECT id,name, lower((REPLACE(name, ',', ''))) as test FROM `list`
where test like '%education%'
You should seriously avoid storing CSV data into single table columns as you are currently doing. That being said, here is one possible workaround:
SELECT id, name
FROM list
WHERE CONCAT(',', LOWER(name), ',') LIKE '%,education,%';
The idea behind the above trick is to build a CSV name string looking something like:
,A,B,C,D,
That is, every single name value is always surrounded by comma boundaries on both sides. Then, we only need to check that ,somename, be present in this CSV string.

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.

Remove a key:value from json string stored in a MySQL database

I have a column in table which is stored in format:
{"field1":"val1","field2":"val4"}
{"field1":"val2","field2":"val5"}
{"field1":"val3","field2":"val6"}
I need to remove all field1 with values(e.g "field1":"val1","field1":"val2","field1":"val3" ) and result should be
{"field2":"val4"}
{"field2":"val5"}
{"field2":"val6"}
I am trying to acheive this via replace but stuck as in '"field1":"val1"' string val1 could be any value like null, some integer.
UPDATE emp SET col = REPLACE(col, '"field1":"val1"', '')
I am stuck due to this dynamic value of val1.
I would prefer to use the JSON_REMOVE function (MySQL) :
UPDATE emp
SET emp.col = JSON_REMOVE(emp.col, '$.field1');
You can also add a WHERE clause :
WHERE emp.col LIKE '%val6%';
References: MySQL JSON_REMOVE and MySQL JSON path
A blog post with examples: MySQL for your JSON
And a note about json path in MySQL:
Propery names in path must be double quoted if the property identifier contains interpunction (spaces, special characters, meta characters) bugs.mysql.com
You can do it like this:
SELECT SUBSTRING(Field, 1, INSTR(Field, '"field1"')) + SUBSTRING(Field, INSTR(Field, '"field2"'), LENGTH(Field)) FROM #Temp
I don't know if this works but this is the idea. (Can't test ATM)
Here is the MsSQL equivalent (works, just tested!):
SELECT SUBSTRING(Field, 0, CHARINDEX('"field1"', Field)) + SUBSTRING(Field, CHARINDEX('"field2"', Field), LEN(Field)) FROM #Temp

Extracting substring from one coloumn and placing it into another SQL

I have a table like so
[filenameAndDate][DateCreated]
the first column looks like this "myvideo/12.12.2012"
and the second column is empty
How would I write a sql query to extract the date from [filenameAndDate] and place it into the [DateCreated] column
UPDATE [dbo].[FileNames]
SET [DateCreated] = Convert(Date,
SUBSTRING(FileNameAndDate, CHARINDEX('/', FileNameAndDate, 0) + 1,
LEN(fileNameAndDate)))
this is for MSSQL
You can try this, mate:
UPDATE
<your_table>
SET
DateCreated = RIGHT(filenameAndDate, 10)
WHERE
filenameAndDate = 'myvideo/12.12.2012';
Suggestion:
Maybe you can organize your table in a way it may not hurt an application based on the content of a field.
Another one is the format of the date you'll be using, it would be better if you use the yyyy-mm-dd format.
PS: this is for MySQL
Cheers!
Equivalent of explode() to work with strings in MySQL
then use it like
insert into blabla (col1, col2) value (val1, SPLIT_STRING(val1,'/',2));

SQL - Query to find if a string contains part of the value in Column

I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?