MySQL REGEXP Y-m-d with multiple months - mysql

I'm storing some data in a field called published. I need to use a regular expression to match the format of Y-m-d. Y and d can be excluded, however i need to any value in array of 3 months like $months = ['01,'02','03]. i need to match any of the 3 months in my regular expression this is my regex so far:
$regex = '^([0-9]{4}\-'.$months.'\-[0-9]{2})$';
I'm unsure on what the syntax would be for the months. would it be like 01|02|03? i'm not very good with regular expressions yet.
Updated
I found the following regex which will match:
^([0-9]{4}\-{1}(05|01|03){1}\-{1}[0-9]{2})$ not sure if its compatible with mysql but i'm going to try and see what happens. http://regexr.com/3c440

Why don't you just use:
WHERE MONTH(`datefield`) IN(1, 2, 3)
It's much easier to work with datetime fields, as it's easier to lookup and parse the format into a readable format MySQL can use.
Searching ranges becomes easy:
WHERE `datefield` >= '2015-01-01' AND `datefield` < '2015-04-01'
The full list of functions can be found here, and you can use functions such as STR_TO_DATE to convert to a dateformat.

#FrankerZ answer works, however for our purposes the field can store any type of text, not just a Y-m-d formatted date. This query is intended to target only records with a pattern that matches Y-m-d format and all others are simply not cared about for this exercise.
I was able to accomplish it myself with:
SELECT * FROM test WHERE published REGEXP '^([0-9]{4}\-{1}(05|01|03){1}\-{1}[0-9]{2})$'
http://sqlfiddle.com/#!2/7e311/1

Related

questions about the datetime type values

I have several things which I want to discuss with you guys.
Since, they were just simple questions, so no dataset here.
Suppose I have a datetime type column which called started_date, the value in it was like:
yyyy-mm-dd hh:mm:ss. So, if I want to select some IDs which were larger than one specified day (let's say June/01/2017), can I just using
select ID, started_date
from table1
where started_date>"2017-06-01";
Does this work?
I tried some samples, and it worked indeed in the mysql. However, someone told me that I cannot compare the datetime column with string values without converting their format. And it confused me. Because I thought the value "2017-06-01" here was date type value, so it does not need convert. Or am I thinking wrong?
Another thing was about the double quote and single quote, I understand that the single quote was used for string values. However, in this case, when I used double quote to quote "2017-06-01", it works. So, does it mean the double quote can quote date values?
I am just asking, so any response is welcome.
Thanks.
Your query is fine. You are using a safe date/time format for the string. In other words, if you have to store the value as a string, then use that format.
I would write the code as:
where started_date >= '2017-06-01'
I see no reason to exclude midnight on 2017-06-01 (although you might have a reason). Second, single quotes are the standard delimiter for strings.
That said, you can store the value as a string.
As a best practice, I stay away from comparing time-stamps to date-stamps. In this case you can be explicit and truncate the start date. And yes, use single quotes instead.
where SUBSTR(started_date, 1, 10) > '2017-06-01'
To make sure it works you could just convert the date time to a string first and compare the two strings:
to_char(started_date,'YYYY-MM-DD') >= '2017-06-01'
The Strings will compare just fine in that format.

Modx revo does not compare date type TV in getResourses

I'm trying to compare date from TV with date from snippet ( i'm using &tvFilters) (or any other date string) but works only == and != operations? but i need to make >= and <=, what's wrong? examples say that it's must work, can it be some MYSQL problem? looks like it's compares like strings...
[[!getResources? &showHidden=`1` &processTVs=`1` &includeTVs=`1`
&tpl=`rotatorPost` &includeContent=`1` &parents=`5` &limit=`3`
&tvFilters=`Date>=2016-01-19 00:00:00` ]]
You are going to have to make sure both dates are converted to unix timestamps [the TV probably already is]
You will have to retrieve your TV "Date" either unprocessed [&processTVs=0] or set it's output type to numeric.
Your snippet will have to provide the date as a numeric type as well.
In any case, you will have to check to docs to make sure getResources will not treat either value as a stringor else:
&tvFilters=1453239348 >= 1453239399 is going to give you unexpected results.
though the docs look good in that respect:
New filter operators available in 1.4.2-pl Starting with release
1.4.2-pl of getResources, there are a number of new comparison operators for use when creating filter conditions. In addition, when
using many of these new operators, numeric comparison values are
automatically CAST TV values to numeric before comparison. Here is a
list of the valid operators:

Performance in rlike expression or alternate query?

I am doing a series of updates on some tables after I import them from tab-separated values. The data comes with dates in a format I do not like. I bring them in as strings, manipulate them so that they are in the same format as MySQL dates and then convert the column. Or sometimes not, but I want them to be like MySQL dates even if they are strings.
They start out like '1/4/2013 12:00:00 AM' or '11/4/2012 2:37:45 PM'.
I turn these into '2013-01-04' (usually, since times are present even when the original schema clearly specifies dates only) and '2012-11-04 14:37:45'.
I am using rlike. And this does not use indexes? Wow. That sucks.
But already, for each column, I have to use 4 updates to handle the different cases ('1/7', '2/13', '11/2', '12/24'). If I did these using like, it might take 16 different updates for each column....
And, if I am seeing it right, I cannot even get positional parameters out of the rlike expression, yes? You know, the part of the expression wrapped in parentheses that becomes $1 or $2....
So, it seems as though it is going to be quicker to pre-process the tsv file with perl. Really? Wow. Again, this sucks.
Any other suggestions? I cannot have this taking 3 hours every time I need to pull in the data.
Recall the classic 1997 quote from Jamie Zawinski:
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
Now they have two problems.
Have you tried using STR_TO_DATE()? This is exactly for parsing nonstandard date/time strings into canonical datetime values.
If you try parsing with STR_TO_DATE() and the string doesn't match the expected format, the function returns NULL.
So you could try parsing in different formats, and return the first one that gives a non-null result.
UPDATE mytable
SET datecolumn = COALESCE(
STR_TO_DATE(stringcolumn, '%m/%d'),
STR_TO_DATE(stringcolumn, '%d/%m/%Y'),
...etc.
);
I can't tell what your different cases are. It might or might not be possible to cover all cases in one pass.
Another alternative is as you say, preprocess the raw data with Perl before you load it into MySQL. But even then, don't fight with regular expressions, use Date::Parse instead.

why this MySQL SELECT doesn't include the right dates?

The main problem is, that I have stored in database datetime , not the date (what I need). Ok never mind.
I have thousands of reports stored each day.
I need to LEFT by 10 my datetime_view (to cut the time) and everything's fine. Except this. I'm trying to figure out why do I have to put in the condition + one day from the future? Otherwise it won't search what I want.
SELECT
LEFT(datetime_view,10),
count(type)
FROM reports
WHERE
type IN (1,2,5)
AND
datetime_view>='2012-10-28'
AND
datetime_view<='2012-11-04'
group by LEFT(datetime_view,10);
You can see I must search from the future. Why??
It gives me an output from 28.10 to 3.11 ....
don't use string operations on date/time values. MySQL has a huge set of functions for date/time manipulation. Try
GROUP BY DATE(datetime_view)
instead, which will extract only the date portion of the datetime field. Your string operation is not y10k compliant. Using the date() function is.
As for your plus one day, consider how the comparisons are done: A plain date value, when used in date/time comparisons, has an implicit 00:00:00 time value attached to it, e.g. all dates have a time of "midnight".
i think it's better to use DATE(datetime_view) to cut the time instead of LEFT(datetime_view,10), also on the where condition:
DATE(datetime_view) <= '2012-11-03'

how to store a date in a mysql database?

If I have a date such as mm/dd/yyyy. How can I get mysql to actually store the date in that format. Is this possible or do have to store it in the form of yyyy/mm/dd and convert it later?
What I want to do is insert a date in the mm/dd/yyyy format but the database will not allow it. It wants yyyy/mm/dd
What is your reason for doing this? I can't see any reasonable use for it.
You cannot change the way MySQL stores dates, no. But you can of course format them when reading/writing them. You can do it in SQL query like this:
For example you can use STR_TO_DATE function to format the date when inserting it:
INSERT INTO mytable (mydate) VALUES (STR_TO_DATE('12/31/2009', '%m/%d/%Y'))
And vice versa:
SELECT DATE_FORMAT(mydate, '%m/%d/%Y') FROM mytable /* returns 12/31/2009 */
But as FactalizeR pointed out, it is not a good practice to do it in the query and it should be moved to script, like this (considering you are using PHP).
$date = '12/31/2009';
$date = date('Y-m-d', strtotime($date));
mysql_query("INSERT INTO mytable (mydate) VALUES ({$date})");
And vice versa
$date = mysql_result(mysql_query("SELECT mydate FROM mytable"), 0, 0);
$date = date('m/d/Y', strtotime($date)); //returns 12/31/2009
There is a built-in DATE type in MySQL. It allows to store a date. And later in your programming language you extract this date and convert to whatever format you like. Also, conversion can be done directly in MySQL via DATE_FORMAT(date,format) function.
MySQL DATE fields must store the date in YYYY-MM-DD (including dashes) format. If you attempt to store it any other way you will have problems with date comparisons and ordering.
Conversion later is a trivial task. Is there a compelling reason why you are trying to avoid doing this? What other technology are you using to talk to the MySQL database? (i.e. PHP, C# etc...)
I think you're mixing up what you are storing - the date itself - with how that date is subsequently referred to.
Why do you want to store in a particular format? Do you want to insert in that format? In which case you might be able to get away with it, depending on the localisation of your install, but otherwise convert - insert would be the way to do it (depending, of course, on how you're inserting).
Are you creating a SQL command from strings?