I have 2 fields in my table: start_date and title.
start_date is NULL.
title contains string like this: 'treatment at date 11/12/2012, at ... '.
I want to copy only the date intostart_date(fromtitle` field.)
what query will do that?
Thank you very match!
You can't. Mysql's support for regexp is only used in the where-part.
You have to either extract it into something else (java, perl, ruby) and manipulate it there or you can try to use locate, substring and other string functions (but none of them are regexp)
MySQL support for regular expressions is very limited, but if the format of your title column is almost fixed, and the date is always followed by ', at ...' you could use STR_TO_DATE in conjunction with two SUBSTRING_INDEX:
UPDATE yourtable
SET start_date = STR_TO_DATE(
SUBSTRING_INDEX(SUBSTRING_INDEX(title, ', at', 1), ' ', -1), '%d/%m/%Y')
The first SUBSTRING_INDEX will return everything before the first ', at' substring, and the second one will return everything after the last space fount.
Please see fiddle here.
I considered dates in the format of day/month/year, if it is month/day/year just use '%m/%d/%Y'
Related
I have a column in database and having value like this
course_repeatfkfjkjfjkfer_10_topics_0_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_1_presentation_link
course_repeatfkfjfkfkfklfflkflkfs_2_presentation_link
coursek_epeatfkfjfkfkfklfflkflkfs_10_presentation_link
course_hdhdhhdhdjhdrepeatfkfjfkfkfklfflkflkfs_21_presentation_link
and so on.
I need to pick 0,1,2,10,21, number before _presentation_link , But i need this in mysql as well
i used substr in mysql, but that is not working. Any idea?
Thanks
One option would be to use a combination of SUBSTRING_INDEX() and REPLACE():
SELECT SUBSTRING_INDEX(REPLACE(col, '_presentation_link', ''), '_', -1)
FROM yourTable
Taking course_repeatfkfjkjfjkfer_10_topics_0_presentation_link as an example, after the replacement, this would become:
course_repeatfkfjkjfjkfer_10_topics_0
The call to SUBSTRING_INDEX() then grabs everything appearing after the final underscore, which is the number you want to capture.
Demo here:
SQLFiddle
You can use substring_index twice like this:
select substring_index(substring_index(col, '_', -3), '_', 1)
from t
Demo
I have a table which i am using to query and getting its one column which matches regular expression which is (\/.+\/\?).
Content of the resulted column is like:
/Anything here/?
Example output:
\abc\cdf\?....
\ab\?....
\abc\cdf\?....
\sb\?....
where '....' can be anything
Desired result i want is unique values before \? such that rows with duplicate regexp matched content are shown once only like here (\abc\cdf\?.... showing twice instead of onece)
\abc\cdf\?....
\ab\?....
\sb\?....
OR
\abc\cdf\?
\ab\?
\sb\?
I have looked very much but couldn't find anything there is regexp_substr in oracle but that is not working in SQL.
Please if someone could help me with the sql query that would be awesome.
If you want everything before the last \, then you can use substring_index() and some string manipulation:
select substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
) as firstpart,
count(*)
from table t
group by substring_index(col, '\\',
length(col) - length(replace(col, '\\', ''))
);
I need to remove the last part of a string in a column where I have a field named "path" that looks like:
images/prop/images/2034/22399_2034.JPG
I need everything after the last "/" to be deleted, in order to have
images/prop/images/2034/
instead of
images/prop/images/2034/22399_2034.JPG
I have no idea if this is possible. Thanks.
You can combine MySQL's TRIM() and SUBSTRING_INDEX() functions:
SELECT TRIM(TRAILING SUBSTRING_INDEX(path, '/', -1) FROM path)
FROM my_table
See it on sqlfiddle.
It is not my code, its something that I need to get it done without modifying the structure of table. I know it would be very easy to just store date as MySQL date format but I cant do that.
There is a column in table which stores serialized array as a string. Now I need to select all rows whose 'date' is less than today.
This date is inside serialized array string.
Is there a way to compare it on mysql query? An example string is:
a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";}
I need to compare the "date" from this string to mysql date using the following query:
"date" BETWEEN 2013-01-01 AND 2013-05-23
You can extract the date value (assuming it's always set off by "date";s:10) using nested SUBSTRING_INDEX calls. The inner one returns everything after "date";s:10" and the outer one cuts off the closing quote and whatever follows:
SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1)
If val is a:3:{s:4:"test";b:1;s:2:"se";i:1;s:4:"date";s:10:"2013-05-23";} as in your example, this will return 2013-05-23. Then your query can be:
...
WHERE SUBSTRING_INDEX(SUBSTRING_INDEX(val, '"date";s:10:"', -1), '"', 1) BETWEEN 2013-01-01 AND 2013-05-23
Not pretty, but we can't expect pretty here :)
I think you should get date with substring with starting character -13 (13 from right side) and length of 10.
Something like this:
SUBSTR(field_name, -13, 10)
select * from postmeta where meta_key = 'your_meta_key' and meta_value REGEXP ('6')
I have a VARCHAR field in mysql with dates separated by commas. Like this:
"10/20/2011,10/21/2011,10/22/2011"
I need to use a WHERE condition like this:
where `date` > '10/10/2011'
So my question is basically how can i use (maybe) regex to retrieve the first date in my field (I only need the first) and apply the where condition to it?
This will get only the first part, before the comma , :
SUBSTRING_INDEX( varcharField, ',' , 1)
You then need to convert it into date format:
STR_TO_DATE( SUBSTRING_INDEX(varcharField, ',', 1), '%m/%d/%Y')
As you have already been told, storing a comma delimited list is a bad idea. But many times it's not within your job duties or abilities to restructure a table.
I think you should look up doing full-text indexes and matching. This will allow for searching within a field. Sadly only available on MyISAM tables.