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.
Related
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()
I have a table with year data in a year datatype field, now I want to insert new data with date data and I want to convert the year from the old data to date with default month and day 1.1.YearFromOldData.
I'm looking for something like the function STR_TO_DATE but for the datatype year NOT VARCHAR NOT VARCHAR and I fail to find it. How would I do this?
I want to do this
SELECT YEAR_TO_DATE(myYearField, '%1/%1/%Y')
FROM myTable
Assuming that you have a varchar field named year_dt with old years, use the following query to get a date with default day and month
SELECT DATE(CONCAT(table.year_dt, '-01-01')) as 'date' FROM table
this will return date in default format i.e. YYYY-MM-DD
If you want the first day of the year, I think the easiest way is with makedate():
select makedate(year, 1)
You can get the timestamp to insert to the database using
timestamp = (year - 1970) / 31557600
STR_TO_DATE() is exactly what you need. Try this if you have four-digit years, in a string, an integer, or a YEAR column. It works for them all.
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
If you have two-digit years try this (lower case %y)
select str_to_date(CONCAT(year_column,'-01-01'), '%y-%m-%d')
This is cool because you can do all sorts of date arithmetic: for example
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
+ INTERVAL 1 QUARTER
- INTERVAL 1 DAY
will give you the last day of the first quarter of your year.
I have this query where I provide to-date & from date.
SELECT *
FROM sales
WHERE date between to-date AND from-date;
Now I want to execute this query with following parameters
to-date = Oct-2015
some-other-date = Oct-2015
That is I want records of the whole month.
How would I do that in a query where I have to and from dates provided it will work for both scenarios where months can be same and different as well.
Update:
dataType for column date is date
You can find the first day of the month containing any given timestamp with an expression like this. For example by using the timestamp NOW(), this finds the first day of the present month.
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW()))
That's handy, because then you can use an expression like
(DATE(NOW() - INTERVAL DAYOFMONTH(DATE(NOW())) - INTERVAL 1 MONTH
to find the beginning of the previous month if you like. All sorts of date arithmetic become available.
Therefore, you can use an expression like the following to find all records with item_date in the month before the present month.
WHERE item_date>=(DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))- INTERVAL 1 MONTH
AND item_date < (DATE(NOW()-INTERVAL DAYOFMONTH(DATE(NOW()))
Notice that we cast the end of a range of time as an inequality (<) to the moment just after then end of the range of time.
You may find this writeup useful. http://www.plumislandmedia.net/mysql/sql-reporting-time-intervals/
It's often useful to create a stored function called TRUNC_MONTH() to perform the conversion of the arbitrary timestamp to the first day of the month. It makes your SQL statements easier to read.
select * from sales
where from-date >= 1-Oct-2015
and to-date <= 1-Nov-2015
Update
select * from sales
where date >= from-date
and date <= to-date
Here is SQLFIDDLE
You Can get month from your both to and from dates and find records of that month
SELECT * FROM sales
WHERE MONTH('2002-01-03') AND MONTH('2002-01-3')
SqlFiddle of Using Month Function
i have three date columns --- dob, colldate ,encounterdate.
I want to do something like this :
if colldate is not null and not '0000-00-00' then subtract year,month from dob otherwise dob's year and month is counted.
then encounterdate column is not null and not '0000-00-00' then subtract it from previously subtracted date otherwise dob's year and month is counted.
is there any good way to do this with query.
basically i want to transfer this condition logic from java code to query.so i get result directly.
The coalesce() function returns the first of its parameters which isn't null. As soon as a nullvalue is involved in a calculation, the entire result is null. Therefore we just reverse the logic.
SELECT COALESCE(
dob - interval year(colldate) year - month(colldate) month - interval year(encounterdate) year - interval month(encounterdate) month,
dob - interval year(colldate) year - month(colldate) month,
dob
)
FROM your_table
I am trying to select records from a database only if they match today's date. The format for the date in the database is 2012-06-20 9:30:00 I am using the statement SELECT id FROMnewsreportsWHERE DATE(newsdate) = CURDATE() but it doesn't not return any records for today?
Screenshot of column with dates
http://img135.imageshack.us/img135/8399/2347f03df0394cd898c7fc5.png
DATE_FORMAT(NOW(),'%Y-%m-%d') = FORMAT_DATE(NOW(newsdate), '%Y-%m-%d')
Or better:
DATE(newsdate) = DATE(NOW())
The best way is to store the additional column with 2001-09-11 date format and compare this one
Example, thanks to #Conrad Frix
It looks like the curdate function is going to give you something different than the date format you've got in the database.
Take a look at the docs here
MySQL Date and Time Functions
It's supposed to return a date in this format:
2008-11-11
So, you could either search for a date range between curdate() and curdate() + INTERVAL 1 DAY (untested), or store the dates in the curdate() format.