sql query to find a column value with specific text available - sql

Is their any other way to find a column values with the specified text available in it.
i know their is one way like,
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE COLUMN_NAME LIKE '%sample_text%'
and i tried
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE CONTAINS(COLUMN_NAME,'sample_text')
also but it requires table to be full-text indexed

You can use PATINDEX():
SELECT COLUMN_NAME FROM TABLE_NAME
WHERE PATINDEX('%sample_text%', COLUMN_NAME) != 0

Query 1
select 1 where patindex('%find_string%','find_stringfdasfsa')> 0
Query 2
select 1
where len(REPLACE('find_stringfdasfsa','find_string',''))<LEN('find_stringfdasfsa')
Query 3
select 1
where CHARINDEX('find_string','find_stringfdasfsa') > 0
Execution plan is same as Like. You will have to check the performance.

Related

Oracle LIKE pattern match

I have 10 records in a table. Out of these records, I need to perform the following 3 operations using a single Oracle query(the reason for saying single query is that this is part of an automation framework and I need to keep a single generic query)
Operation 1: get all the 10 records
Select * from table_name where col01 like ('%')
<<10 records fetched>>
Operation 2: get records that start with the string "Tivoli"
Select * from table_name where col01 like ('Tivoli%')
<<1 record fetched>>
Operation 3: get records that DOES NOT start with "Tivoli"
<<should give 9 records>>
I am Not able to write the query for operation 3 but I do not want to use a separate NOT LIKE clause as this would make me create a separate query altogether.
Then, you can use minus set operator :
Select * from table_name -- all records
minus
Select * from table_name where col01 like 'Tivoli%'
-- records for col01 column starts with Tivoli
If all types of Tivoli( such as TIVOLI.. or tIvOli or .. ) should be included case-insensitively, then you can consider the condition as
where lower(col01) like 'tivoli%'
You can use:
select *
from table_name
where col01 not like 'Tivoli%'
If col01 can be NULL, then you need to take that into account:
select *
from table_name
where col01 not like 'Tivoli%' or col01 is null;
Select * from table_name where not (col01 like ('Tivoli%') )
You are asking for a generic query that covers all three options. Use two variables for this and set them null when you don't want to use them:
Select *
from table_name
where (col01 like :must_match or :must_match is null)
and (col01 not like :must_not_match or col01 is null or :must_not_match is null);
Or, if you want to have your framework deal with match or no match, select all rows with a flag:
Select
t.*,
case when col01 like :pattern then 'match' else 'no match' end as is_match
from table_name t;

concat all rows in one rows

Hi i need to concat all rows from my table.
I have this query select * from table1; this table contains 400 fields
i cannot do this select column1 ||','||column2||','||.....from table1
can someone help e to fix it using select * from table1 to concatinate all rows.
And thank you.
In Oracle (and similar in other DBMS) you could use system tables.... and do this in two steps:
Assuming you want to combine all the columns into 1 column for X rows...
STEP 1:
SELECT LISTAGG(column_Name, '|| Chr(44)||') --this char(44) adds a comma
within group (order by column_ID) as Fields
--Order by column_Id ensures they are in the same order as defined in db.
FROM all_tab_Cols
WHERE table_name = 'YOURTABLE'
and owner = 'YOUROWNER'
--Perhaps exclude system columns
and Virtual_Column = 'NO'
STEP 2:
Copy the results into a new SQL statement and execute.
The would look something like Field1|| Chr(44)||Field2|| Chr(44)||Field3
SELECT <results>
FROM YOURTABLE;
which would result in a comma separated list of values in 1 column for all rows of YOURTABLE
If the length of all the columns (along with , space and ||) would exceed the 4000 characters allowed... we can use a clob data type instead through the use of XML objects...
* Replace step 1 with *
SELECT RTRIM(XMLAGG(XMLELEMENT(Column_ID,Column_Name,'|| Chr(44)||').extract('//text()') order by Column_ID).GetClobVal(),'|| Chr(44)||') fields
FROM all_tab_Cols
WHERE table_name = 'YOURTABLENAME'
and owner = 'YOUROWNER'
--Perhaps exclude system columns
and Virtual_Column = 'NO';
Syntax for the above attributed to This Oracle thread but updated for your needs.

PostgreSQL - Select tables where specific column is not present

I have a schema with about 3000 tables in a PostgreSQL DB, all having one row with the following columns:
id, area, use, geom, site_id
However, I just discovered that there's at least one table that misses the column site_id. I'm wondering if there are more of those.. How can I query for tables NOT having the column site_id?
My following idea doesn't work. Any suggestions?
SELECT table_name
FROM information_schema.columns
WHERE table_schema = 'schema_A' AND column_name NOT LIKE 'site_id'
GROUP BY table_name;
An easy way would be counting how many times side_id appears in that table's column listing. If the sum is zero, the table has no site_id:
SELECT table_name
FROM information_schema.columns
WHERE table_schema = 'schema_A'
GROUP BY table_name
HAVING SUM(CASE WHEN column_name LIKE 'site_id' THEN 1 ELSE 0 END) = 0;
I'm also not sure if it was intentional or not, but LIKE 'site_id' will have the same effect as = 'site_id'. If you meant to check if it contains site_id, LIKE '%site_id%' would be more appropriate.

Query information_schema.columns to return output column names and value in PostgreSQL

I want to write a query to select a column and value with information_schema clause. I tried a few options but still did not work for me.
I try the following:
SELECT column_name FROM information_schema.columns
WHERE
table_name='tblmst_app' AND table_the_column_is_from.app_id = 'some_id'
The return output that i want it should like:
Column Name | the Values
===========================
app_id | 20

Selecting multiple columns in SQL

I have a table with over a hundred columns, but I only want to select the the columns named nvarchar1, nvarchar2, ... nvarchar64. All of these 64 columns are next to each other so I was thinking maybe there is a way to select using column indices, but I couldn't find a solution.
Note: I don't want to do the obvious solution,
SELECT nvarchar1,
nvarchar2,
...
nvarchar64
...
Make use of the result from this query:
select column_name + ','
from information_schema.columns
where table_name = 'your table name'
and column_name like 'nvarchar%'