How to search in a SQL database table - sql

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.

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".

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

Select all Table/View Names with each table Row Count in Teredata

I have been stuck into a question.
The question is I want to get all Table name with their Row Count from Teradata.
I have this query which gives me all View Name from a specific Schema.
I ] SELECT TableName FROM dbc.tables WHERE tablekind='V' AND databasename='SCHEMA' order by TableName;
& I have this query which gives me row count for a specific Table/View in Schema.
II ] SELECT COUNT(*) as RowsNum FROM SCHEMA.TABLE_NAME;
Now can anyone tell me what to do to get the result from Query I (TableName) and put it into QUERY II (TABLE_NAME)
You help will be appreciated.
Thanks in advance,
Vrinda
This is a SP to collect row counts from all tables within a database, it's very basic, no error checking etc.
It shows a cursor and dynamic SQL using dbc.SysExecSQL or EXECUTE IMMEDIATE:
CREATE SET TABLE RowCounts
(
DatabaseName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
TableName VARCHAR(30) CHARACTER SET LATIN NOT CASESPECIFIC,
RowCount BIGINT,
COllectTimeStamp TIMESTAMP(2))
PRIMARY INDEX ( DatabaseName ,TableName )
;
REPLACE PROCEDURE GetRowCounts(IN DBName VARCHAR(30))
BEGIN
DECLARE SqlTxt VARCHAR(500);
FOR cur AS
SELECT
TRIM(DatabaseName) AS DBName,
TRIM(TableName) AS TabName
FROM dbc.Tables
WHERE DatabaseName = :DBName
AND TableKind = 'T'
DO
SET SqlTxt =
'INSERT INTO RowCounts ' ||
'SELECT ' ||
'''' || cur.DBName || '''' || ',' ||
'''' || cur.TabName || '''' || ',' ||
'CAST(COUNT(*) AS BIGINT)' || ',' ||
'CURRENT_TIMESTAMP(2) ' ||
'FROM ' || cur.DBName ||
'.' || cur.TabName || ';';
--CALL dbc.sysexecsql(:SqlTxt);
EXECUTE IMMEDIATE sqlTxt;
END FOR;
END;
If you can't create a table or SP you might use a VOLATILE TABLE (as DrBailey suggested) and run the INSERTs returned by following query:
SELECT
'INSERT INTO RowCounts ' ||
'SELECT ' ||
'''' || DatabaseName || '''' || ',' ||
'''' || TableName || '''' || ',' ||
'CAST(COUNT(*) AS BIGINT)' || ',' ||
'CURRENT_TIMESTAMP(2) ' ||
'FROM ' || DatabaseName ||
'.' || TableName || ';'
FROM dbc.tablesV
WHERE tablekind='V'
AND databasename='schema'
ORDER BY TableName;
But a routine like this might already exist on your system, you might ask you DBA. If it dosn't have to be 100% accurate this info might also be extracted from collected statistics.
Use dnoeth's answer but instead use create "create volatile table" this will use your spool to create the table and will delete all data when your session is closed. You need no write access to use volatile tables.
Is there any way to find the row count of a table/view from system tables with out using count(). Count() took ages to run for big tables. dbc.statsv view only include tables as we are not able to collect the stat of a view.

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".

Using Alias in Like Query

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||'%';