Oracle Date and Time Conversion - oracle

I have a problem with the SELECT statement below:
SELECT SO FROM ORDERS
UNION ALL
SELECT SO FROM QUOTES
When I run the query I get the error Fields must have same data types. I understand that there is data conflict between the field SO. In the Orders table it is of NVARCHAR2(50) whereas the Quotes table is composed of the Integer datatype. Is there any workaround to make a union with the field SO as shown in the above statement without changing the data type of the field SO?
Any help here is greatly appreciated.

It is just like the query shown below
select 1 from dual --integer datatype
union all
select '1' from dual --varchar2 datatype
In order to make this work ,you need to change the INTEGER DATATYPE to VARCHAR
select to_char(1) from dual --change the datatype to CHAR in Quotes table
union all
select '1' from dual -- this remain unchanged
Your final Query will be like
SELECT SO FROM ORDERS
UNION ALL
SELECT TO_CHAR(SO) FROM QUOTES

Related

Should order-by-clause be able to deduce implicit column names?

Question:
Can order by clause obtain column names when we specify asterisk * instead of column names in a select query?
The following code works, so the answer seems to be "yes":
select * from dual
order by dummy
The following code does not work, so the answer seems to be "no":
select * from dual
union all
select * from dual
order by dummy
Is this behaviour documented?
In the SQL Reference Manual, under "Sorting Query Results":
For compound queries containing set operators UNION, INTERSECT, MINUS, or UNION ALL, the ORDER BY clause must specify positions or aliases rather than explicit expressions. Also, the ORDER BY clause can appear only in the last component query. The ORDER BY clause orders all rows returned by the entire compound query.
So in your case
select * from dual
union all
select * from dual
order by 1
works as expected, as does
select dummy from dual
union all
select dummy from dual
order by dummy

Display data from some columns from multiple tables in oracle not working

I have multiple tables from which I want to just show data of 2-3 columns from each tables. So I tried using like below
SELECT RJ_MAINTENANCE_ZONE_NAME, RJ_R4G_STATE_NAME, RJ_SITE_NAME FROM ne.mv_structure#FACID147
union all
select model_name,serial_no from solar_equipment
union all
select model_name from solar_plugin;
But getting error as
ORA-01789: query block has incorrect number of result columns
If you want to UNION them, then all SELECT statements must have the same number of columns (that would be 3 in your case), and they must match in datatype.
For example:
select rj_maintenance_zone_name, rj_r4g_state_name, rj_site_name
from ne.mv_structure#FACID147
UNION ALL
select null , model_name , to_char(serial_no)
from solar_equipment
UNION ALL
select null , model_name , null
from solar_plugin

BULK COLLECT INTO a UNION query into a table of objects

How can I collect into a table of objects, the values produced by a query that has a union in it as shown below
Select customer_name
from customer
where customer_id = 'xxx'
BULK COLLECT INTO customer_obj
UNION
Select customer_name
from customer
where customer_name like '%adam%'
the constraints above are completely made up.
The bulk collect clause comes right after the (first) select clause, before the (first) from clause. You have it in the wrong place.
It is not clear why you are using a union (although that by itself will not result in an error). Perhaps as an unintended consequence, you will get a list of distinct names, because that is what union does (as opposed to union all).
Other than that, as has been pointed out in a Comment already, you don't need union - you need an or in the where clause. But even if you modify your query that way, you still must move bulk collect to its proper place.
Another option would be to put your UNION into an inline view. For example,
SELECT cust.customer_name
BULK COLLECT
INTO customer_obj
FROM (
SELECT customer_name
FROM customer
WHERE customer_id = 'xxx'
UNION
SELECT customer_name
FROM customer
WHERE customer_name LIKE '%adam%'
) cust

how to display identical values in a single column using EXCEL or ORACLE

Hello I need a formula in column ā€˜Cā€™ which calculates/adds the amount of B Column based on the column A ID. If there are several amounts in same ID it should add the total amount and would show the result in column ā€˜Cā€™ as a single row.
the output can be obtained from Oracle SQL query or an Excel formula.your help would be appreciated.
You can get the same output from Oracle itself, using analytical functions like below.
SUM() OVER(PARTITION BY ... ) -> This actually do the cumulative sum
WITH MYTABLE(ID,AMT) AS
(SELECT '2UF2', '500' FROM DUAL
UNION ALL
SELECT '2TC6', '300' FROM DUAL
UNION ALL
SELECT '2TC6', '200' FROM DUAL
UNION ALL
SELECT '2TC6', '800' FROM DUAL
)
SELECT ID,
AMT,
CASE ROW_NUMBER() OVER(PARTITION BY ID ORDER BY NULL)
WHEN 1
THEN SUM(AMT) OVER(PARTITION BY ID ORDER BY NULL)
END AS FORMULA
FROM MYTABLE
ORDER BY ID, FORMULA NULLS LAST;
SQL Fiddle Demo
You can use rollup in oracle
Select id,amt,sum (amt) nullFrom table nullGroup by rollup (id,amt)
For more details see below link
https://oracle-base.com/articles/misc/rollup-cube-grouping-functions-and-grouping-sets
In SQL you need an aggregation function, in this case sum, and a group by clause. The generic query should look like the following:
Select sum(b) from table group by a
I hope this helps.

Is Numeric in Oracle

I'm working in oracle 11g. I've a table with Number as the datatype. For development purpose we have created a staging table with varchar type. Initially data would be loaded in the staging table. We need find out the records that has only number in that column, since the data might contain the noises. Is there any way to find it.
You can select your data with a regexp_like :
SELECT *
FROM your_table t
WHERE REGEXP_LIKE (t.your_colonne, '^[0-9]+$');
The regexp_like function can be used to determine if a value consists of only digits. Here is an example:
with Your_table(your_column) as(
select '123456' from dual union all
select 'a123452' from dual union all
select '01456' from dual union all
select '1j6-d' from dual
)
select your_column
from your_table
where regexp_like(your_column, '^[[:digit:]]+$')
Result:
YOUR_COLUMN
--------------
123456
01456
SQL Fiddle Demo

Resources