Strange behavior on Oracle CAST to NVARCHAR2 - oracle

I have the following query:
SELECT (CAST("META_SECT_ORDER" AS NVARCHAR2(3)) || '#' || "CODE") AS "SECTION"
FROM "BMAN_TP2"."LOADER_TEMPLATE_SECTIONS"
META_SECT_ORDER is obviously in integer, while CODE is a string.
It outputs 700 rows like these:
SECTION
-------
0#F01
0#I05
1#I05
2#I05
etc...
I know that CAST is not necessary but the query is composed by an underlying querybuilder that also outputs for MsSQL Server (where the CAST is needed) and Postgres.
The strange fact is that if I raise the NVARCHAR2 length I obtain different results. For example:
with values <= 80, I get the correct result (see above)
with a value of 81, I get 700 rows of 쥴ឋ醴ఀ퉶൐凨쓥昁菄ࢋ䖼譕貉ႋ䖼莀鐋
with a value of 82, I get 700 rows of ਖଆ
with odd values, in [83-127], I get 700 empty rows
with even values, in [84-128], I get 700 rows of ڢ
with values >= 129, I get ORA-03113: end-of-file on communication channel
What's that??
EDIT :
Actually, It seems that it depends only on the CAST, the concatenation is not relevant.

This looks like "Bug 9949330 - ORA-7445 or garbled data casting a NUMBER to NVARCHAR2". You've already discovered the official work-around, use values <= 80.
You should contact support to either download the patch or request one for your platform.
Here's an easier way to reproduce the issue without using any of your data. It still fails as of 11.2.0.3.
SQL> select cast(level as nvarchar2(130)) from dual connect by level <= 1;
CAST(LEVELASNVARCHAR2(130))
--------------------------------------------------------------------------------
ååååååååå┐┐┐ ┐┐ ┐┐A ┐┐ ┐┐A ┐┐ A ┐┐ ┐ ┐┐ ┐┐A

Related

How to restrict query result from multiple instances of overlapping date ranges in Django ORM

First off, I admit that I am not sure whether what I am trying to achieve is possible (or even logical). Still I am putting forth this query (and if nothing else, at least be told that I need to redesign my table structure / business logic).
In a table (myValueTable) I have the following records:
Item
article
from_date
to_date
myStock
1
Paper
01/04/2021
31/12/9999
100
2
Tray
12/04/2021
31/12/9999
12
3
Paper
28/04/2021
31/12/9999
150
4
Paper
06/05/2021
31/12/9999
130
As part of the underlying process, I am to find out the value (of field myStock) as on a particular date, say 30/04/2021 (assuming no inward / outward stock movement in the interim).
To that end, I have the following values:
varRefDate = 30/04/2021
varArticle = "Paper"
And my query goes something like this:
get_value = myValueTable.objects.filter(from_date__lte=varRefDate, to_date__gte=varRefDate).get(article=varArticle).myStock
which should translate to:
get_value = SELECT myStock FROM myValueTable WHERE varRefDate BETWEEN from_date AND to_date
But with this I am coming up with more than one result (actually THREE!).
How do I restrict the query result to get ONLY the 3rd instance i.e. the one with value "150" (for article = "paper")?
NOTE: The upper limit of date range (to_date) is being kept constant at 31/12/9999.
Edit
Solved it. In a round about manner. Instead of .get, resorted to generating values_list with fields from_date and myStock. Using the count of objects returned; appended a list with date difference between from_date and the ref date (which is 30/04/2021) and the value of field myStock, sorted (ascending) the generated list. The first tuple in the sorted list will have the least date difference and the corresponding myStock value and that will be the value I am searching for. Tested and works.

Max Val function not working as I expected it to work

I write this query I dont know why its not working for me can anyone suggest for me which is right
SELECT MAX(NVL(CPV.SR_NO,0)+1) INTO :CPV.SR_NO FROM CPV
WHERE VOUCHER_ID=4;
I have to bring MAX value I put 0 but it never bring 1 for first record after one record it worked properly mean if table is empty then first entry not give 1 after one record saved than its showed 1 even nvl is shown to null to 0 then + 1 please correct me
thanks
If there are no rows with VOUCHER_ID = 4, then you are taking MAX over zero rows, and that is always NULL - it doesn't matter what expression you are taking the MAX over. The NVL you used will only have effect if there are rows with VOUCHER_ID = 4, but in those rows you may have NULL in the SR_NO column. It won't help if there are no rows to begin with.
You could write the code like this **:
SELECT MAX(SR_NO) INTO :CPV.SR_NO FROM CPV WHERE VOUCHER_ID=4;
:CPV.SR_NO := NVL(:CPV.SR_NO, 0) + 1;
That is - apply the NVL (and also add 1) outside the SELECT statement.
With that said - what is the business problem you are trying to solve in this manner? It looks very much like an extremely bad approach, no matter what the problem you are trying to solve is.
** Note - I haven't seen qualified bind variable names before, like :CPV.SR_NO, but since the query works for you, I assume it's OK. EDIT - I just tried, and at least in Oracle 12.2 such names are invalid for bind variables; so I'm not sure how it was possible for your code to work as posted.
ANOTHER EDIT
The whole thing can be simplified further. We just need to pull the NVL out of MAX (and also the adding of 1):
SELECT NVL( MAX(SR_NO), 0) + 1 INTO :CPV.SR_NO FROM CPV WHERE VOUCHER_ID=4;

Handling zero as amount format in Oracle

I have an amount column which needs to be in 225,000.00 format, for which below is the query that i have written
Select TRIM(to_char(pen_amt,'999,999,999,999,999.99')) as PenAmount from transact;
Above query is giving correct result for all values except 0, for 0, its coming as .00, instead of it, it should come as 00.00. How to do it?
Add a 0 where you want the precision to start.
Select TRIM(to_char(pen_amt,'999,999,999,999,909.99')) as PenAmount from transact;

data comparison oracle (source to target)

I have a query below from source this is how Active flag for target is derived
select case when active_end_date is null then 'Y' else 'N' end
from csi_item_instances cii
where instance_id = <<INSTALL BASE ID>> --- (MP.INSTALL_BASE_ID)
I am comparing the active field value using the SQL below, is there better way to do this?
select * from stgdba.Stg_s_csi_item_instances cii, MDHDBA.M_CUSTOMER_PRODUCT mp
where cii.instance_id= MP.INSTALL_BASE_ID
and cii.active_end_date is null
and MP.ACTIVE_FLAG = 'N'
If that value is to be permanently calculated like that, you could do that in a view / computed column, which would make the logic a bit more permanent and not repeated all over the place.
(Stylistically, I would also try using ANSI joins a bit more.)

Symfony2/Doctrine DQL QueryException

So I'm attempting to do a query on a table that holds two foreign keys. This table basically sets a specific userID to be an "admin" of a specific zoneID in our application my DQL is this:
'SELECT z, zone FROM MLBPBeerBundle:TableZoneAdmins z JOIN z.zoneAdminsZID WHERE z.zoneAdminsPID = '.$userID .'AND zone.zoneId = z.zoneAdminsZID'
I am getting this error:
An exception has been thrown during the rendering of a template ("[Syntax Error] line 0, col 80: Error: Expected end of string, got 'z'") in "MLBPBeerBundle:Profile:index.html.twig" at line 10.
From looking at the query the part in question "line 0, col 80 is the beginning of z.zoneAdminsPID which means at least in my interpretation of the error that it expects the string to end right after the WHERE which makes no sense
What makes this even more confusing is I have already successfully used a similar query to get a team name out of our games table which has a foreign key to the teams id:
'SELECT g, team1 FROM MLBPBeerBundle:TableGame g JOIN g.gameWinnertid WHERE g.gameZoneid = '.$zoneId .'AND team1.id = g.gameWinnertid'
Thank you for any help you can provide this has left me stumped as to me I don't really see a difference in how to two queries operate other than the fact they are grabbing different data
I was able to fix this by not including the JOIN, Symfony is more automagical than I thought in the fact that
SELECT z FROM MLBPBeerBundle:TableZoneAdmins z WHERE z.zoneAdminsPID = '.$userID
From here the zoneAdminsZID was a proper zone Entity, now I believe this is lazily loading this entity aka not firing the query till I "derefence" the ZID but for us this works just fine

Resources