How can a column be renamed in results when getting columns from INFORMATION_SCHEMA? - sql

So I have this:
CREATE PROCEDURE getBattingColumnNames
AS
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Batting'
AND COLUMN_NAME NOT IN ('playerID','yearID','stint','teamID','lgID', 'G_batting','GIDP','G_old');
GO
And it works great. I get all the column names that I want, in c# I use this to populate a drop down with the column names. however, one of my column names, "Doub" I would like to change. So playing around with it I tried:
SELECT COLUMN_NAME.Doub AS 'DB'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (TABLE_NAME = 'Batting')
and a variation of that, and the error is the mulitplart identifier could not be bound. How can i change that column name in this query?

You could use a case to translate the column name:
select case COLUMN_NAME
when 'Doub' then 'DB'
else COLUMN_NAME
end
from ...

Related

How to find every table/columns in SQL with a string/number

I'm Searching for a query to find all possible columns and table names for a specific piece of string/number.
Let's say I have a string/number as 100031-16-FWMD-D20, is there a query to find all the columns and table names which has the value in my database?
Column name-----
Select * from information_schema.columns where column_ name like '%string%'
Table name----
Select * from information_schema.tables where table_name like '%string%'
I would suggest you string up the columns for text datatype from information_schema.columns and after that use the query which is creatd to manually query the search string
Step 1:
select 'select '+column_name+' as search_text,'''+table_name+''' as table_name from '+TABLE_SCHEMA+'.'+TABLE_NAME+' union all'
from INFORMATION_SCHEMA.columns
where TABLE_SCHEMA='dbo'
and data_type like '%char%'
Step 2:
select *
from(<output of Step 1 after removing the last 'union all')x
where x.search_text like '%100031-16-FWMD-D20%'

SQL Server : getting certain table names from query

Is it possible to display just certain table names in the query:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
Most of the time I would need all the table names but there are curtain situations where I need just certain row names.
When I try doing the following:
USE [WebContact]
SELECT COLUMN_NAME, ContactDateTime
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
It tell me that
Invalid column name 'ContactDateTime'
even though that is one of the row names.
Is this possible to do?
The column ContactDateTime may be a column in your table but it is not a column in the INFORMATION_SCHEMA.COLUMNS view.
Since it is not a column there, SQL Server is going to error out saying that it is invalid.
I think what you're trying to do is add another WHERE clause to your statement:
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME] = 'ContactDateTime'; -- Here!
Or if you want to add multiple columns...
USE [WebContact]
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
AND [COLUMN_NAME]
IN ('ContactDateTime', 'column2', 'column3', ... 'column(n)'); -- Here!
Also see here for the case against using INFORMATION_SCHEMAS.
if ContactDateTime is a column that you are looking for in table memberEmails then you can do this
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'memberEmails'
and COLUMN_NAME='ContactDateTime'
The view INFORMATION_SCHEMA.COLUMNS don't have column name ContactDateTime. Please read document first

Find all tables whose name ends with a certain suffix

I have thousand of tables in database. Some names end with _History.
For example :
abc_History
bcd_History
123_History
How do I find all tables which name is end with _History.
Some thing like:
SELECT
table_name
FROM sys.tables WHERE table_name LIKE '_History%'
And
error : Invalid column name 'table_name'.
Try this:
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE TABLE_NAME LIKE '%_History'
OR
SELECT name
FROM sys.tables
WHERE name LIKE '%_History'
Thanks to #Saharsh-shah.
To get table name with all column names in the same query, use this :
SELECT
`TABLE_NAME`,
group_concat(`COLUMN_NAME` separator ',') AS `COLUMNS`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_NAME` IN(
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.tables
WHERE `TABLE_NAME` LIKE '%_History'
)
GROUP BY `TABLE_NAME`
You can push it in multidimensional PHP array very simply with the following :
$tables['columns'] = explode(',', $tables['columns']);
Hope it can help some.

Find out the default value for a column (Oracle)

I wonder if there is a way to find out the default value of some column with a simple select statement. Tried several things like:
SELECT * FROM all_tab_columns WHERE table_name = 'tablename'
But I can't see the defaultvalues for the columns there. And no I do not want to use something like SQL Plus, I need a SELECT, guess there is some table providing that info?
Select TABLE_NAME, COLUMN_NAME, DATA_DEFAULT
from DBA_TAB_COLUMNS
where TABLE_NAME = 'TABLE_NAME';
Replace the Table_Name for which you want to see the default column data.
try the below query
Select * From USER_TAB_COLUMNS where TABLE_NAME ='Table Name'
Default values are in DATA_DEFAULT column from ALL_TAB_COLUMNS:
SELECT TABLE_NAME, COLUMN_NAME, DATA_DEFAULT
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'tablename'
[2021-01-26] Edit:
Note that the 'tablename' parameter must match the case of real table name, as it appears on ALL_TAB_COLUMNS. More often it will be uppercase, so may be useful use UPPER() function to ensure it.
WHERE TABLE_NAME = UPPER('tablename')
None of the above examples worked for me, but this one did. Key part being the 'Upper' for table_name. Perhaps specific to my team's database but if you're stuck this should work
SELECT a.column_name "Column",
a.data_default "Default"
FROM all_tab_columns a
WHERE a.table_name = Upper('tablename')

Query to pull all the table names and column names

I need a query (ORACLE) to pull all the table names and column names in a database for a given value?
Example: If I give a value as "TEST", I need a query which pulls all the TABLE_NAMES and COLUMN_NAMES which has the value "TEST".
select table_name, null column_name from all_tables where table_name like '%TEST%'
union all
select null, column_name from all_tab_columns where column_name like '%TEST%';
Another way is using bind variables within a procedure such
DEFINE vSearch = '%TEST%'
ACCEPT vSearch char PROMPT 'Enter a search value: '
SELECT *
FROM USER_TAB_COLS
WHERE column_name LIKE '&&vSearch'
OR table_name LIKE '&&vSearch';
View All columns of a particular table user use "ALL_TAB_COLUMNS"
If you wants to describe a particular table user use "DESC Table_name;"
Kindly Try this..
You can use the USER_TAB_COLUMNS tables
This should get you columns and tables and views:
SELECT 'Column: '||owner||'.'||table_name||'.'||column_name
FROM dba_tab_columns
WHERE column_name = 'TEST'
UNION ALL
SELECT 'Table: '||owner||'.'||table_name
FROM dba_tables
WHERE table_name = 'TEST'
UNION ALL
SELECT 'View: '||owner||'.'||view_name
FROM dba_views
WHERE view_name = 'TEST';
Note you can also use the ALL_* dictionary views if you don't have access to the DBA_ views, but you'll only see objects you have access to.