How to get all the columns that has leading spaces in data? - sql

I have a table with around 100 columns, I could get for a single column by using the following query
Select * from tes_tbl where col_1 like ' %'
But how can I retrieve all the columns in a single query
Im using oracle database
P.s: l'm a beginner to SQL

You've not specified which sort of SQL so I'm going to assume MS SQL Server.
As others have pointed out you can generate a query along the lines of
select * from tes_tbl where col_1 like '% ' OR col_2 like '% '
However you've mentioned that you've got a lot of columns to query. (This is the MSSQL specific bit) You can query Information Schema to get a list of the columns in your table:
select 'OR [' + COLUMN_NAME + '] like '' % ''' from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME = 'tes_tbl'
This will give you a list of OR clauses for each of the columns. You can use these to build your query (replacing the part of the WHERE clause in the query above).
You can also use this trick to explicitly name the columns in the select which is often a good idea for anything other than ad-hoc queries.

You can use OR operator as like below,
Select * from tes_tbl where col_1 like '%' OR col_2 like '%'
This will give you the result set which satisfies the conditions in WHERE clause

Related

How to select a column from the output of 'SHOW COLUMNS' sql

I am using the sql command SHOW COLUMNS in this way
SHOW COLUMNS FROM TABLE A;
It outputs multiple columns, specifically one called 'COLUMN_NAME' that I would like to select.
I tried doing
SELECT COLUMN_NAME FROM (SHOW COLUMNS FROM TABLE A);
which gives an error, is there another way I can just show one of the columns of the output? Is this because the output is not a table so I cannot query from it?
You can use the like function:
show columns like 'COLUMN_NAME' in table A;
There are limitations on using the output of the show command, so an alternate approach is to use the INFORMATION_SCHEMA.
select * from INFORMATION_SCHEMA.COLUMNS where
TABLE_SCHEMA = 'MY_SCHEMA'
and TABLE_NAME = 'A'
and COLUMN_NAME = 'COLUMN_NAME';
If you need to query the results of a show command, you can use the result_scan table function:
show columns like '%' in table t1;
select * from table(result_scan(last_query_id())) where "column_name" = 'COLUMN_NAME';
Remember to double quote the column names produced by the show command. It's a metadata result and the column names are lower case.

Is it possible to search multiple columns from multiple tables by using variables for references?

I am using a search algorithm to find tables in my database that meet certain criteria. The output looks like this:
TableName | ColumnName
--------------+---------------
Table1 | Column1
Table1 | Column2
Table1 | Column3
Table2 | Column4
Table2 | Column1
... | ...
Table652 | Column873
I then try to use that table as input for another query where I attempted to use either:
WHERE Tablename.ColumnName LIKE 'Post 2013 - %'
Or
WHERE ((TableName).(ColumnName)) LIKE 'Post 2013 - %'
Or
SELECT ...
CONCAT(TableName, '.', ColumnName) AS TabCol
FROM search
WHERE TabCol LIKE 'Post 2013 - %'
The problem is that my code is returning errors at each of these WHERE statements. Is it possible for me to use the first output as a way to scan through those tables for a specific value, returning the TableName and ColumnName where it found the value?
To do something like that there are generally two methods that I have found that work. One is to extract the entire database to text file and use a text searching tool for the data you seek.
The other is to use the system tables to identify all the tabels / fields. Then have some code loop through those fields running your query. You may want to check the data type first to eliminate things like date, numeric or integer data types. Or you could do this manually. But there is not really as standard Sql way to do this.
If your using SQL Server as Database :- You can use dynamic queries.
you can write a stored procedure or Function which has logic similar to that in the screenshot which return table data which the desired results. And you can pass Tablename , Columnname, Conditions as the parameters . Assuming you have all the data in the table.
Here is a sample query i tried with above scenario.
select * from search;
select * from table2;
Declare #sql varchar(max)
select #sql='select * from '+ tablename+' where '+columname+'='+'''john-her'''
from search where tablename='table2' and columname='namedata2'
select #sql
exec(#sql)
I just tried with sample ...there may be multiple scenario with conditions. Hope this helps you in giving you an idea how to solve your problem!!

How to locate all columns in all tables that have all NULL values?

I have legacy SQL Server database that's been upgraded since forever. I shaved the DB down to keep only last 4 years worth of information.
As a result of its longevity, there are a ton of columns that are no longer used and have been deprecated over the years.
I am looking to find a reasonably painless way to locate all columns in all tables that have nothing but NULL values.
Other than writing a little desktop app to do it, is there a way to do this via SQL?
Yes, select count() over every column. Those that return 0 are the ones that only have null values.
Like this:
select count(col1) col1 from mytable
Repeat for every table and column.
You could generate such SQL statements with this statement:
SELECT 'SELECT COUNT(' + COLUMN_NAME + ') AS COLUMN_NAME FROM ' + TABLE_NAME + ';'
FROM INFORMATION_SCHEMA.COLUMNS;
Take the output of the above query and execute it as batch of SQL SELECT statements, and inspect the result, locating all the 0 outputs.

Select columns containing string

Is it possible to retrieve all data in columns containing a particular string? I do not have any control over the data structure and I rather shorten my code than to type everything if possible. As far as I know statements as LIKE and CONTAIN are only used to retrieve particular rows instead of columns. Lets say I have a table with column names: time, run1, run2, run3, ....., run 34, I would like to use something like:
SELECT columns FROM [Tablename] WHERE columns CONTAIN 'run';
Is this possible?
Thanks!
Use like.
In Oracle, you could do
SELECT column_name
from all_tab_columns
where table_name = 'YOUR_TABLE'
and lower(column_name) like 'run%'
In SQL Server, you could do
SELECT column_name
from information_schema.columns
where table_name = 'YOUR_TABLE_NAME'
and lower(column_name) like 'run%'

How can I search for numbers in a varchar column

I've got a simple nvarchar(25) column in an SQL database table. Most of the time, this field should contain alphanumeric text. However, due to operator error, there are many instances where it contains only a number. Can I do a simple search in SQL to identify these cases? That is, determine which rows in the table contain only digits in this column. As an extension, could I also search for those column values which contain only digits and a space and/or slash.
In other languages (eg. Perl, Java) a regular expression would resolve this quickly and easily. But I haven't been able to find the equivalent in SQL.
yes you can achive this by using isnumeric function available in sql sever
check more at this link : http://msdn.microsoft.com/en-us/library/aa933213(SQL.80).aspx
All the answers referred to the isnumeric function, but they are not correct, I've mentioned in a comment for all the answers the flaw in the answers. The correct solution is to use a regular expression in your where clause, which contains not like '%[^0-9]%', see the example below:
select column_name from table_name where column_name not like '%[^0-9]%'
select column_name from table_name where IsNumeric(column_name) <> 1
Numeric Only:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1
With Space:
SELECT * FROM Table WHERE Field LIKE '% %'
With Slash:
SELECT * FROM Table WHERE Field LIKE '%/%'
Combined:
SELECT * FROM Table WHERE ISNUMERIC(Field) = 1 OR Field LIKE '% %' OR Field LIKE '%/%'