Compare date in serialized string MySQL Query - mysql

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

Related

MySQL get value from string with char_length()

I have a column that consists of details of an orderline named 'ConcatValue'. An example of a value in this column is:
573856014/100/M00558640/OrderQty12
I want to extract the order value which can be founded after 'OrderQty'. I thought I had a solution by executing the following statement: substr(ConcatValue,char_length(ConcatValue)-1,char_length(ConcatValue))
This results in only level the last 2 characters of the string from the column ConcatValue. For the ConcatValue mentioned above I will get the following result: '12'. Which is the desired result.
But when the orderline has an Order quantity below 10, for example in the following ConcatValue:573856014/100/M00558640/OrderQty3
I will get the following result: y3
My question: Is there a way to delete 'y' if a row has an y within the value? Or is there a way to replace the y with a 0? Or is there a way to only select the last digits from the ConcatValue string?
Use string functions.
With substring_index() you can get the last part of the string and with replace() remove 'OrderQty':
select replace(
substring_index(ConcatValue, '/', -1),
'OrderQty',
''
)
from tablename
Actually, the simplest method is simply:
select substring_index(ConcatValue, 'OrderQty', -1)

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.

SQL, filter pattern with data format on end of field value

in SQl code how can be filtered out values that match the following pattern:
some characters and after the last underscore ('_') has a date in the format DDMMYYY,
example
values
-----
hello01122015
hello_2000
22_text_01022015
hello_again_22012015
result:
22_text_01022015
hello_again_22012015
Regards
You can use a simple regular expression '_[0-9]{8}$' for this & check that the actual date is valid:
-- with PostgreSQL
select *
from t
where values ~ '_[0-9]{8}$' and
to_char(to_date(right(values, 8), 'DDMMYYYY'), 'DDMMYYYY') = right(values, 8);
-- with MySQL
select *
from t
where `values` regexp '_[0-9]{8}$' and
str_to_date(right(`values`, 8), '%d%m%Y') is not null;
Or, you can use more robust regular expression, like '_(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])[0-9]{4}$', but that won't be bullet-proof (this can accept some invalid date).
SQLFiddle: for PostgreSQL, for MySQL
Note: values is a reserved word in SQL, please avoid as a column name, if you can.

How do I get the type of a variable in MySQL?

I'm trying to change a table field that contains decimal numbers from varchar(255) to decimal(12,2). And before I do that, I'd like to find out if there is information that would get deleted in the process: are there any rows where this field contains something other than a decimal(12,2).
I'm stumped how to do this. Apparently there isn't a string function like is_numeric() in PHP. I already tried casting the field to decimal and then comparing it with the original string, but this returns TRUE even for obvious cases where it should not:
select ('abc' = convert('abc', decimal(12,2)));
returns 1
Any help? How do I find out if a string contains something other than a decimal in MySQL? Thanks.
Stupid me, I have to cast twice (to decimal and back to char), which makes it work:
select ('abc' = convert(convert('abc', decimal(12,2)), char(255)));
returns 0
Thanks.
If you want to examine if the strings are actually floating points numbers, you could also use a regular expression. The following regex can help :)
SELECT '31.23' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT '31' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 1
SELECT 'hey' REGEXP '^[[:digit:]]+([.period.][[:digit:]]+)?$'; # returns 0

Mysql convert VARCHAR to date using REGEX

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.