Using Alias in Like Query - sql

I have a Query "namePrefix + ' ' + nameFirstname + ' ' + nameSurname AS salName".
How can I use it in Store Procedure Like Query?

select a.salName from (
select namePrefix || ' ' || nameFirstname || ' ' || nameSurname salName
from table1) a
where a.salName like `%partialName%`;
For stored proc, if l_full_name_search represents the variable holding search string, you may write the query as:
select a.salName from (
select namePrefix || ' ' || nameFirstname || ' ' || nameSurname salName
from table1) a
where a.salName like '%'||l_full_name_search||'%';

Related

Formatting Spaces, punctuation marks in Oracle SQL [duplicate]

So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".

How to search in a SQL database table

I have a table with the following columns:
user_id
user_name
user_unit
user_last_name
user_first_name
user_email
I want to write a query that the user declares a string that contains a word/part of a word/user_name/user_id/full name/ext. and the query returns all rows the contains the string, sorted by most relevant.
Any offers?
You can concatenate the columns together and use like:
where (user_id || ' ' || user_name || ' ' || user_unit || ' ' ||
user_last_name || ' ' user_first_name || ' ' || user_email
) like '%' || :input_string || '%'
You haven't specified the database, so this uses the standard SQL concat operator.
var word = 'user Input';
var query = word+"/user_name/user_id/full name/ext"
user query variable to query data.

How to quickly see what columns in a table have data?

We are currently undertaking a testing phase which requires us to see if there is any data in each column for each table. Now, the route that is long and labour-intensive is:
SELECT COUNT(Col1), COUNT(Col2)...FROM TABLE
Is there any easier way to do this? We can go down this route by concatenating each column name from our data lineage document with the COUNT() function, but we have a lot of tables and a lot of columns in each table, making this a bit unfeasible.
Essentially we just need a count of records in each column for each table, without having to write long COUNT(Col) queries.
Thanks
This query will return accurate results if the table statistics were recently gathered with the default value for ESTIMATE_PERCENT:
SELECT utab.table_name
, tcol.column_name
, utab.num_rows
from user_tables utab,
user_tab_cols tcol
where utab.table_name = tcol.table_name
and utab.num_rows > 0
and utab.num_rows = tcol.num_nulls;
You could use a dynamic query to build the queries. This will generate all the queries.
SELECT 'SELECT COUNT(' || t.column_name || ' ) FROM ' || t.owner || '.' || t.table_name || ';' FROM dba_tab_columns t
You can generate all the select statements like so:
SELECT CASE WHEN column_id = 1 AND column_id_desc != 1 THEN 'SELECT ''' || LOWER(owner) || '.' || LOWER(table_name) || ''' table_name, ' || CHR(10) || 'COUNT(' || LOWER(column_name) || ') ' || SUBSTR(LOWER(column_name), 1, 26) || '_cnt,'
WHEN column_id = 1 AND column_id_desc = 1 THEN 'SELECT ''' || LOWER(owner) || '.' || LOWER(table_name) || ''' table_name, ' || CHR(10) || 'COUNT(' || LOWER(column_name) || ') ' || SUBSTR(LOWER(column_name), 1, 26) || '_cnt FROM ' || LOWER(owner) || '.' || LOWER(table_name) || ';'
WHEN column_id_desc = 1 THEN ' COUNT(' || LOWER(column_name) || ') ' || SUBSTR(LOWER(column_name), 1, 26) || '_cnt' || CHR(10) || 'FROM ' || LOWER(owner) || '.' || LOWER(table_name) || ';'
ELSE ' COUNT(' || LOWER(column_name) || ') ' || SUBSTR(LOWER(column_name), 1, 26) || '_cnt,'
END sql_text
FROM (SELECT owner,
table_name,
column_name,
column_id,
row_number() OVER (PARTITION BY owner, table_name ORDER BY column_id DESC) column_id_desc
FROM all_tab_columns)
WHERE <predicates to filter on the tables you're interested in>
ORDER BY owner,
table_name,
column_id;
This goes through all the tables you're interested in plus their columns and outputs text that will, when taken together, form a select statement for each table.
The text that is output in the sql_text column depends on whether the column in the list is the first or last (or both!); this way you get the full statement which queries each table once, rather than one per table and column.
You can then copy and paste the results and run that as a script.
It's can help you
SELECT
a.table_name,
a.column_name
FROM
ALL_TAB_COLUMNS a
WHERE owner = '<your user>'
AND a.SAMPLE_SIZE = a.NUM_NULLS

Oracle SQL: Alternative to aggregate large texts (when exceeding Listagg limit)

I have a simple Select query that aggregates one column containing large texts.
The following worked for me with small texts but I am now exceeding the Listagg character limit (4000 bytes ?).
I am very new to Oracle and couldn't find a proper solution for this online that I could apply here.
Can someone tell me the best alternative to this ?
My Query (simplified):
SELECT
m.S_ID AS SID
, LISTAGG
(
'ITEM NO.: ' || m.ITEM ||
' -nl-ARTICLE: ' || a.ARTICLE ||
' -nl-NET: ' || m.NET ||
' -nl-TAX: ' || NVL(m.TAX, 0) ||
' -nl-GROSS: ' || (m.NET + m.TAX),
' -nl--nl-'
) WITHIN GROUP (ORDER BY m.S_ID) AS Details
/* ... */
FROM
myTable m
/* ... */
Many thanks for any help with this,
Mike
One of possible method.
select xmlagg(xmlelement(xxx,'ITEM NO.: ' || m.ITEM ||
' -nl-ARTICLE: ' || a.ARTICLE ||
' -nl-NET: ' || m.NET ||
' -nl-TAX: ' || NVL(m.TAX, 0) ||
' -nl-GROSS: ' || (m.NET + m.TAX),
' -nl--nl-'||',<-separator').extract('//text()') order m.S_ID).getClobval() from mytable
group by ...
2nd method.
oracle allows to creat own aggregation function user defined aggregation function

Concatenation of two columns in Oracle [duplicate]

So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".