How to split string and insert the second part into another column - mysql

I have a column which contains string in the following format: strin1/string2. I need to take string2 part and insert it into another column, string3. I am aware of substr( ) function, but this function would require me to know the index of the character & in my case, this is not know.

substr(col, instr(col, '/')+1)

SELECT SUBSTRING_INDEX('string1/string2','/',-1);
like that????

Related

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

Append a "+" at the beginning of every field without a "+"

I need to write a query that appends a "+" on the front of every p2_number meta_key that doesn't already begin with a "+". The name of the table is "wp_4_postmeta". I attached an image of the database so you can see what I'm talking about. http://mmw-file-sharing.s3.amazonaws.com/2015/Screen%20Shot%202015-07-30%20at%204.12.25%20PM.png
UPDATE aTable
SET someField = CONCAT('+', someField)
WHERE someField NOT LIKE '+%'
;
If someField is also indexed, the query should be fairly quick as well.
Update table set column = concat("+", column) where column not like "+%"
Should do it
You can check using substr if the first character is a +. If so, return just the field value, else prepend a +.
select
case when substr(p2_number, 1, 1) = '+' then
p2_number
else
concat('+' p2_number)
end as p2_numberplus
from
wp_4_postmeta
Or do you mean actually updating the table data? In that case, Uueerdo's answer is the one you want.

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));

Returns 5th object and soon and make this as a long integer

I want to return the 5th character or in a id number.
Ex. I have id number PID-15, I want to return the query after the '-' so in this example 15 then make this a a integer or long integer. How can I achieve this? what particular function that can make this?
I think you can use SUBSTRING_INDEX
SELECT SUBSTRING_INDEX(id,'-',-1) from your_table;
SQLFiddle Demo
Try this code
If you want to get this out of your table using SQL, take a look at the following functions that will help you: SUBSTRING and CHARINDEX. You can use those to trim your entries.
A possible query will look like this (where col is the name of the column that contains your image directories:
SELECT SUBSTRING(col, LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) + 1, LEN(col) - LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) - LEN(SUBSTRING(col, CHARINDEX ('.', col), LEN(col))));

MySQL SELECT WHERE 'a' IN (`field`)

I know it is not an appropriate technique to have a structure of MySQL table as such, but I have to work with such. The problem is, that the field in table has value with comma seperated integers, like "1,3,5,7,10" and I want the query to return rows, in which field has a to the query passed number in it, like:
SELECT * FROM `table` WHERE '5' IN (`field_in_table`)
However, it does not work, if, in this case, '5' is not the first number in the field.
Any advises, how to solve that?
Thanks in advance,
Regards,
Jonas
Have a look at
FIND_IN_SET
Returns a value in the range of 1 to N
if the string str is in the string
list strlist consisting of N
substrings. A string list is a string
composed of substrings separated by
“,” characters. If the first argument
is a constant string and the second is
a column of type SET, the
FIND_IN_SET() function is optimized to
use bit arithmetic. Returns 0 if str
is not in strlist or if strlist is the
empty string.
You could use WHERE field_in_table LIKE '%5%' instead.
Of course, the problem would be, '1,59,98' would return as wel.
SELECT * FROM table WHERE field_in_table LIKE '%5'");
should work
You could try
SELECT *
FROM table
WHERE '%,5,%' LIKE field_in_table OR
'%,5' LIKE field_in_table OR
'5,%' LIKE field_in_table;
A better approach might be to use regular expressions, a subject on which I am not an authority.
SELECT *
FROM table
WHERE FIELD LIKE '%,5,%' OR
FIELD LIKE '5,%' OR
FIELD LIKE '%,5'