ORA-00911 error with to_date function - sql

This request works as intended:
select dit_in.id data_item_in, dit_out.id data_item_out, alg.id algo_id
from algorithm_run arun
join algorithm_run_of arof on
arof.algorithm_run_id = arun.id
join algorithm_run_input arin on
arin.algorithm_run_id = arun.id
join data_item dit_in on
dit_in.id = arin.data_item_id
join algorithm alg on
alg.id = arof.algorithm_id
join algorithm_run_output arout on
arout.algorithm_run_id = arun.id
join data_item dit_out on
dit_out.id = arout.data_item_id
where alg.id in (182,183,143,162,125,222)
Unfortunately I get an error when I add at the end:
and arun.start_time >= to_date(’01/jun/2011’,’dd/mm/yyyy’)
and arun.start_time < to_date(’01/jul/2011’,’dd/mm/yyyy')
I'm using a web interface, the error message is:
warning: oci_execute() [function.oci-execute]: ORA-00911: invalid character in /opt/csw/apache2/share/htdocs/DAE/sites/default/modules/data_repository/data_repository.inc on line 117.
warning: oci_fetch_row() [function.oci-fetch-row]: ORA-24374: define not done before fetch or execute and fetch in /opt/csw/apache2/share/htdocs/DAE/sites/default/modules/daedatabase/daedatabase_db.inc on line 852.

and arun.start_time < to_date(’01/jul/2011’,’dd/mm/yyyy')
Do I see two different types of quote characters around that last bit? A single quote and a backquote? Or is that just a cut/paste or translation problem?

Try this:
and arun.start_time >= to_date(’01/06/2011’,’dd/mm/yyyy’)
and arun.start_time < to_date(’01/07/2011’,’dd/mm/yyyy’)
or
and arun.start_time >= to_date(’01/jun/2011’,’dd/mon/yyyy’)
and arun.start_time < to_date(’01/jul/2011’,’dd/mon/yyyy’)
The problem is that your date string (01/jun/2011) doesn't match the format specifier (dd/mm/yyyy). You need to either change your date or the specifier, as the above examples show.

As Phil points out, your strings are wrapped in two different quotes marks. It looks like you're mainly use fancy quote marks, from a word processor. This is a problem, because Oracle is expecting plain ASCII apostrophes (ASCII 39).
It would certainly explain why you're getting an ORA-00911 error.
To fix this, you simply need to replace all the ’ with ' .
To avoid it in the future you should use a text editor or IDE when writing code.

Related

Raw query doesn't accept where condition with date

I have this Raw query, but when I run it I get a sql syntax error.
here is the date params i use :
$last_week = Carbon::now()->subdays(7);
$last_2_week = Carbon::now()->subdays(14);
here is the query:
DB::raw("(SELECT SUM(vd.qty_available) FROM products AS P
JOIN variations AS V ON V.product_id=P.id
JOIN variation_location_details AS VD ON VD.variation_id=V.id
JOIN transaction_sell_lines as ts ON ts.variation_id=v.id
JOIN transactions as t ON ts.transaction_id=t.id
WHERE t.transaction_date>=$last_2_week) AS last_2_week_quantity"),
error :SQLSTATE[42000]: Syntax error or access violation: 1064
The actual SQL output will be similar to WHERE t.transaction_date >= 2020-01-01 00:00:00
which is invalid, you should wrap it in a single quotes.
something like
WHERE t.transaction_date>= '{$last_2_week}'
or better
WHERE t.transaction_date>= '{$last_2_week->toDateTimeString()}'
The toDateTimeString() forces the output to be in the correct format in this case.
or even better use the parameter binding
->whereRaw("... t.transaction_date>= ?", [$last_2_week->toDateTimeString()])
Put some space between the operand.
Instead of:
WHERE t.transaction_date>=$last_2_week) AS last_2_week_quantity")
Put:
WHERE t.transaction_date >= $last_2_week) AS last_2_week_quantity")

issue formatting into human time

SELECT
prefix_grade_items.itemname AS Course,
prefix_grade_items.grademax,
ROUND(prefix_grade_grades_history.finalgrade, 0)
AS finalgrade,
prefix_user.firstname,
prefix_user.lastname,
prefix_user.username,
prefix_grade_grades_history.timemodified
FROM
prefix_grade_grades_history
INNER JOIN prefix_user ON prefix_grade_grades_history.userid = prefix_user.id
INNER JOIN prefix_grade_items ON prefix_grade_grades_history.itemid =
prefix_grade_items.id
WHERE (prefix_grade_items.itemname IS NOT NULL)
AND (prefix_grade_items.itemtype = 'mod' OR prefix_grade_items.itemtype = 'manual')
AND (prefix_grade_items.itemmodule = 'quiz' OR prefix_grade_items.itemmodule IS NULL)
AND (prefix_grade_grades_history.timemodified IS NOT NULL)
AND (prefix_grade_grades_history.finalgrade > 0)
AND (prefix_user.deleted = 0)
ORDER BY course
Currently I am trying to polish this query. The problem I am having is using a UNIX Command to convert the time queried from timemodified into Human time. It comes out in epoch time. I have been attempting to use commands such as FROM_UNIXTIME(timestamp,'%a - %D %M %y %H:%i:%s') as timestamp. For reference this is a adhoc query to a moodle server contained in MariaDB. My desired result from the query is that nothing would change as far as the results we are getting, except that the time would be in a month/day/year format instead of the current format.
I have converted the timestamp into a custom date format using the below command in my select query.
DATE_FORMAT(FROM_UNIXTIME(`timestamp`), "%b-%d-%y")
As included in your question where you mention FROM_UNIXTIME(timestamp,'%a - %D %M %y %H:%i:%s'), it is indeed possible to include a second argument in order to specify the specific time/date format you wish to output converted from the UNIX timestamp.
That's the bit that looks like: '%a - %D %M %y %H:%i:%s' - this particular format string will give you an output that looks something like this: Fri - 24th January 20 14:17:09, which as you stated isn't quite what you were looking for, but we can fix that!
For example, the statement below will return the human-readable date (according to the value returned in the timestamp) in the form of month/day/year as you specified as the goal in your question, and would look similar to this: Jan/01/20
FROM_UNIXTIME(timestamp), '%b/%d/%y')
If you instead wish to use a 4 digit year you can substitute the lowercase %y for a capital %Y.
Additionally if a numeric month is instead preferred you can use %m in place of %b.
For a more comprehensive reference on the available specifiers that can be used to build up the format string, this page has a handy table
So putting it all together in the specific context of your original SQL query, using FROM_UNIXTIME to gain the human readable date (along with a suitable format string to specify the format of the output) may look something like this perhaps:
SELECT
prefix_grade_items.itemname AS Course,
prefix_grade_items.grademax,
ROUND(prefix_grade_grades_history.finalgrade, 0) AS finalgrade,
prefix_user.firstname,
prefix_user.lastname,
prefix_user.username,
FROM_UNIXTIME(prefix_grade_grades_history.timemodified, '%b/%d/%Y') AS grademodified
FROM
prefix_grade_grades_history
INNER JOIN prefix_user ON prefix_grade_grades_history.userid = prefix_user.id
INNER JOIN prefix_grade_items ON prefix_grade_grades_history.itemid = prefix_grade_items.id
WHERE (prefix_grade_items.itemname IS NOT NULL)
AND (prefix_grade_items.itemtype = 'mod' OR prefix_grade_items.itemtype = 'manual')
AND (prefix_grade_items.itemmodule = 'quiz' OR prefix_grade_items.itemmodule IS NULL)
AND (prefix_grade_grades_history.timemodified IS NOT NULL)
AND (prefix_grade_grades_history.finalgrade > 0)
AND (prefix_user.deleted = 0)
ORDER BY course
NOTE: I ended up specifying an alias for the timemodified column, calling it instead grademodified. This was done as without an alias the column name ends up getting a little busy :)
Hope that is helpful to you! :)

Error: unexpected symbol in " ON a.guest_id"

sqldf('Select a.guest_id,case when b.guest_id is not null then 'old' else 'new' end as tagging from JDUniqueGuestid as a
left join UniqueGuestidallsource b
ON a.guest_id = b.guest_id', drv="SQLite")
After running the above code getting below mentioned error, kindly help me and resolve the issue
Error: unexpected symbol in " ON a.guest_id"
You have single quotes around the whole query, but also single quotes within the query; it's not being parsed the way you intend it to.
Depending on the larger context, something like this might work:
"Select a.guest_id, case ... 'old' else 'new' ... ON a.guest_id = b.guest_id"
or you might need to escape the single quotes with something like this:
'Select a.guest_id, case ... \'old\' else \'new\' ... ON a.guest_id = b.guest_id'
It depends on the context in which that query string appears, and how it parses quoted strings.

Wildcard Parameter CR SQL Query

The following code I have written works find. Outputs what I expect.
select distinct
mi_tm.st_event_time,
mi_tm.st_location,
mi_tm.st_log_class,
mi_tm.st_qty,
mi_tm.st_reason,
mi_tm.st_reason_data,
mi_tm.st_sku_code,
mi_tm.st_ulid,
mi_tm.st_user_id,
x_prod.description,
x_supplier.supplier_name
from
mi_tm, x_prod,
x_igd, x_supplier
where
mi_tm.st_date >= {?Start} and
mi_tm.st_date <= {?End} and
mi_tm.st_sku_code = x_prod.prod_code (+) and
mi_tm.st_sku_code = x_igd.prod_code (+) and
x_igd.supplier_no = x_supplier.supplier_no (+) and
mi_tm.st_sku_code like '{?SKU}' and
mi_tm.st_log_class in ('TM_ADJUST_QTY', 'TM_CREATE', 'TM_DELETE')
However for this line
mi_tm.st_sku_code like '{?SKU}' and
I would like it to be able to expect a wildcard parameter, that will return all values run between the dates. Just querying a % seems to crash my report. and I have tried adding '%' around the parameter. But I either get an invalid number error, or SQL not ended properly. I would be grateful for any help on this issue.
Edit: Place this at the bottom
If {?SKU} = '*' then
mi_tm.st_sku_code like '*'
else mi_tm.st_sku_code like '{?SKU}'

How to fix error "Conversion failed when converting datetime from character string" in SQL Query?

The error that I found at the log is the one below.
'Illuminate\Database\QueryException' with message 'SQLSTATE[22007]:
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Conversion
failed when converting date and/or time from character string. (SQL:
SELECT COUNT(*) AS aggregate
FROM [mytable]
WHERE [mytable].[deleted_at] IS NULL
AND [created_at] BETWEEN '2015-09-30T00:00:00' AND '2015-09-30T23:59:59'
AND ((SELECT COUNT(*) FROM [mytable_translation]
WHERE [mytable_translation].[item_id] = [mytable].[id]) >= 1)
)'
in
wwwroot\myproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:625
On the database, the DataType is datetime and is not null
Based on marc_s's answer I tried to change the format that I'm sending to the database. So I tried without the T on and [created_at] between '2015-09-30 00:00:00' and '2015-09-30 23:59:59'.
In my local, I'm using mysql, and the code works just fine. If I test the query above on the SQL Server client, both (with and without the T) works too.
How can I fix this problem without create any changes on the database itself?
The PHP/Laravel code:
$items = $items->whereBetween($key, ["'".$value_aux."T00:00:00'", "'".$value_aux."T23:59:59'"]);
With #lad2025 help, I got it to work.
Based on the point of his comments on my question, I changed in the code part (Laravel/PHP in this case) the format that I was passing. (In reality, I "removed" the format it self, and just added fields to a variable before passing to the query. This way, I let the database decide the format that he wants)
Instead of
$itens = $itens->whereBetween($key, ["'".$value_aux."T00:00:00'", "'".$value_aux."T23:59:59'"]);
I changed the code to this:
$sta = $value_aux."T00:00:00";
$end = $value_aux."T23:59:59";
$itens = $itens->whereBetween($key, [$sta, $end]);