I want to know if a date (Year-Month-Day) is between a day of the month (Month-Day).
For example, I want to know if 'April 2, 2013' is between 'April 2' and 'April 19'.
I can easily do this of the range has a year value. Howver, without a year, I need ideas how to do this.
Some examples of what I'm trying to achieve:
SELECT 1 WHERE '2013-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-04-02' BETWEEN '04-01' AND '04-19'; -- result is 1
SELECT 1 WHERE '2014-03-02' BETWEEN '04-01' AND '04-19'; -- result is 1
Thanks
You can make use of DATE_FORMAT().
Example
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '04-01' AND '04-19'
Edit
SELECT
*
FROM
table
WHERE
STR_TO_DATE(date, '%m-%d') BETWEEN '04-01' AND '04-19'
Following is work with oracle
select * from table_name where to_char(date_column, 'MM-DD') BETWEEN '08-01' AND '08-14'
The accepted answer is good, but it does not work if you want to select from December to February as #tony-brix said. You can extend that answer with two DATE_FORMAT functions. One goes from December to end of year, other goes from start of year to February.
SELECT
*
FROM
table
WHERE
DATE_FORMAT(date,'%m-%d') BETWEEN '12-01' AND '12-31'
OR
DATE_FORMAT(date,'%m-%d') BETWEEN '01-01' AND '02-01'
Related
I use mysql, and these time columns are not all in the same year.
I use this:
SELECT TIMESTAMPDIFF(QUARTER, DATE_FORMAT('2018-03-30', '%Y-%m-%d'), DATE_FORMAT('2018-09-30', '%Y-%m-%d'))
FROM DUAL;
It returns 2, but this:
SELECT TIMESTAMPDIFF(QUARTER, DATE_FORMAT('2018-03-31', '%Y-%m-%d'), DATE_FORMAT('2018-09-30', '%Y-%m-%d'))
FROM DUAL;
It returns 1.
Now I want the second returns 2 too, because March in quarter 1 and September in quarter 3, I just want it return 3-1=2.
I use this now:
SELECT (YEAR('2018-09-30') - YEAR('2018-03-31')) * 4 + (QUARTER('2018-09-30') - QUARTER('2018-03-31'))
FROM DUAL;
Is it the right way to do this? and how to do this right please?
You can achieve the correct answer just using the QUARTER() function:
SELECT QUARTER('2018-09-30') - QUARTER('2018-03-31') AS QuarterDifference
FROM DUAL;
QuarterDifference
-----------------
2
Here's a demo of this: SQL Fiddle
select * from table where 1 and DATE_FORMAT(dateColumn,"%d-%m") BETWEEN "01-09" and "30-09"
This query is returning all the data. I just want the data between these dates, without taking into account the year.
In the table dateColumn is yyyy-mm-dd.
You need to do the comparison as MM-DD, not DD-MM:
select *
from table
where DATE_FORMAT(dateColumn, '%m-%d') BETWEEN '09-01' and '09-30'
Or, for this case, you could just do:
where month(dateColumn) = 9
Try this:
SELECT *
FROM table
WHERE MONTH(dateColumn) = 9
You can check if the month is September
select * from table where DATE_FORMAT(dateColumn,"%m")
= '09
If you want rows from your table where the month of your date is 9 and the day of month is between 1 and 30, you can actually do this quite easy and more explicit:
SELECT
*
FROM
table t
WHERE
MONTH(t.date) = 9 and
DAY(t.date) BETWEEN 1 and 30
It's also easier to understand.
I made a SQL Statement and I want to use the date but without the time.
My Select is:
SELECT DATEPART(dw, [Order].PerformDate)
And my Group by is:
GROUP BY [Order].PerformDate
Now how can I ignore the time?
You can use CONVERT function of SQL
select datepart(dw, CONVERT(DATE,[Order].PerformDate))
GROUP BY CONVERT(DATE,[Order].PerformDate)
Cast datetime value to date:
select cast(`Order`.PerformDate as date) as PerformDate
GROUP BY says "I want one result row per ____". In your case one row per PerformDate. If PerformDate is a datetime, but you only want to have one result row per date without time, then you must extract the date part:
group by cast(performdate as date)
You also want to display the weekday with datepart(dw, performdate) but this is no longer possible, because PerformDate is no longer available. Only its date part is. So:
select datepart(dw, cast(performdate as date))
from ...
group by cast(performdate as date);
Another one method:
select date_format(`order`.PerformDate, '%Y%m%d')
...
group by 1
I need to query a database which will return the number of people subscribed to a particular service during that month.
I use the below query
select subscriberid, count(*) from ABC where updated time like '2013-05-%' ;
In this query I need to update the Updatedtime field to be 2013-06-% when the next month comes and then 07 when the next to next month comes. I want the query to be updated automatically when the next month comes instead of manually changing it every time.
Also note that I want the data for a particular month at a time, so please don't suggest grouping by month as an answer.
One way to do it
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE YEAR(updated_time) = YEAR(CURDATE())
AND MONTH(updated_time) = MONTH(CURDATE())
or
SELECT subscriberid, COUNT(*)
FROM ABC
WHERE updated_time BETWEEN ADDDATE(LAST_DAY(SUBDATE(CURDATE(), INTERVAL 1 MONTH)), 1)
AND LAST_DAY(CURDATE())
The following should work fine:
SELECT
subscriberid,
count(*)
from
ABC
where
updatedtime LIKE CONCAT(DATE_FORMAT(NOW(),'%Y-%m'), '-%')
I think you can use DATE_FORMAT function
SELECT subscriberid, count(*) as total
FROM ABC
WHERE DATE_FORMAT(updated_time, "%Y-%m") = "2013-05";
Use the following query:
SELECT subscribersid, updated, COUNT(*) from subscribers
WHERE YEAR(updated) = YEAR(NOW())
AND MONTH(updated) = MONTH(NOW())
You can see it working in this SQL Fiddle
Hope this helps
I am trying to nest a few queries but so far am getting back error 1242: Subquery returns more than 1 row. I want more than one row, as I am working on a number of records.
I have 2 tables. One has a commencement date stored in 3 columns; yr_comm, mth_comm, day_comm. The 2nd table has a period of service (in years) for a number of users which is expressed as an integer (2.71, 3.45, etc).
I need to take this start date (from table 1), and add on the period of service (from table 2) to obtain an end date, but I only need to display the year.
I have 2 queries which work just fine when seperate, they result in the required values, however I am having trouble combining the queries to get the desired end result.
Query 1: Concatenate the 3 commencement values into date format
SELECT concat_ws('-', yr_comm, mth_comm, day_comm) AS date_comm
FROM table 1
Query 2: Convert the integer yrs_service into days
SELECT format(yrs_served * 365, 0) AS days_served
FROM table 2
Query 3: Use date_add function to add the days service to the commencement date
SELECT date_add(date_comm, INTERVAL days_served DAY) AS date_left
Can anyone suggest how I can achieve the above? Many thanks in advance.
EDIT - Here is the full query I am working on:
SELECT prime_minister.pm_name, yr_comm, party, ADDDATE(
(SELECT CONCAT_WS('-', yr_comm, mth_comm, day_comm) FROM ministry), INTERVAL
(SELECT FORMAT(yrs_served * 365, 0) FROM prime_minister) YEAR) AS date_left
FROM ministry JOIN prime_minister USING (pm_name)
WHERE party NOT LIKE '%labor%'
AND prime_minister.pm_name = ministry.pm_name
ORDER BY pm_name;
you can use user variables
SET #date = CONCAT_WS('-', 2012,1,1); -- paste your query here
SET #toAdd = (SELECT MONTH(CURDATE())); -- paste your query here
SELECT DATE_ADD(#date, INTERVAL #toAdd DAY) AS date_left
SQLFiddle Demo
which is the same as
SET #date = CONCAT_WS('-', 2012,1,1); -- paste your query here
SET #toAdd = (SELECT MONTH(CURDATE())); -- paste your query here
SELECT #date + INTERVAL #toAdd DAY AS date_left
SQLFiddle Demo
or without using variable, which is more longer,
SELECT (CONCAT_WS('-', 2012,1,1)) + INTERVAL (SELECT MONTH(CURDATE())) DAY AS date_left
SQLFiddle Demo