I ran this query in DBeaver
SELECT DLY.badge_nbr,
DLY.DIM_DT_ID,attribute_type
FROM FACT_MDM_DAILY_INT DLY
WHERE SCENARIO_TYPE = 'VOLTAGE'
AND ATTRIBUTE_TYPE = 'Phase_A_Average_RMS_Voltage'
AND DLY.dim_dt_id >= TO_DATE('2016-01-28','yyyy-mm-dd');
I get the error as QUERY [VIRTUAL] [ERROR]. Interestingly when I run the same query without date comparison in WHERE clause it works fine.
SELECT DLY.badge_nbr,
DLY.DIM_DT_ID,attribute_type
FROM FACT_MDM_DAILY_INT DLY
WHERE SCENARIO_TYPE = 'VOLTAGE'
AND ATTRIBUTE_TYPE = 'Phase_A_Average_RMS_Voltage';
The to_date() function in Denodo must have at least 2 parameters:
The date format of your string field (look at java SimpleDateFormat)
The string you want to convert to a date.
Thus, your parameters appear to be transposed, and you must use a capital M for month... since lower case m means minutes.
to_date('yyyy-MM-dd','2016-01-28')
Related
I need to retrieve rows from SQL database where a time-difference condition apply.
Basically I need to retrieve rows where date difference from Now() is < x minutes.
Column datetime has this format: 2022-12-05 15:01:43
I tried
SELECT * FROM copytrade WHERE DATEDIFF(minute, datetime, GETDATE() AS DateDiff) < 10
Using this select I get error "Incorrect parameter count in the call to native function 'DATEDIFF'
Is it possible in a single SQL line to achieve this select?
Thanks
Trying and failing to get a date-sub function to work in a node-red function. MySQL db.
Without any date_sub, this works fine (in a function)-
var options = { hour12: false };
var nowtime = new Date().toLocaleString("en-GB", options);
msg.topic = `SELECT COUNT(*) AS rowcount FROM \`node-red\`.\`tag_reads\` WHERE \`datetime\` < "${nowtime}"`;
return msg;
With the date_sub part the query fails (with a count of zero even though there are valid records, the query works fine in a conventional jsp)
msg.topic = `SELECT COUNT(*) AS rowcount FROM \`node-red\`.\`tag_reads\` WHERE \`datetime\` > date_sub( "${nowtime}", INTERVAL 90 MINUTE) `;
I suspect it is syntax, escape codes etc. The resulting sql string appears to show that datetime (from the table) is not being interpreted as a string (just 'datetime' appears rather than the contents of datetime which are, for example, 10/6/2022, 11:18:43), but, if this was the case then the first select statement would not be working either.
Thoughts appreciated
Ralph
it looks like in this statement
WHERE \`datetime\` < "${nowtime}" mysql is converting the string to datetime for you.
MySQL has the ability to compare two different dates written as a string expression.
from How to do date comparisons in MySQL
However, in this statement, date_sub( "${nowtime}", date_sub wants the argument to be of type date.
Syntax
DATE_SUB(date, INTERVAL value interval)
from MySQL DATE_SUB() Function
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
I work for a gun club and we are trying to find the total number of targets shot at during a specific year. The table contains totals from years 2000-2018 and I am trying to write a query that will look at the string for the date of the shoot which is in a format like this 2010-06-13 00:00:00.000 I just care about the year so I created this query:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate LIKE '2007-%%-%% 00:00:00.000'
If I run the query up to the AND clause it works and returns a total. However, I get a null value if I highlight the whole thing. I have tried variations of the string as well. Such as this '2007%' and this '2007-- %'
Not sure what I am missing. Any help is appreciated.
Don't convert to string to query for a year, use YEAR() function instead:
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND YEAR(ShootDate)=2007 -- MySQL
You could also use a range query
SELECT SUM(ShotAt) AS TotalTargets
FROM MemberShootsC
WHERE GunClubNumber = 210015 AND ShootDate BETWEEN '2007-01-01' AND '2007-12-01 23:59:59.999'
Note: The above assumes that you do not store dates as strings. The function to use depends on RDBMS. In MS SQL Server you would use DATEPART(year, ShootDate) = 2007
When I execute the mysql query in postgresql it is showing the error .I have searched in google about the error but they are not working for me.Please suggest me to solve.
below is the mysql query:
SELECT caller_id_number,
caller_id_name,
dialed_number,
count(*) NumberOfCalls,
round(sum(duration_seconds)/60,2) CallDurationMin
FROM kazoo_cdr_rpt
WHERE cdr_date_time > '20-08-2017'
AND cdr_date_time < 24-08-2017
AND length(caller_id_number) < 5
AND direction = "outbound"
GROUP BY dialed_number;
when I execute above query in postgres it is getting the error as
round(sum(durati...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
If your duration second is varchar, you'll have to convert it to a number:
SELECT caller_id_number,
caller_id_name,
dialed_number,
count(*) NumberOfCalls,
round(sum(CAST(duration_seconds as numeric))/60,2) CallDurationMin
FROM kazoo_cdr_rpt
WHERE cdr_date_time > '20-08-2017'
AND cdr_date_time < '24-08-2017'
AND length(caller_id_number) < 5
AND direction = "outbound"
GROUP BY caller_id_number, caller_id_name, dialed_number;