Trim null values in AS400 - sql

I am doing a join between two tables A and B on A.Item = B.Item. I am not getting the records as expected. After doing some investigations, I saw that all the items in table B contains nulls at the end of the item.
I would like to be able to do something like:
SELECT * FROM A INNER JOIN B ON TRIMNULL(A.ITEM) = TRIMNULL(B.ITEM);
Is there any such method in AS400 to trim the null values?

Take a look at the TRIM function in the manual. You can specify a character to trim.
If assuming you mean a hex x'00' when you say NULL. Then this should work:
SELECT *
FROM A INNER JOIN B
ON TRIM(TRAILING x'00' FROM A.ITEM)
= TRIM(TRAILING x'00' FROM B.ITEM);

Related

Count the number of rows returned in SQL ORACLE

I have a little problem, my query look like this
select count(A.toto)
from B
inner join C
on B.tata = C.tata
inner join A
on C.tutu = A.tutu
group by A.toto, A.zaza, A.zozo;
and my result look like this :
1
2
1
6
7
4
1
1
1
But I want only the number of rows, for this example, the value that I would like to have is 9.
But I don't know how I can have this value...
Thank you in advance !!
You can use count(distinct). Unfortunately, Oracle doesn't support count(distinct) with multiple arguments, so a typical method is just to concatenate the value together:
select count(distinct A.toto || ':' || A.zaza || ':' || A.zozo)
from B inner join
C
on B.tata = C.tata inner join
A
on C.tutu = A.tutu;
This assumes that. the column values don't have the separator character (or at least in such a way that the concatenation is the same for rows with different key values).
An alternative method is to use a subquery:
select count(*)
from (select 1
from B inner join
C
on B.tata = C.tata inner join
A
on C.tutu = A.tutu
group by A.toto, A.zaza, A.zozo
) abc

Remove leading zero in join

The tables are:
Table A: tag_num (nvarchar(6)) Table B: tag_num (nvarchar(6))
------------------------------ ------------------------------
883 00883
The query:
select * from A test1
inner join B test2 on test2.tag_num = test1.tag_num
return 0 rows due to the leading zeros in Table B.
How can I return the result
A.tag_num | B.tag_num
883 | 00883
even though Table B column contain leading zeros.
Which data type should I use?
You can cast the columns to int
select *
from A test1
inner join B test2
on cast(test2.tag_num as int) = cast(test1.tag_num as int)
Or better use int as data type in the table in the first place.
you should cast the string to int and join on them
SELECT *
FROM A AS test1 INNER JOIN B AS test2
ON CAST(test2.tag_num AS INT) = CAST(test1.tag_num AS INT)
You can keep datatype of NVARCHAR and use SUBSTRING to remove leading zero in following:
select * from A test1
inner join B test2 on test1.tag_num =
SUBSTRING(test2.tag_num, PATINDEX('%[^0]%', test2.tag_num+'.'), LEN(test2.tag_num))
You want to avoid doing a type conversion on both sides, if you want any hope of using an index. One method is to use like:
select *
from A inner join
B
on b.tag_num like '%' + a.tag_num;
However, this is too general, because the wildcards will match too many things. Another approach would be to use in:
select *
from A inner join
B
on b.tag_num in (a.tag_num, '0' + a.tag_num, '00' + a.tag_num)
This has some hope of using an index on b.tag_num. Getting a sargable expression fora.tag_num` is more challenging.
Just a shorten version of casting:
select * from A test1
inner join B test2 on 1*test2.tag_num = test1.tag_num
Multiplying by 1 will automatically cast result to int.

SQL Join / Union

I have two statements that I want to merge into one output.
Statement One:
select name from auxiliary_variable_inquiry
where inquiry_idbr_code = '063'
Returns the following list of names:
Name
------------
Affiliates
NetBookValue
Parents
Worldbase
Statement Two:
select name, value from auxiliary_variable_value
where inquiry_idbr_code = '063'
and ru_ref = 20120000008
and period = 200912
Returns the following:
Name Value
-------------------
Affiliates 112
NetBookValue 225.700
I would like to have an output like this:
Name Value
-------------------
Affiliates 112
NetBookValue 225.700
Parents 0
Worldbase 0
So basically, if the second query only returns 2 names and values, I'd still like to display the complete set of names from the first query, with no values. If all four values were returned by both queries, then all four would be displayed.
Sorry I must add, im using Ingres SQL so im unable to use the ISNULL function.
You can do a left join. This ensures that all records from the first table will stay included. Where value is null, no child record was found, and we use coalesce to display 0 in these cases.
select i.name, COALESCE(v.Value,0) from auxiliary_variable_inquiry i
left join auxiliary_variable_value v
on v.inquiry_idbr_code = i.inquiry_idbr_code
and v.ru_ref = 20120000008
and v.period = 200912
where i.inquiry_idbr_code = '063'
I'd recommend a self-JOIN using the LEFT OUTER JOIN syntax. Include your 'extra' conditions from the second query in the JOIN condition, while the first conditions stay in the WHERE, like this:
select a.name, CASE WHEN b.Value IS NULL THEN 0 ELSE b.Value END AS Value
from
auxiliary_variable_inquiry a
LEFT JOIN
auxiliary_variable_inquiry b ON
a.name = b.name and -- replace this with your real ID-based JOIN
a.inquiry_idbr_code = b.inquiry_idbr_code AND
b.ru_ref = 20120000008 AND
b.period = 200912
where a.inquiry_idbr_code = '063'
if i got right, you should use something like:
SELECT i.NAME,
v.NAME,
v.value
FROM auxiliary_variable_inquiry i
LEFT JOIN auxiliary_variable_value v
ON i.inquiry_idbr_code = v.inquiry_idbr_code
WHERE v.ru_ref = 20120000008
AND v.period = 200912

left join table on string like '%table.name%'

I am trying to left join products.table and manufacturer.table to implement the manufacturer into the products table
The only problem there is no id linking the tables, so I am trying something like this.
SELECT [kArtikel]
,[cArtNr]
,a.[cName]
,a.[cBeschreibung]
,H.cName
,[cKurzBeschreibung]
,[cHersteller]
,[cHAN]
FROM [db].[dbo].[tartikel] a
left join [db].[dbo].[tHersteller] h on a.cName Like '%H.Cname%'
where cHersteller is null
But with this query I am recieving only H.cName = Null
After 2 min execute with the same number of products, having cHersteller = Null
While searching I only find examples where join is on some id with where clause. But this is not the possible here.
You are querying whether a.cName contains the literal H.Cname, which is probably not what you were gunning for. If you want to have wildcards around H.Cname, you can use the + operator:
SELECT [kArtikel]
,[cArtNr]
,a.[cName]
,a.[cBeschreibung]
,H.cName
,[cKurzBeschreibung]
,[cHersteller]
,[cHAN]
FROM [db].[dbo].[tartikel] a
LEFT JOIN [db].[dbo].[tHersteller] h ON a.cName LIKE '%' + H.Cname + '%'
WHERE cHersteller IS NULL

Combine SQL query result with another query statement under one query? (Complicated)

I currently want to combine two SQL queries into one. This is a bit similar to SQL: Taking the result of of query and using it another - combine. Suppose there are two queries:
SQL Statement
1.) SELECT *
FROM (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY ) as colstate
2.) SELECT colstate
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
I want to use the result in query 1.) as the colstate (column statement) in query 2.). But:
What Have I tried is:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC
And it turns out to be Error: Scalar subquery is only allowed to return a single row, how should I replace the "=" into "IN"? Or is my statement totally wrong?
"Combine two queries into one" - that's not a good specs. Try to find out what exactly you want to get as a FLAT 2-dimensional table, think of nested SELECTs as of nested loops where the inner one can only set a single value for parent's row. Like this:
[Outer loop - parent row]
[Inner loop - children rows]
// all you can do here is change a single parent's field to anything
// like constant/sum/avg/topmost/ugly-subquery-returning-a-single-result
[/Inner loop]
[/Outer loop]
The error says that query you are using as column statement must return at most a single row.
It should probably look something like this:
SELECT (SELECT B.example1
FROM EXAMPLE1A A
INNER JOIN EXAMPLE1B B
ON A.ID = B.ID
WHERE A.ABC ='ABC'
AND A.SOME_COLUMN = E.SOMECOLUMN // retrieve only relevant data for this row
ORDER BY A.ORDER_BY )
FROM EXAMPLE_TABLE E
WHERE EFG LIKE '%'
AND BGTHAN >= '1'
AND SMTHAN <= '100'
ORDER BY ORDER_BY ASC