how to convert from one date format to another in mysql - mysql

I am using MySQL and I want to get all the dates in the previous week from a column, say xyz, which is of type date and has the format YYYY-MM-DD. However, when I use
select
*
from
tablename
where
xyz > date_sub(curdate(),INTERVAL 1 week);
I get dates which are not within the past 1 week. I get dates which are one month from now and some random dates.

You query is pulling all records in which xyz (your date column I assume) is newer than one week ago. This could in fact be future records. If you want to exclusively view things from the previous week, you need to use the between keyword.
select
*
from
tablename
where
xyz between date_sub(curdate(), INTERVAL 1 week) and curdate()

Related

Get last 3 months record from paid salary table where month and year is stored in separate column

I need help with the MySQL query. I have a table like this in MySQL
What I want is to get the last 3 months' records from the table. But I don't understand how to make this MySQL query.
I have tried to contact both columns and tried to generate a MySQL date to compare but it returns me null.
I also tried this query and it works for current data. But i am not sure if it is the only way to do this or is there any better way.
What I want is to get the last 3 months' records from the table.
I think you want a where clause like this:
date(concat_ws('-', salary_year, salary_month, 1)) >= (curdate() - interval (1 - day(curdate()) day) - interval 2 month
The key idea here is to convert the string to a date so it is handled correctly.
If you have a fixed date, then you would represent that as:
date(concat_ws('-', salary_year, salary_month, 1)) >= '2021-04--01'

Extract only month and day from date field mysql

I've a table in a db with some date field with format yyyy-mm-dd
I'm trying to perform a query that take just records with a interval of 3 month from today.
I've done like this
WHERE DATE_SUB(myTable.myField, INTERVAL 3 MONTH) = CURDATE()
and it works, but my second step is ignore years of my date field and from curdate().
I've tried EXTRACT or DATEFORMAT, but query doesn't work with those function.
How can I modify my query?
Thanks
The condition is wrong.
Try this instead:
...WHERE myTable.myField >= CURDATE() - INTERVAL 3 MONTH...
EDIT:
Based on your comment:
with my query i've got all record that have in dateField this date
'2016-12-07' (curdate() is today '2016-09-07') and it's fine. but i
want that query gives me also date that have 12 on month and 07 on
day, ignoring year. Eg. if i have '2016-12-07' and '2014-12-07', my
query must give me both records. it's a query that will run every day
...WHERE DATE_FORMAT(myTable.myField,'%m-%d') =
DATE_FORMAT((CURDATE() + INTERVAL 3 MONTH),'%m-%d')...
Use below condition which will find records with a interval of 3 month from today.
WHERE myTable.myField = DATE_SUB(CURDATE(), INTERVAL 3 MONTH)

Efficient SELECT query to find records within a month

I have a MySQL DB table with multiple date type fields. I need to do different SELECT queries on this table but I am not sure which way is the best to find records from the same month.
I know I can do the following:
SELECT *
FROM table
WHERE MONTH(somedate) = 5
AND YEAR(somedate) = 2015
But I keep reading that isn't efficient and that I should go with using actual dates, i.e.
SELECT *
FROM table
WHERE somedate BETWEEN '2015-05-01' AND '2015-05-31'
However, all I would have is the month and the year as variables coming in from PHP. How do I easily and quickly calculate the last day of the month if I go with second option?
Don't calculate the last day of the month. Calculate the first day of the next month instead.
Your query can be like this
WHERE t.mydatetimecol >= '2015-05-01'
AND t.mydatetimecol < '2015-05-01' + INTERVAL 1 MONTH
Note that we're doing a less than comparison, not a "less than or equal to"... this is very convenient for comparing TIMESTAMP and DATETIME columns, which can include a time portion.
Note that a BETWEEN comparison is a "less than or equal to". To get a comparison equivalent to the query above, we'd need to do
WHERE t.mydatetimecol
BETWEEN '2015-05-01' AND '2015-05-01' + INTERVAL 1 MONTH + INTERVAL -1 SECOND
(This assumes that the resolution of DATETIME and TIMESTAMP is down to a second. In other databases, such as SQL Server, the resolution is finer than a second, so there we'd have the potential of missing a row with value of '2015-05-31 23:59:59.997'. We don't have a problem like that with the less than the first day of the next month comparison... < '2015-06-01'
No need to do the month or date math yourself, let MySQL do it for you. If you muck with adding 1 to the month, you have to handle the rollover from December to January, and increment the year. MySQL has all that already builtin.
date('t', strtotime("$year-$month-01")) will give days in the month

MySQL date interval with specific date format

I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.

how to find the bigger date among two dates in a week by using mysql workbench

i have two dates in the same week i want to know which is the highest date among those two dates
for example 3/24/2014 and 3/27/2014 are the two dates in same week i want to know which is the bigger date among these two i.e 3/27/2014
what function should i use to get this result.
i tried with some of the question and answers in this link it did not work out
MySQL Query to select data from last week?
Might be you want this
SELECT max(date) FROM table_name
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY