In my db I have table 'leads' with column 'datum_event' (varchar). I need get rows between 'from' to 'to' datum_event. For example, from 30.11.2018 to 31.03.2019. I use str_to_date for convert varchar to date for compare.
My sql:
SELECT DISTINCT(l.lead_id)
FROM leads as l
WHERE STR_TO_DATE(datum_event,'%d.%m.%y') >= STR_TO_DATE('30.11.2018','%d.%m.%y')
AND STR_TO_DATE(datum_event,'%d.%m.%y') <= STR_TO_DATE('31.03.2019','%d.%m.%y')
ORDER BY l.lead_id desc
But now i get empty result. It is wrong. There is row with datum_event '01.12.2018', but I can't see it in result.
I can see result if compare dates are in one year, for example, from 01.01.2019 to 31.03.2019, but if i compare from 31.12.2018 I can't see any result.
Please help me, how can i solve this? Thanks.
You can maybe use SQL BETWEEN condition.
SELECT DISTINCT(l.lead_id)
FROM leads as l
WHERE (STR_TO_DATE(datum_event,'%d.%m.%y') BETWEEN
STR_TO_DATE('30.11.2018','%d.%m.%y') AND STR_TO_DATE('31.03.2019','%d.%m.%y'))
ORDER BY l.lead_id desc`
select STR_TO_DATE('30.11.2018','%d.%m.%y') it will return 2020-11-30
but your need 2018-11-30 this date so you have to change like below
in format
select STR_TO_DATE('30.11.2018','%d.%m.%Y') this will `2018-11-30`
i have added date format as demolink
so in your query you need to correct your format
SELECT l.lead_id
FROM leads as l
WHERE STR_TO_DATE(datum_event,'%d.%m.%Y') >= STR_TO_DATE('30.11.2018','%d.%m.%Y')
AND STR_TO_DATE(datum_event,'%d.%m.%Y') <= STR_TO_DATE('31.03.2019','%d.%m.%Y')
ORDER BY l.lead_id desc
Related
I need to perform the following query in mysql.
SELECT
evaluationpart.id,
evaluationpart.creation,
evaluationpart.evaluationid,
evaluationpart.partid,
evaluation.horimeter,
personcompressorpart.hourcapacity,
evaluation.evaluationdate AS changedate,
evaluation.averageworkload,
#ed := DATEDIFF(curdate(), evaluation.evaluationdate) AS elapseddays,
#uh:= #ed * evaluation.averageworkload AS usedhours,
#htu:= personcompressorpart.hourcapacity - #uh AS hourstouse,
#nc:= curdate() + INTERVAL (#htu/evaluation.averageworkload) DAY AS nextchange
FROM evaluationpart
LEFT JOIN evaluation ON evaluation.id = evaluationpart.evaluationid
LEFT JOIN personcompressorpart ON personcompressorpart.id = evaluationpart.partid
ORDER BY #nc ASC
But the Order By is not working and I'm getting this result
Could anyone tell me why?
It seems that you are not using the column name in the ORDER BY clause.
If you want to order the query result by the column named 'nextchange', the ORDER BY clause should be ORDER BY nextchange ASC.
Here's the MySQL documentation on Sorting Rows: https://dev.mysql.com/doc/refman/8.0/en/sorting-rows.html
I hope this helps.
I want to fetch data between two range but in my database Date field as Text.
How Now fetch data between two range
$startdate = 01-01-2020
$enddate = 31-12-2020
and my database field name DATE as text datatype(format 01-12-2019)
Below is the query I am using
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (START_DATE BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC
Please help how to get data with text datattype with range. How to convert text to data .
Please help me
have you tried using STR_TO_DATE() function available in mysql ?
I think something like below should work.
SELECT m.id, m.centers, c.BUDGET_ANNUAL_AMOUNT
FROM Cost_centers m INNER JOIN ANNUAL_BUDGET_BUDGET_CENTER c
ON c.BUDGET_ID = 25
where (STR_TO_DATE(START_DATE, "%d-%m-%Y") BETWEEN '$startdate' AND '$enddate')
ORDER BY ID DESC
According to
My SQL Reference - DATE, DATETIME, and TIMESTAMP Types
MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format.
I would suggest trying this format.
There is also a worked example where variables are used in date queries on :
TutorialsPoint
What could be wrong with my sql query here , I'd like to retrieve data from both tables meeting a WHERE condition
SELECT *, UNIX_TIMESTAMP(i.sent_date) AS udate
FROM ibc_sent_history as i INNER JOIN
ibc_messages as u
ON i.msg_ids = u.id
WHERE (i.sent_date >= '02-02-2013' AND i.sent_date <= '02-02-2014')
ORDER BY i.sent_date
LIMIT 200
Assuming your ibc_sent_history.sent_date datatype is DATETIME, here's a way to refactor this query. (This will work even if the datatype is DATE). You need to change your date input string format from 02-02-2013 to the more standard '2014-02-02` (YYYY-MM-DD).
SELECT whatever, whatever
FROM ibc_sent_history AS i
INNER JOIN ibc_messages AS u ON i.msg_ids = u.id
WHERE i.sent_date >= '2013-02-02'
AND i.sent_date < '2014-02-02' + INTERVAL 1 DAY
ORDER BY i.sent_date DESC
LIMIT 200
I changed the ORDER BY to include DESC. This is to return the most recent items, not the oldest. If that's not what you need, take off the DESC.
I changed the date formatting.
I changed the end of your selection range to
i.sent_date < '2014-02-02` + INTERVAL 1 DAY
That's because
i.sent_date <= '2014-02-02`
will include items that occur precisely at midnight on 2-Feb-2014, but won't include any other items on that day. What you probably want are items that occurred up to but NOT including midnight on the next day.
I don't know MySQL very well, but in SQL Fiddle when I run:
CAST('2014-02-02' AS DATE)
I get a date, when I run
CAST('02-02-2014' AS DATE)
I get NULL, so seems like your date format is wrong.
Demo: SQL Fiddle
In this question, I have 2 query
1) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '12/31/2013'
This query give proper data from the table.
But in 2 query
2) SELECT * FROM order WHERE order-date BETWEEN '12/01/2013' AND '01/10/2014'
This query not display any date from table, how this not display any data, there is some year change problem in mysql server.
Please help me.
You have to convert string to date for comparing two dates otherwise it consider as string. For that you have to use STR_TO_DATE() function
Try this:
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2013-12-31'
SELECT *
FROM `order` o
WHERE STR_TO_DATE(o.orderDate, '%m/%d/%Y') BETWEEN '2013-12-01' AND '2014-01-10'
my table and fields are like these:
i must find $sy<year<$ey then it must filter only values by $sm<month<$em at last it must find $sd<day<$ed
i need to find records between dates for example like 2010/10/25 , 2010/10/10
at first i tried :
SELECT SUM(barname) allin,SUM(rooz) allhoghogh,user_id FROM work_result
WHERE (`year`>='$sy' and `month`>='$sm' and `day`>='$sd') and (`year`<='$ey' and `month`<='$em' and `day`<='$ed') group by user_id ;
but it cant find records for dates like e like 2010/10/25 , 2010/10/28
than i tried
SELECT * FROM work_result as t1 join work_result as t2 on t1.year<='$sy' and t2.year>='$ey' and t1.month<='$em' and t2.month>='$sm' and t1.day<='$ed' and t2.day>='$sd' WHERE 1 group by t1.wrid
this isnt usful in my case!
i need some thing like priority select first select all between years than month and than day!!
other way is convert mysql records to timestamp by year and month and day and compare it by input date but UNIX_TIMESTAMP('year-month-day 00:00:00') dont worked correct for me.
i used it like :
SELECT * FROM `work_result` WHERE UNIX_TIMESTAMP('year-month-day 00:00:00')>1238921453
If convert to timestamp didn't work for you what about use date_format to convert:
SELECT *
FROM `work_result`
WHERE date_format(concat(year,'-',month,'-',day), '%Y-%m-%d') >
DATE_FORMAT(FROM_UNIXTIME(`yourDateGoesHere`), '%Y-%m-%d')