Good day.
I am executing a query and encountering:
ORA-01861 literal does not match format string error.
I executed this query and IT WORKED.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice in (75078741)
AND id_analytical_center in (100000002)
AND interface_date = '2013-06-30'
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND generated_actual_acc_doc
IN (select id_accounting_document
from gcacc_accounting_document
where document_status = 'DOCSTA0001');
My other query is written below which DID NOT WORK.
SELECT * FROM GCACC_OPERATION_DETAIL WHERE id_notice IN (75078741)
AND id_analytical_center in (100000002)
AND generic_client = 'someGenClient'
AND document_class = 'DOCCLA0001'
AND accounting_tag_identifier = 1
AND interface_date = '2013-06-30'
AND ind_pending_process = 1
AND operation_type
IN (select cod_develop from gcacc_operation_type where ind_operation = 'B');
This is really weird because the error happens in the date part but I am writing the same syntax for the date part. I maybe missing something silly here and fresher eyes are needed. Thanks in advance!
I don't know what the specific problem is, but assuming "interface_date" is a DATE type, it is bad practice to use literals in a query for a date. This makes the assumption that the default NLS_DATE_FORMAT agrees with your date literal. That will come back to bite you. To ensure that your date constraint is portable, change to this:
AND interface_date = to_date('2013-06-30','YYYY-MM-DD')
Related
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! :)
I am using Kayako Querying Language and trying to get the query to return all closed/resolved, closed/unresolved, and review tickets from a specified month. When I run the query, every ticket ever comes up, and it seems to ignore the date function I am using. What am I doing wrong?
SELECT COUNT(*) AS Total
FROM ‘Tickets'
WHERE ‘Tickets.Creation Date’ = month(June 2016) AND ‘Tickets.Status’ = ‘Closed’ OR ‘Tickets.Status’ = ‘Resolved’ OR ‘Tickets.Status’ = ‘Unresolved'
GROUP BY ‘Tickets.Status'
Thanks.
Replace the date comparison in your WHERE clause for this:
WHERE 'Tickets.Creation Date':Month = 6 and 'Tickets.Creation Date':Year = 2016
Be also careful with the quotes, you sometimes use ‘ and others '
I need to convert MSSQL query to Oracle but end up with SQL command not properly ended.
Here is MSSQL query
SELECT * FROM [dbo].[trade] AS [Extent1]
WHERE EXISTS (
SELECT 1 AS [C1] FROM
[dbo].[findetail] AS [Extent2]
INNER JOIN [dbo].[transact] AS [Extent3] ON [Extent2].[transact] = [Extent3].[transact]
WHERE [Extent1].[trade] = [Extent2].[trade]
AND 'ACCR' = [Extent3].[subledger]
AND [Extent3].[date] = '2016-03-18T00:00:00'
)
Converting it to Oracle SQL I end with this.
SELECT * FROM trade Extent1
WHERE EXISTS
(SELECT 1 C1 FROM findetail Extent2
JOIN transact Extent3
ON Extent2.transact=Extent3.transact
WHERE Extent1.trade=Extent2.trade
AND 'ACCR'=Extent3.subledger
AND Extent3.date='2016-03-18T00:00:00'
);
and receive error above.
Date formats are different in Oracle. Perhaps something like this:
SELECT *
FROM trade Extent1
WHERE EXISTS (SELECT 1
FROM findetail Extent2 JOIN
transact Extent3
ON Extent2.transact = Extent3.transact
WHERE Extent1.trade = Extent2.trade AND
Extent3.subledger = 'ACCR' AND
Extent3."date" = DATE '2016-03-18'
);
DATE is a reserved word so needs to the surrounded in double quotes and, I am assuming that it is of DATE data type so you will probably need to convert the string:
SELECT *
FROM trade t
WHERE EXISTS (
SELECT 1
FROM findetail f
JOIN transact r
ON f.transact = r.transact
WHERE t.trade = f.trade
AND 'ACCR' = r.subledger
AND r."DATE" = TO_DATE( '2016-03-18T00:00:00', 'YYYY-MM-DD"T"HH24:MI:SS' )
);
If you just use the string in r."DATE" = '2016-03-18T00:00:00' then Oracle will implicitly try to convert the string literal using the TO_DATE() function with the NLS_DATE_FORMAT session parameter as the format mask. If they match then it will work but this is a client variable so can be changed and then the query will break without the code having changed (and be a pain to debug). The simple answer is to ensure that you compare date value by either using TO_DATE() and specifying the format mask (as per the query above) or to use an ANSI date literal DATE '2016-03-18' (which is independent of the NLS settings).
UPDATE KopierenJPGs
SET(IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
WHERE IdImage = 30
And
WHERE Path = '\fantasy\6.jpg'
AND
WHERE Date = '22/02/2015 18:10:28'
I was taught to execute update statements like this, though not a single site gives this example.
It seems to me that my teacher confused an insert command with an update command?
Do I have it wrong here? Is there an actual error in this statement?
I am aware there are three where clauses, I need them for my program. I cannot update a fixed ID since the column names are variable, this is intentional.
Thank you.
I doubt your teacher made the confusion. Use the correct syntax for update:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '2015-02-22 18:08:28'
WHERE IdImage = 30 And Path = '\fantasy\6.jpg' and Date = '2015-02-22 18:10:28';
Note that I also changed the date formats to ISO standard formats. You should use these in code to avoid ambiguity and future problems.
If you want to insert a new row, then yes instead of update you need insert statement like:
INSERT INTO KopierenJPGs (IdImage, Path, Date)
VALUES (115,'\fantasy\5.jpg', '22/02/2015 18:08:28')
If you want to update an already existing row, you could do something like:
UPDATE KopierenJPGs
SET IdImage = 115,
Path = '\fantasy\5.jpg',
Date = '22/02/2015 18:08:28'
WHERE IdImage = 30
AND Path = '\fantasy\6.jpg'
AND Date = '22/02/2015 18:10:28'
I want to change the query to return multiply values in extra_fields, how can I change the regex? Also I don't understand what extra_fields is - is it a field? If so why it is not called with the table prefix like i.extra_fields?
SELECT i.*,
CASE WHEN i.modified = 0 THEN i.created ELSE i.modified END AS lastChanged,
c.name AS categoryname,
c.id AS categoryid,
c.alias AS categoryalias,
c.params AS categoryparams
FROM #__k2_items AS i
LEFT JOIN #__k2_categories AS c ON c.id = i.catid
WHERE i.published = 1
AND i.access IN(1,1)
AND i.trash = 0
AND c.published = 1
AND c.access IN(1,1)
AND c.trash = 0
AND (i.publish_up = '0000-00-00 00:00:00'
OR i.publish_up <= '2013-06-12 22:45:19'
)
AND (i.publish_down = '0000-00-00 00:00:00'
OR i.publish_down >= '2013-06-12 22:45:19'
)
AND extra_fields REGEXP BINARY '(.*{"id":"2","value":\["[^\"]*1[^\"]*","[^\"]*2[^\"]*","[^\"]*3[^\"]*"\]}.*)'
ORDER BY i.id DESC
The extra_fields is a column of the #__k2_items table. The table qualifier can be omitted, because it is not ambiguous in this query. The column is JSON encoded. That is a serialization format used to store information which is not searchable by design. Applying a RegExp may work one day, but fail another day, since there is no guarantee for id preceeding value (as in your example).
The right way
The right way to filter this is to ignore the extra_fields condition in the SQL query an evaluate in the resultset instead. Example:
$rows = $db->loadObjectList('id');
foreach ($rows as $id => $row) {
$extra_fields = json_decode($row->extra_fields);
if ($extra_fields->id != 2) {
unset($rows[$id]);
}
}
The short way
If you can't change the database layout (which is true for extensions you want to keep updateable), you must split the condition into two, because there is no guarantee for a certain order of the subfields. For some reason, one day value may occur before id. So change your query to
...
AND extra_fields LIKE '%"id":"2"%'
AND extra_fields REGEXP BINARY '"value":\[("[^\"]*[123][^\"]*",?)+\]'
Prepare an intermediate table to hold the contents of extra_fields. Each extra_fields field will be converted into a series of records. Then do a join.
Create a trigger and cronjob to keep the temp table in sync.
Another way is to write UDF in Perl that will decode the field, but AFAIK it is not indexable in mysql.
Using an external search engine is out of scope.
Ok, i didnt want to change the db strucure, i gost some help and changed the regex intoAND extra_fields REGEXP BINARY '(.*{"id":"2","value":\[("[^\"]*[123][^\"]*",?)+\]}.*)'
and i got the right resaults
Thanks