Converting Oracle query to mySQL - mysql

I'm no expert in DB Admin, I need to edit a query from Oracle to SQL in my Middleware. I am getting the error
102: Incorrect syntax near '#P0'
Can anyone help please
below is the query
<assign to="/ProcessData/WERCS/SQL">
select F_PRODUCT,
F_PRODUCT_NAME,
SUPPLIER
from T_INTERPHASE
where MODIFIED_DATE >=
TO_DATE(?,&apos;yyyymmdd&apos;)
and AUTHGRP like &apos;%Mining Services%&apos;
</assign>
I have been asked to use CONVERT instead of DATE_TO - so I changed query to:
<assign to="/ProcessData/WERCS/SQL">
select F_PRODUCT,
F_PRODUCT_NAME,
SUPPLIER
from T_INTERPHASE
where MODIFIED_DATE >=
CONVERT(?,&apos;yyyymmdd&apos;)
and AUTHGRP like &apos;%Mining Services%&apos;
</assign>

The MySQL function for parsing a date with a specified format is STR_TO_DATE.
<assign to="/ProcessData/WERCS/SQL">
select F_PRODUCT,
F_PRODUCT_NAME,
SUPPLIER
from T_INTERPHASE
where MODIFIED_DATE >=
STR_TO_DATE(?,&apos;%Y%m%d&apos;)
and AUTHGRP like &apos;%Mining Services%&apos;
</assign>
However, the error referring to #P0 must be coming from something else.

Related

MySQL "Incorrect DATE value" in SELECT query inside WHERE clause

MySQL version: 8.0.23-0ubuntu0.20.04.1 - (Ubuntu)
When running sample query:
SELECT * FROM `redacted-tbl`
WHERE `redacted-col` = 'some-invalid-date'
ORDER BY `redacted-col` DESC LIMIT 0, 25
data structure: redacted-col DATE
I'm getting #1525 - Incorrect DATE value: 'some-invalid-date' error.
Now I understand that 'some-invalid-date' is definitely not a valid mysql date format. I understand that the error is expected behavior if it's an INSERT or UPDATE query.
But why do I get such error on SELECT query? Previous version of mysql didn't throw such error for SELECT query (only for INSERT/UPDATE).
Also, how do I turn off this error for SELECT-ing DATE column? Is there any mysql flags to disable such check?
Edit (added from my comment):
In my opinion, there are good reasons to allow comparison of non-valid-date-string with DATE columns:
querying with WHERE mydatecol > '2015' to get all date that is after '2015-01-01'
even better, I can just pass user inputted date as filter (sanitized and parameter-bind-ed of course): WHERE mydatecol > ?,
if user enter 2015 then it will become shorthand for user who cares only to get all records after 2015
if user enter 2015-04, then it will become shorthand for user who want records after 2015 month 04/April)
if user enter 2015-04-15 (normal/valid mysql date string), then app will display records after 2015 month 04/April date 15
without this "non-date-validated comparison", I would have to write more application code just to check if the user inputted valid date or not, e.g.:
if the input format is 2015 then I have to change it into 2015-01-01,
else if the input format is 2015-04 then I have to change it into 2015-04-01,
else if the input format is 2015-04-15 then it's valid,
else it's not valid and throw error (or just output current date/default date or just show 'no entry matched your search criteria')
[The text of this answer was originally written by forpas https://stackoverflow.com/users/10498828/forpas ]
You can cast mydatecol to a string to perform the comparison. An easy way to do it is with CONCAT():
WHERE CONCAT(mydatecol) > '2015'
or with cast:
WHERE CAST(redacted-col AS CHAR) > 2015

How do you convert a string to a date in mysql?

I must be doing something wrong. I'm trying to search the database on a OrderDate column which is stored as a string ex. "7/21/2016 9:13:31 PM"
AND I want to convert OrderDate to a unix timestamp for the query statement :
UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%e/%c/%Y %r')) >=
'1471496400' AND UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%e/%c/%Y
%r')) <= '1471669199'
There are plenty of results in the table that should match this. But I'm not getting any of them. What am I doing wrong?
I've read the manual and searched the internet and cannot figure out what I am doing so please don't tell me to RTM
This worked for me
UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%c/%e/%Y')) >= '$timestamp1'
AND UNIX_TIMESTAMP(STR_TO_DATE(OrderDate, '%c/%e/%Y')) <= '$timestamp2'

how to get the data using from date and to date in sql server?

i need to retreive data from database with the condition from date to to date using between query,
my query is,
select * from Master where Date between '01-08-2013' and '30-08-2013'
but it retreive all data from the table...
i need only data with in that date..
i tried another one like,
select * from PatientMaster where EntryDate >= '01-08-2013' and EntryDate<= '30-08-2013'
how its posible..
whats wrong with my query...
sorry im very bad in english...
thank you in advance...
A date string has the syntax YYYY-MM-DD and not DD-MM-YYYY
select * from Master
where `Date` between '2013-08-01' and '2013-08-30'
for that you can use
select * from Master where Date >='01-08-2013' and dateadd(dd,1,'30-08-2013')
You have to convert your strings to dates. This page shows you how to do it in mysql, which is what you have tagged. For sql server, which is in your subject line, use this page.
Then you do a slight modification of your 2nd attempt. Instead of
and EntryDate <= the end date
you want
and EntryDate < the day after the end date
That takes care of any time components. It might not matter in your case, but it's a good habit to get into.
You'll be looking for an query that works with your format? (dd-mm-yy)
CAST to the desired format!
http://www.w3schools.com/sql/func_convert.asp
105 = dd-mm-yy
SELECT * FROM Master
WHERE CONVERT(date, Date, 105) BETWEEN '01-08-13' and '30-08-13'
Be conscious with regards of the choice of data type for date Columns,
with or without time, day or year first etc. and please do not use varchar
for dates...
know that it CAN be confusing to call a date column for only Date...
be consistent with high/lower case.

Query with sql datepart

I have a table with two column as :
serial_number
1
2
3
dateOfAppoinement
2011-06-30 00:39:04.130
2011-06-30 00:40:01.130
2011-06-30 00:49:04.130
I want to get the highest serial_number of a day. I have to avoid the time part. I'm just using the date part.
Can anyone tell me how can I do this?
I updated my answer to use Convert like snkmchnb suggested. However in SQL Server 2008 there is a DATE datatype that is just the date portion of the year so you don't have to specify the 120 code. I tested this and it works perfectly and the SQL is pretty straight forward.
SELECT
MAX(serial_number),
CONVERT(DATE, dateOfAppointment) as [Day]
FROM
#TempSerialsByDate
GROUP BY
CONVERT(DATE, dateOfAppointment)
I think this select solves your problem, can't figure if it is the best way:
select dateadd(dd,0, datediff(dd,0, dateOfAppoinement ))
from your_table where serial_number = (select max(serial_number) form your_table)
How truncate date in sql server can be found here:
How can I truncate a datetime in SQL Server?

Help me to revise mySQL query

I am trying to check if a regulation's date reminder is today and regulation's date end not yet passed then I do not want it to display. The problem is that the query that I made isn't working in mysql. Can anyone help me to revise my query?
Here is my query:
$query="select * from t_regulation where dt_reminder >= '$today' and dt_ended ='$today'"
This is assuming that your dt_reminder columns type is DATETIME, and not some sort of timestap.
SELECT * FROM t_regulation WHERE DATE(dt_reminder) >= CURDATE() AND DATE(dt_ended) = CURDATE()
You can do many funky things with date functions;
Mysql date/time functions
Very frequently I run into the problem that my date variable is a string that is not properly formatted for the default date time stamp in Mysql.
Remember it should be 'yyyy-mm-dd' for this comparison.
Also, for since the 'date ended' has not yet passed, shouldn't it be:
$query="select * from t_regulation where dt_reminder >= '$today' and (dt_ended > '$today' or dt_ended is null)"