I am trying to run the query;
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail, PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE (PO_PurchaseOrderDetail.PurchaseOrderNo=PO_PurchaseOrderHeader.PurchaseOrderNo)
AND ***(PO_PurchaseOrderHeader.PurchaseOrderDate > '2013-12-31')***
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo, PO_PurchaseOrderDetail.PurchaseOrderNo
My problem is, that the date is stored in the YYYY-MM-DD format. I need to only show data after a certain date. So, because 2013-12-31 is a string rather than a number, the operator > doesn't work. Any ideas?
Try to CAST string as DATE in following:
P.S. if PurchaseOrderDate is not DATE datatype you could also CAST It: CAST(PO_PurchaseOrderHeader.PurchaseOrderDate AS DATE)
SELECT PO_PurchaseOrderDetail.ItemCodeDesc,
PO_PurchaseOrderDetail.QuantityOrdered,
PO_PurchaseOrderDetail.QuantityReceived,
PO_PurchaseOrderDetail.UnitCost,
PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo,
PO_PurchaseOrderHeader.PurchaseName,
PO_PurchaseOrderHeader.PurchaseOrderDate
FROM PO_PurchaseOrderDetail PO_PurchaseOrderDetail,
PO_PurchaseOrderHeader PO_PurchaseOrderHeader
WHERE PO_PurchaseOrderDetail.PurchaseOrderNo = PO_PurchaseOrderHeader.PurchaseOrderNo AND
PO_PurchaseOrderHeader.PurchaseOrderDate > CAST('2013-12-31' AS DATE)
ORDER BY PO_PurchaseOrderDetail.JT158_WTSalesOrderNo,
PO_PurchaseOrderDetail.PurchaseOrderNo
Related
I have stored this kind of format 2022-02-06 18:40:00 in my trans_reminder_date. I want to use only date in where condition but with this condition i am not able to fetch data
$today = date('Y-m-d');
SELECT * FROM sales_detail
WHERE trans_reminder_date = '".$today."'
AND trans_reminder_date != ''
ORDER BY sales_detail_id DESC";
If when your filter parameter is a string:
select * from sales_detail
where cast(trans_reminder_date as date) = cast('2020-03-22' as date)
if you want to use the current date for filtering then MySQL has a function that getting only the current date without time.
select * from sales_detail
where cast(trans_reminder_date as date) = curdate()
On MySQL for converting other types to another, you can use a cast
P.S.
Starting with MySQL 8.0.13 we have now an easiest way to create functional indexes. When you are using cast(updated_at as date) then DB will not use index for column updated_at. You must create a functional index for best performance.
The following query will give you all the result for the current date. By doing this there won't be any need to cast values.
SELECT
*
FROM
sales_detail
WHERE
trans_reminder_date >= curdate()
AND trans_reminder_date < curdate() + INTERVAL '1' DAY
ORDER BY
sales_detail_id DESC;
Using Cast function to change datetime type to date example
Cast(column_name as date)
Or
You using convert function change datetime type to date
CONVERT(column_name, date);
CONVERT(expression, datatype);
OR,
CONVERT(expression USING character_set);
character_set: It specifies the desired character set in which we want to be converted.
Data type : It specifies the desired data type in which we want to be converted.
Expression : It is a specified value going to be converted into another specific datatype.
How can a ISO datetime String timestamp be correctly parsed to time type column in mysql? I noticed the following:
select CAST('2013-09-05T10:10:02' as time) from mytable limit 1
Result incorrect:
00:20:13
select CAST(CAST('2013-09-05T10:10:02' as datetime) as time) from mytable limit 1
Result correct:
10:10:02
Why do I have to make a double CAST here to get the correct time? And more important: how is time parsing done property?
Because you have a string which you need to first cast it into date format.
If you cast it in time like below:
select CAST('2014-09-05T10:10:02' as time)
00:20:14
select CAST('2015-09-05T10:10:02' as time)
00:20:15
select CAST('2013' as time) --below casting string as time
00:20:13
If you monitor closely its treating it as string and getting year as time.
So you need to cast it datetime first then time.
select CAST(CAST('2013-09-05T10:10:02' as datetime) as time)
Basically when you try to convert something to a time that looks like an integer MySQL treats that as something in the form HHMMSS, so your 2013-09-05T10:10:02 becomes 00:20:13. To convert properly, the value needs to be a MySQL datetime, which you can do via CAST or you can use STR_TO_DATE to convert your date string to a MySQL date, then TIME to extract the time part of it:
SELECT TIME(STR_TO_DATE('2013-09-05T10:10:02', '%Y-%m-%dT%H:%i:%s'))
Output:
10:10:02
Demo on dbfiddle
i have a huge data with dates as string.
column name date1
datatype varchar
the stored data is in this format:14-Mar-2016 05:44:38pm
Now I have split only date from this string like this: 14-03-2016
By using this: DATE_FORMAT(STR_TO_DATE(gr.date1, '%d-%M-%Y'),'%d-%m-%Y')
Now I am trying to compare the date with this query:
SELECT * FROM
( SELECT date1,DATE_FORMAT(STR_TO_DATE(date1, '%d-%M-%Y'),'%d-%m-%Y') as dateFormatted
FROM `grabt` ) as mTbl WHERE mTbl.dateFormatted >= '19-01-2016'
AND mTbl.dateFormatted <= '25-01-2016'
but it is not working what could be the possible error.?
The timestamp string 14-Mar-2016 05:44:38pm can be converted to a datetime using the STR_TO_DATE() along with the format string %d-%b-%Y %r. We can then obtain only the date portion by wrapping that with DATE(). Have a look here for a demo to see that this works.
SELECT *
FROM
(
SELECT DATE(STR_TO_DATE(date1, '%d-%b-%Y %r')) AS dateFormatted
FROM grabt
) AS mTbl
WHERE mTbl.dateFormatted BETWEEN '2016-01-19' AND '2016-01-25'
As Gordon already pointed out, you should ideally be using date types not strings for your date calculations. And by the way, use a valid date string when comparing in your WHERE clause. YYYY-MM-DD is a valid format, e.g. 2016-01-19, but 19-01-2016 is not.
Learn to use the right types for columns. Perhaps you are stuck with someone else's really bad decision to store date/times as strings. Perhaps you cannot change that. But, within a query, use the right types!
SELECT mTbl.*,
LEFT(date1, 10) as FormattedDate -- Is this really necessary?
FROM (SELECT date1,
STR_TO_DATE(LEFT(date1, 10), '%d-%M-%Y') as thedate
FROM `grabt`
) mTbl
WHERE mTbl.thedate >= '2016-01-19' AND
mTbl.thedate <= '2016-01-25';
This will do the comparison as dates not as strings.
I have table that contain a string field with data.
The date is in this format: dd/mm/yyyy
I need to show the max date.
I try some thing but without success
Select max(to_date) from payment
Thanks
Use the STR_TO_DATE() function:
SELECT MAX(STR_TO_DATE(to_date, '%d/%m%Y')) FROM payment;
you must convert your string to a date,
because now you are doing the max over a string not a date
SELECT MAX(STR_TO_DATE(to_date, '%d/%m/%Y'))
FROM payment
I plan to make a selection of multiple database tables.
The problem is that I want the current date is equal to current date, only that it is of type varchar, I can't make a direct comparison with the CURDATE () ... Is there any way to compare the current date with the String type?
Code:
select records.date , records.hour, records.Temp, records.Hum, sensors.idSensor, sensors.idLocalization
from records, sensors
where records.idSensor=sensors.idSensor
and records.date = curdade() // Here is where I wanted to make the comparison date
order by records.date desc;
------------------EDIT (Solved)--------------------------
Thank you all for your help.
I switched CURDATE by curdade.
He made the comparison correctly.
Convert the curdate() to a string of the appropriate format. Something like:
select r.date, r.hour, r.Temp, r.Hum, s.idSensor, s.idLocalization
from records r join
sensors s
on r.idSensor = s.idSensor
where r.date = date_format(curdate(), '%Y-%m-%d') // Here is where I wanted to make the comparison date
order by r.date desc;
You need to use the appropriate format for your date column. I just used the "right" format, the ISO standard date format that is the best to use when representing a date as a string. You can read about the formats in the documentation. And, it is better to turn the current date into the right format than to convert the string to a date. By putting the function on the "constant" part of the comparison, MySQL can still take advantage of an index on date.
Try this. It may work. But this is a date-to-date data type comparison, not a string-to-string comparison.
select STR_TO_DATE(records.date, '%Y-%m-%d') AS date , records.hour, records.Temp, records.Hum, sensors.idSensor, sensors.idLocalization
from records, sensors
where records.idSensor=sensors.idSensor
and date = curdate() // Here is where I wanted to make the comparison date
order by date desc;