How to find quarter stemp diff by sql please? - mysql

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

Related

sql date ... get full year behind?

i need help for a function ... what i want to do is get the quantity of year behind of a date chosen .... for example i write '12-12-2017' ... so it will back year behind 'til '12-12-2016' i guess ?? any help...
SELECT DATE_ADD('2017-12-12', INTERVAL -1 YEAR)
Most simple to use is
SELECT '2017-12-12' - INTERVAL 1 YEAR
demo http://rextester.com/SUM99725

how to use mysql extract year in where clause

i want to search on date field with year. i used Extract like this but it generates error.
SELECT * FROM (blog) WHERE EXTRACT(YEAR FROM blog_dt) = '2014'
you should do it like this:
SELECT * FROM (blog) WHERE EXTRACT(YEAR FROM blog_dt)= '2014' ;
or
SELECT * FROM (blog) WHERE YEAR(blog_dt)='2014';
It's much simpler:
SELECT * FROM (blog) WHERE YEAR(blog_dt) = 2014
See the manual:
YEAR
Returns the year for date, in the range 1000 to 9999, or 0 for the
“zero” date.
mysql> SELECT YEAR('1987-01-01');
-> 1987
You have an extra apostrophe after the EXTRACT() function, remove it and it should work (although #VMai's solution is much easier and more readable
You can use the Function DateFormat available in MYSQL.
select * FROM blog WHERE DATE_FORMAT(blog_dt,'%Y')='2014';
For more Date Related Functions visit URL

Query to check if date is between two month-day values

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'

sql query with changing month

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

Nested MySQL Query w/ concat and adddate

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