Evrey row in my table has a date Y-m-d. Now I want to select only those rows that have a date from before a certain month and year independet of the day. So for example I want all rows with a date befor january 2014.
How would I do this in MySQL? Only considering the month does not work, because it will give me also rows with a month smaller, but a year after my year. E.g. i want rows with date smaller than 03/2013. It will give me also 02/2014.
Of course I could in php define first the first day of the month and than compare the full date. But this does no seem too great and when I want to compare dates after a certain month I have to know how many days a month has.
You can use the STR_TO_DATE function to convert to actual DATE type and then do a simple comparison to the cut-off date.
SELECT *
FROM Your_Table
WHERE STR_TO_DATE(Your_Date_Column,'%Y-%m-%d') < STR_TO_DATE('2013-03-01','%Y-%m-%d');
Related
I have a date column which is formatted like this: 28-15.
The format for this is the first number tells which week, and the second one which year.
I have tried using str_to_date(datecolumn, '%v-%y') with no good results.
It orders the list BUT its not in the correct order.
I also tried concatting the datecolumn to make the string appear like this:
01-28-15 (First is the day of the week) and using str_to_date(datecolumn, '%w-%v-%y), with no luck.
What am I doing wrong?
From MySql docs:
You cannot use format "%X%V" to convert a year-week string to a date
because the combination of a year and week does not uniquely identify
a year and month if the week crosses a month boundary. To convert a
year-week to a date, you should also specify the weekday:
SELECT STR_TO_DATE('200442 Monday', '%X%V %W');
For you case, you need to make assumptions about week day(for example monday) and century(for example 2000), then you can get date next way:
SELECT DATE_ADD(STR_TO_DATE(CONCAT(datecolumn, ' ', 'Monday'), '%V-%X %W'), INTERVAL 2000 YEAR)
I have following date:
$params['ExpirationMonth'];
$params['ExpirationYear'];
I need some suggestion regarding how can I store only month and year in mysql database. After some research I have found 3 ways to do it.
1) I can use 1st as the days and can store along with month and year.
2) Instead of using date I can use varchar datatype and can only store month and year in it.
3) I can store the two as two separate columns as integers. i.e. for month and year.
Which is the better way?. Need some suggestion. Thanks.
You can use a date column, keeping in mind that:
You could set the day portion to 1
The card is good for the entire month of the specified expiry date. 0512 means valid through 31 May 2012.
Advantages:
Sorting is straight forward (compared to storing as varchar)
Date operations are straight forward (compared to storing as varchar/two integer columns)
Data stays in one column (compared to storing as two integer columns)
Sample queries:
-- expires in 2012
WHERE expires >= '2012-01-01' AND expires < '2012-01-01' + INTERVAL 1 YEAR
-- expires in Dec 2012
WHERE expires >= '2012-12-01' AND expires < '2012-12-01' + INTERVAL 1 MONTH
1) I can use 1st as the days and can store along with month and year.
This option is better as if there is any calculation or sorting need
to be done, it will be useful and handy.
2) Instead of using date I can use varchar datatype and can only store month and year in it.
3) I can store the two as two separate columns as integers. i.e. for month and year.
For above two option are fine as it will take less space as compare to
date or timestamp. But you can do calucation or orderby with this. For
calculation you need to convert it date by "str_to_date" function
I hope this will help you in some way.
stored as UNIX. suddenly need to add something in the future, and no problem to get anything out of date
Expiration Month/Year from credit cards means that this card is valid until the end of this Month/Year.
Best way to store as a comparable date you should get the last day of this month
STR_TO_DATE( CONCAT( '01,', #Month, ',', #Year, '%d,%m,%Y )
+ INTERVAL 1 MONTH
- INTERVAL 1 DAY
You can always store it like YYYYDD as an integer:
So for May 2021 you can store this integer: 202105
I am trying to select all records in a table which have a date between the current date and 1 month ahead.
The date is stored like this DD-MM-YYYY
And the query I have tried:
SELECT * from tablename WHERE renewalDate BETWEEN DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')) AND DATE_ADD(DATE_FORMAT(CURDATE(),GET_FORMAT(DATE,'EUR')), INTERVAL 1 MONTH)
But this does not return the correct results.
Is the date stored in an actual date or datetime field? If it's in a char/varchar field, you won't be able to use the BETWEEN syntax, as mysql will just treat them as fixed strings.
i want to compare database field of date that has YYYY-MM-DD
format and i want to compare from MM-DD
actually i want to fetch records those have birthdays between month /selected week durations
and birthdate is stores as YYYY-MM-DD format in db table
how can i achieve this kindly help.
Look at this:
http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html
you can use CURDATE(), DATE_ADD() and DATE_SUB() to make date interval
Within your sql you could try something like the following:
select * from people where birthdate>='10/01/2010' AND birthdate<='30/01/2010';
if you want to get more specific with months and days then you can utilise the month(birthdate) and day(birthdate) functions in mysql;
select * from people where month(birthdate)=10;
would return all your people where the month for birthdate is October.
Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.
In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.
Now what i want to do is build an archive link that will display as such
July 2009
June 2009
March 2009
Feburary 2009
December 2008
Note the months on which there were no posts are not displayed.
Now as an initial thought i was thinking
Start with the last day of the current Month
And get the Value of the First day of the current Month
Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
Display or put the values in an Array
Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)
Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.
Will say in advance, date needs to be in mktime format
I would suggest using a database "native" time format, but it works with UNIX timestamps as well.
You can simply do:
SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;
Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.
If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:
date($myCustomFormat, strtotime("$date-01"));
You can use this query to get years
"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";
now use can use this query to get month of that year
$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";