How can search a column by date format? - mysql

I'm trying to create a query according the day and month at the same time condition between 2 columns in a period of time like this:
Here is the demo
Here is the information:
CREATE TABLE clients
(date_birth date, [date_anniversary] date)
;
INSERT INTO clients
([date_birth], [date_anniversary])
VALUES
('1911-01-04',NULL ),('1921-01-05',NULL ),('1931-01-06',NULL ),('1941-01-07',NULL ),
('1951-01-08',NULL ),('1961-01-09',NULL ),('1971-01-10',NULL ),('1981-01-11',NULL ),
('1991-01-12',NULL ),(NULL, '1998-02-13'),(NULL, '1999-02-14'),(NULL, '2000-02-15'),
(NULL, '2001-02-16'),(NULL, '2002-02-17'),(NULL, '2003-02-18'),(NULL, '2004-02-19'),
(NULL, '2005-02-20'),(NULL, '2006-02-21');
Here is the condition:
since "04-01" until "13-02"
show all from clients where date_birth BETWEEN "04-01" AND "13-02" OR date_anniversary "04-01" AND "13-02"
This should be the output:
('1911-01-04',NULL ),
('1921-01-05',NULL ),
('1931-01-06',NULL ),
('1941-01-07',NULL ),
('1951-01-08',NULL ),
('1961-01-09',NULL ),
('1971-01-10',NULL ),
('1981-01-11',NULL ),
('1991-01-12',NULL ),
(NULL, '1998-02-13'),
I tried this but is just for month:
select date_birth from clients
where month(date_birth) BETWEEN '00' AND '01'
And also tried:
SET #date1 := 01-14;
SET #date2 := 02-20;
select date_birth from clients
where date_birth BETWEEN #date1 AND #date2
Please somebody can help me?

You can do so by using Mysql's month and day functions
SELECT date_birth ,date_anniversary
FROM clients
WHERE
(
MONTH(date_birth) >= 01 AND MONTH(date_birth) <= 01
AND DAY(date_birth) >= 01 AND DAY(date_birth) <= 31
)
OR
(
MONTH(date_anniversary) >= 01 AND MONTH(date_anniversary) <= 01
AND DAY(date_anniversary) >= 01 AND DAY(date_anniversary) <= 31
)
Fiddle Demo
using Mysql's date_format function just format your columns by day and month can compare with the provided range of same format,also provide your parameters as in this '%m-%d' format
select date_birth ,date_anniversary
from clients
where date_format(date_birth,'%m-%d') BETWEEN '01-01' AND '01-31'
or
date_format(date_anniversary,'%m-%d') BETWEEN '01-01' AND '01-31'
Fiddle demo 2
Storing values in variables
SET #start:='01-01';
SET #end:='01-31';
SELECT date_birth ,date_anniversary
FROM clients
WHERE DATE_FORMAT(date_birth,'%m-%d') BETWEEN #start AND #end
OR
DATE_FORMAT(date_anniversary,'%m-%d') BETWEEN #start AND #end
Fiddle demo 3

If you are using MySQL try this:
SELECT
date_birth
,date_anniversary
FROM clients
WHERE
DATE_FORMAT(date_birth, '%m-%d') BETWEEN DATE_FORMAT('1900-01-04', '%m-%d') AND DATE_FORMAT('1900-02-13', '%m-%d')
OR DATE_FORMAT(date_anniversary, '%m-%d') BETWEEN DATE_FORMAT('1900-01-04', '%m-%d') AND DATE_FORMAT('1900-02-13', '%m-%d')
If you are using MSSQL e posted into wrong subject try to google a DATE_FORMAT() function equivalent on it.

Related

always getting error trying to elaborate sql query into cakephp; how to convert this one?

If I use the Following code in PHPMyAdmin (SQL) statement, it works and I get a list of all Birthdays of the following 31 days. Here is my code:
SELECT
*
FROM
membres
WHERE
DATE_ADD(
date_de_naissance,
INTERVAL
YEAR(CURDATE()) -
YEAR(date_de_naissance) +
IF(DAYOFYEAR(CURDATE()) > DAYOFYEAR(date_de_naissance), 1, 0)
YEAR
)
BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 31 DAY)
AND
(
MONTH(date_de_naissance) <> MONTH(CURDATE())
OR
DAY(date_de_naissance) <> DAY(CURDATE())
)
How would it be in Cakephp? Please help
I generally find it's much easier to do the date math in CakePHP than in MySQLs, so I'd do something like this:
$birthday_members = $this->Membres->find()
->where([
'date_de_naissance >' => Cake\I18n\FrozenDate::now(),
'date_de_naissance <=' => Cake\I18n\FrozenDate::now()->addDays(31),
]);

How to select rows based on datetime

I have to select some rows where the date is today or higher and I can select only the registers where the time is ten minutes before the current time. My doubt is on how to do that with 'datetime' type. Down below is the query:
SELECT
GROUP_CONCAT(timee.nome_time
ORDER BY timee.nome_time
SEPARATOR ' X ') AS nome_time,
partida.id,
DATE_FORMAT(partida.data_hora, '%d/%m/%Y %H:%i') AS data_hora,
partida.tb_cotacao_id
FROM
tb_partida AS partida,
tb_time AS timee,
tb_partida_time AS partidaTime
WHERE
(partida.id = tb_partida_id
&& timee.id = tb_time_id)
AND (partida.flag_ativo = 1
AND partida.flag_cancelado <> 1
AND partida.flag_finalizado <> 1)
AND partida.tb_campeonato_id = 11
GROUP BY partida.id
I'd really appreciate any help. Thanks in advance.
Use the TIME_TO_SEC(TIMEDIFF(now(),<datetime_field>) to get the time difference and convert it to seconds. For greater than 10 minutes it will be something like this
SELECT ... FROM .. WHERE TIME_TO_SEC( TIMEDIFF( now() , tb_time )) > 600 ...
EDIT
To get the rows that has time 10 minutes more than current time, just reverse the parameters inside the TIMEDIFF. ie.,
SELECT ... FROM .. WHERE TIME_TO_SEC( TIMEDIFF(tb_time, now() )) > 600 ...

Set Date Format in Variable

Just wondering if anyone can help me setting a date variable for a report to run between certain dates every year without needing to be updated manually.
For example, I have the dates hard-coded below, but I was wondering if I could set a date format for when it goes into 2017 that the dates will change for the 2017 year. I presume there is a way to set a yearly date format I am just not sure how.
This is my hard-coded variables I set.
Put in the wrong Date Ranges changed as below.
set #start_date = '2016-01-02';
set #end_date = '2017-01-01';
Below is what I have in my where clause also.
and create_date between #start_date and #end_date
Your range covers all days in the year, except January 1. So you can simply check the day and month:
WHERE NOT (DAY(create_date) = 1 AND MONTH(create_date) = 1)
set #start_date = CAST(CAST(YEAR(getdate()) AS nvarchar(4)) + '0102' AS DATETIME);
to get end of current year
SET #end_date = DATEADD(day, -1 , DATEADD(month,13 - MONTH(getdate()), DATEADD(day,1 - DAY(getdate()),getdate())));
I assume you want the FULL "current year" based on "today"
MySQL
select *
from whatever
where ( create_date >= CONCAT(YEAR(CURDATE()),'-01-01')
and create_date < CONCAT(YEAR(CURDATE())+1,'-01-01')
)
Test it with:
SELECT
CONCAT(YEAR(CURDATE()),'-01-01') This_year_start
, CONCAT(YEAR(CURDATE())+1,'-01-01') Next_year_start
SQL Server (tsql)
select *
from whatever
where ( create_date >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)
and create_date < DATEADD(YEAR, DATEDIFF(YEAR, -1, GETDATE()), 0)
)
Test it with:
select
DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) , 0) This_year_start
, DATEADD(YEAR, DATEDIFF(YEAR, -1, GETDATE()), 0) Next_year_start
This_year_start Next_year_start
01.01.2016 00:00:00 01.01.2017 00:00:00
DATEDIFF(YEAR, 0, GETDATE()) calculates the number of years from the
base year
add that number of years to the base date (1900-01-01)
DATEDIFF(YEAR, -1, GETDATE()) calculates the number of years from
year before the base year (is a bigger number, by 1, than above)
add that number of years to the base date (1900-01-01)
Please avoid using between for date ranges. It is far safer to use a combination of >= and <
e.g.
where create_date >= '2016-01-01' and create_date < '2017-01-01'
With that approach, no matter what time precsion applies to the data in [create_date] you will get the precise range of information requested.
Here you go.
declare #s datetime, #e datetime
set #s=cast(cast(year(getdate()) as char(4))+'-01-02' as date)
set #e=cast(cast(year(getdate()) as char(4))+'-12-31' as date)
select #s,#e

How can show ages according specific years?

I'm trying to show ages according a specific rank of ages.
Here is the demo:
CREATE TABLE clients
(date_birth date, date_anniversary date);
INSERT INTO clients
(date_birth, date_anniversary)
VALUES
('1991-01-04',NULL ),
('1992-01-05',NULL ),
('1993-01-06',NULL ),
('1994-01-07',NULL ),
('1995-01-08',NULL ),
('1996-01-09',NULL ),
('1997-01-10',NULL ),
('1998-01-11',NULL ),
('1999-08-12',NULL ) ;
Here is the query,it shows all ages converted.
SET #start:='0';
SET #end:='22';
SELECT YEAR(CURDATE())- year(date_birth) AS ages
FROM clients
I'm trying to show ages between 0 AND 22, I tried this demo :
SET #start:='0';
SET #end:='22';
SELECT YEAR(CURDATE())- year(date_birth) AS ages
FROM clients
WHERE year(date_birth) >= #start AND year(date_birth) <= #end
Please somebody can help me or advice me?
Thanks in advance.
Your query should be
SELECT YEAR(CURDATE())- year(date_birth) AS ages
FROM clients
WHERE date_birth <= (curdate() - interval #start year)
and date_birth >= (curdate() - interval #end year)
This will make use of your index on date_birth as well (if any).
Change your query to be this:
SET #start:='0';
SET #end:='22';
SELECT YEAR(CURDATE())- year(date_birth) AS ages
FROM clients
WHERE YEAR(CURDATE())- year(date_birth) >= #start
AND YEAR(CURDATE())- year(date_birth) <= #end
I prefer to use the variables in the query that way you don't have to reset them
SELECT YEAR(CURDATE())- year(date_birth) AS ages
FROM clients
CROSS JOIN(SELECT #start := 0, #end := 22)t
HAVING ages BETWEEN #start AND #end
DEMO

How to replace a date with a standard date in SQL

Ok,
This is a little tricky, I am trying to replace the dates in a SQL Query results with a standard date, based on the month.
For example:
Any dates that are in July get 20140701
August gets 20140801
I could use a case statement:
Case
When Datepart(mm, TxnDate) = 1 and Datepart(yy, TxnDate) = 2014 then TxnDate = 20140101
etc...
but that could get very long as the database goes back 5 years and the result sets cover different periods then.
Any quick suggestions would be greatly appreciated.
Thanks,
declare #mydate datetime
select #mydate = GETDATE()
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
select #mydate = GETDATE() - 10
select cast(datepart(yy,#mydate) as varchar(4)) + RIGHT('0' + RTRIM(MONTH(#mydate)), 2) + '01'
should print 20140701 and 20140601
If you simply want to set every TxnDate to the first of its month, you can do this:
TxnDate = DATEADD(month, DATEDIFF(month, 0, TxnDate), 0)
If your requirement is more complicated than that, you will need to explain.
Try Something like this
SELECT REPLACE (CONVERT(VARCHAR(8), TxnDate, 112),SUBSTRING(CONVERT(VARCHAR(8), TxnDate, 112),7,8),'01') AS [YYYYMMDD]
hope this is what you are looking for