I'm trying to select records from a table using SQL Developer based on an INT value (in one file) to a STR (in the other file) and I'm using a nested(?) query:
select *
from proddta.FQ584871
where tdrscn in (select rmrscn from proddta.f48310)
and tdan8 > 1 and tdeqhr > 0
and tddoco not in (select glsbl from proddta.f0911)
In this example the TDDOCO is an INT and the GLSBL is a STR. The match would be TDDOCO = 456123 to the GLSBL = 00456123.
Thank you for any assistance
I've tried to CAST and the STR and VARCHAR but they don't appear to work when I'm trying to convert the TDDOCO.
I've tried:
tddoco not in (select to_number(ltrim(glsbl,'0')) from proddta.f0911
and I've tried:
to_char(tddoco) not in (select glsbl from proddta.f0911)
and I get an invalid number error.
Related
I am trying to get the location of the last time an item was moved via sql function with the code below. Pretty basic, I'm just trying to grab the max date and time. If I run the sql as a regular select and hard code an item number in ATPRIM I get only one location. But if I create this function and then try to run it and then pass the function an item number I get every occurrence in the history file instead of just the MAX which would be the most recent. Also I have tried a Select Distinct and that did not do anything for me.
ATOGST = Item Location
ATPRIM = Item
ATDATE = Date
ATTIME = Time
CREATE FUNCTION ERPLXU/F#QAT1(AATPRIM VARCHAR(10))
RETURNS CHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
BEGIN DECLARE F#QAT1 CHAR(50) ;
SET F#QAT1 = ' ' ;
SELECT ATOGST
INTO F#QAT1 FROM ERPLXF/QAT as t1
WHERE ATPRIM = AATPRIM
AND ATDATE = (SELECT MAX(ATDATE) FROM ERPLXF/QAT AS T2
WHERE T2.ATPRIM = AATPRIM)
AND ATTIME = (SELECT MAX(ATTIME) FROM ERPLXF/QAT AS T3
WHERE T3.ATPRIM = AATPRIM
AND T3.ATDATE = T1.ATDATE) ;
RETURN F#QAT1 ;
END
EDIT:
So what I am trying to do is get that location and I got it to work on my iSeries in strsql but the problem is we use a web application called Web Object Wizard (WoW) which lets us use sql to make reports that are more user friendly. Below is what I was trying to get to work but the subquery in the select does not work in WoW so that is where I was trying create a function which we know works in other applications.
SELECT distinct t1.atprim, atdesc, dbtabl, dbdtin, dblife, dblpdp,
dbcost, dbbas, dbresv, dbyrdp, dbcurr,
(select atogst
from erplxf.qat as t2
where t1.atprim = t2.atprim and atdate = (select max(atdate) from
erplxf.qat as t3 where t2.atprim = t3.atprim) and attime = (select
max(attime) from erplxf.qat as t4 where t1.atprim = t4.atprim and
t1.atdate = t4.atdate)
) as #113_ToLoc
FROM erplxf.qat as t1 join erplxf.qdb on atassn = dbassn
where dbrcid = 'DB'
and dbcurr != 0
So instead of that subquery at the end of the select it would just be
, erplxu.f#qat1(atprim) as #113_ToLoc
Try this:
CREATE FUNCTION ERPLXU/F#QAT1(AATPRIM VARCHAR(10))
RETURNS CHAR(50)
LANGUAGE SQL
RETURN
SELECT ATOGST
FROM ERPLXF/QAT
WHERE ATPRIM = AATPRIM
ORDER BY ATDATE DESC, ATTIME DESC
FETCH FIRST 1 ROW ONLY;
I have an issue where I am trying to add a leading 0 to run an output.
SELECT
CASE
WHEN LEN(t.trans_time) = 5
THEN CONCAT(0, [trans_time])
ELSE T.[trans_time]
END AS [TransactionTime]
,RIGHT(CONCAT(0,trans_time),6) AS trans_time
,LEN(T.trans_Time)
,t.trans_time
Why does the case statement not return the leading 0 whereas using:
,RIGHT(CONCAT(0,trans_time),6) AS trans_time
Works no problem.
Case expression return only one type, whereas concat() would return different type & i am assuming trans_time has INT type.
So, you would need to do type conversations :
SELECT (CASE WHEN LEN(t.trans_time) = 5
THEN CONCAT(0, [trans_time])
ELSE CAST(T.[trans_time] AS VARCHAR(255))
END) AS [TransactionTime],
. . .
Another way to do this is to use the format function, wich is available from sql server 2012.
It not only makes the code more readable but will also perform better.
declare #t table (id int)
insert into #t values (90113), (90204), (90207), (90235), (90302), (90318), (90324)
select format(id, '000000') as TransactionTime from #t
this will return
TransactionTime
---------------
090113
090204
090207
090235
090302
090318
090324
I am having trouble with this code.
(SELECT r.SITE_ID, count(distinct r.NIIN)
FROM
(SELECT t.SITE_ID, t.NIIN, B.DT_INVT, B.DT_CRTD,CAST(
case when
B.DT_INVT = '0000000' then B.DT_CRTD + 2000
when B.DT_INVT IS NULL then B.DT_CRTD + 2000
when B.DT_INVT = ' ' then B.DT_CRTD + 2000
ELSE B.DT_INVT + 2000
End as INT) As DueDate
FROM
(SELECT A.SITE_ID, A.NIIN
FROM DDCNENVR.QBO A
WHERE NOT (A.RIC IN ('SMS', 'S9W', 'S9D'))
and A.SITE_ID in ('HECL', 'HECN')
GROUP BY A.SITE_ID, A.NIIN) as t
inner join DDCNENVR.QBS B on t.NIIN = B.NIIN and t.SITE_ID = B.SITE_ID) as r
where r.DueDate between 2018206 and 2019273
GROUP BY r.SITE_ID)
The same code works for some dbs but fails in others.
After exporting the data without using the duedate between statement, the column types according to excel are as follows:
B.Dt_inv and B.dt_created are text
Duedate exports as number
In Db2 V11.1.0.0 and above you could use a function such as this to check if a value can be CAST to a DECIMAL.
CREATE OR REPLACE FUNCTION IS_DECIMAL(s VARCHAR(255))
RETURNS SMALLINT
LANGUAGE SQL
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
RETURN
REGEXP_LIKE(s, '^\s*[+-]?\s*((\d+\.?\d*)|(\d*\.?\d+))\s*$')
Use it like this
SELECT DT_INVT FROM DDCNENVR.QBS WHERE IS_DECIMAL(DT_INVT) = 0
It is not perfect as it does not allow all formats (e.g. 1e3 is castable to DECIMAL, but this function will say it is not), but it will catch e.g. 12 34 as not castable to DECIMAL whereas the TRANSLATE function would not.
It also does not check the length of the string to ensure that it is not more than 31 decimal digits.
I have a problem with a function in db2
The function finds a record, and returns a number according to whether the first and second recorded by a user
The query within the function is this
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
And works perfect
I create the function with this code
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = POL)
) AS T WHERE POLLIFE = POL
)
The command runs executed properly
WARNING: 17:55:40 [CREATE - 0 row(s), 0.439 secs] Command processed.
No rows were affected
When I want execute the query a get a error
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY -- dummy has only one row
I get
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
I detect, when I remove the parameter the function works fine!
With this code
CREATE FUNCTION LIBWEB.BNOWPAPOL()
RETURNS DECIMAL(7,2)
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN (
SELECT
CASE
WHEN NUM IN (1,2) THEN 5
ELSE 2.58
END AS VAL
FROM (
select ROW_NUMBER() OVER() AS NUM ,s.POLLIFE
from LQD943DTA.CAQRTRML8 c
INNER JOIN LSMODXTA.SCSRET s ON c.MCCNTR = s.POLLIFE
WHERE s.NOEMP = ( SELECT NOEMP FROM LSMODDTA.LOLLM04 WHERE POLLIFE = '0010111003')
) AS T WHERE POLLIFE = '0010111003'
)
Why??
This statement:
SELECT LIBWEB.BNOWPAPOL('0010111003') FROM DATAS.DUMMY
causes this error:
[Error Code: -204, SQL State: 42704] [SQL0204] BNOWPAPOL in LIBWEB
type *N not found.
The parm value passed into the BNOWPAPOL() function is supplied as a quoted string with no definition (no CAST). The SELECT statement assumes that it's a VARCHAR value since different length strings might be given at any time and passes it to the server as a VARCHAR.
The original function definition says:
CREATE FUNCTION LIBWEB.BNOWPAPOL(POL CHAR)
The function signature is generated for a single-byte CHAR. (Function definitions can be overloaded to handle different inputs, and signatures are used to differentiate between function versions.)
Since a VARCHAR was passed from the client and only a CHAR function version was found by the server, the returned error fits. Changing the function definition or CASTing to a matching type can solve this kind of problem. (Note that a CHAR(1) parm could only correctly handle a single-character input if a value is CAST.)
Using the following code. Keep getting the following error
Select distinct
ship_L.ship_ID,
ship_L.Item_Num,
C.sku,
C.ob_oid,
c.Container
From (
Select distinct
ob_oid,
sku,
substr((ltrim(sys_Connect_By_Path(trim(to_Cont),' / '))),2,(length(ltrim(sys_Connect_By_Path(trim(to_Cont),' / '))))) as Container
From (
Select distinct
Ob_oid,
sku,
To_Cont,
row_number() Over (Partition by sku order by to_Cont) -1 as seq
From (
Select distinct
ob_oid,
sku,
To_Cont
from elite_76_w1.ITH_f
--Where ob_oid = '237472'
-- and sku = '64154'
)
)
Where connect_By_Isleaf = 1
Connect by seq = Prior seq +1 and sku = Prior sku
Start with seq = 1
) C
Left Join elite_76_D.ship_L
on ship_L.ship_id = C.ob_oid
and ship_L.item_num = C.sku
WHere C.ob_oid = '237472'
and C.sku = '64154'
Getting this error:
ORA-01489: result of string concatenation is too long 01489. 00000 - " result of string concatenation is too long" *Cause: String concatenatin result is more than the maximun size. * Action: Make sure that the result is lees than the maximum size. Vendor Code 1489Error at line 3.
Start with:
SKU Location
64154 A153945
64154 A153943
64154 A153947
64154 A153946
64154 A153944
Need:
64154 A153944 / A153945 / A153946 / A153947
Thank you for all the help,
David
ORA-01489: result of string concatenation is too long means that part of your SQL is generating a string longer than 4000 characters, which is the limit of a VARCHAR2 in SQL.
You can't truncate the string in SQL (e.g. with SUBSTR), because SQL needs to construct the string before it sends it to the SUBSTR function; and it bails because it exceeds the limit.
To solve this you'll probably need to create your own PL/SQL function, in which you have a greater length limit (32k) for VARCHAR2s.