How to find the maximum length of data field in a particular column in a table and for all tables in schema - postgresql-9.5

Good Evening
We have multiple tables in multiple schemas in DB. we are tyring to find out maximum size of each column in the table for all tables and for all schemas.
Example column names: a,b,c,d,e,f,g
Example schema names: A,B,C,D
Expected output:
column_name Max_size_of_column
or
column_name Max_size_of_column column_table table_schema
I have tried the below query, but not able to get the desired output.
SELECT (select column_name from INFORMATION_SCHEMA.COLUMNS
where table_name='building'),
select max(length(select column_name from INFORMATION_SCHEMA.COLUMNS
where table_name='building')) from from INFORMATION_SCHEMA.COLUMNS
where table_name='building'
group by column_name;
Please help us to get the desired output.
Thanks

You need some kind of dynamic SQL for questions like this. But in Postgres this can be done without the need of a PL/pgSQL function, similar to the approach from this answer but combining max() and length():
with all_lengths as (
select table_schema, table_name, column_name,
query_to_xml(format('select max(length(%I)) from %I.%I', column_name, table_schema, table_name), false, true, '') as xml_max
from information_schema.columns
where table_schema in ('public') -- add the schemas you want here
and data_type in ('text', 'character varying')
)
select table_schema, table_name, column_name,
(xpath('/row/max/text()', xml_max))[1]::text::int as max_length
from all_lengths
;
I don't have Postgres 9.5 available, but I think that should work with that old version as well.
If you just want specific columns, rather than all columns with a text or varchar type, then just change the and data_type in (..) condition to and column_name in (...) inside the CTE

Related

Oracle query to SQL Server query

What is the equivalent sql sever query for
SELECT Table_Name,
Column_Name,
Data_Type,
Data_Length,
Data_Precision,
Data_Scale, Nullable,
Character_Set_Name,
Char_Col_Decl_Length,
Char_Length
FROM all_tab_cols ORDER BY Table_Name, Column_Id
May be this is what you are looking for..
SELECT TABLE_SCHEMA ,TABLE_NAME ,COLUMN_NAME
,ORDINAL_POSITION,COLUMN_DEFAULT,IS_NULLABLE,DATA_TYPE
,CHARACTER_MAXIMUM_LENGTH,CHARACTER_OCTET_LENGTH,COLLATION_NAME
,*
FROM INFORMATION_SCHEMA.COLUMNS
this is a standard sql query syntax!
You can use:
SELECT
*
FROM
information_schema.columns
Which will select most of what you have asked for, then your WHERE and ORDER BY conditions are done in the usual way.
EDIT:
For those asking, using "all_tab_cols" in a SQL server FROM condition throws an invalid object error, unless the user defines it as something. The above how this is achieved in SQL server.

get all columns of type Date/timestamp

in oracle, I want to get all columns of type DATE/TIMESTAMP(6) and i did the following. But I get the error '00933. 00000 - "SQL command not properly ended"'
My query:
Select column_name ,DATA_TYPE from user_tab_columns
Where Table_name = 'ACCOUNT' Where DATA_TYPE IN('DATE','TIMESTAMP(6)');
I searched stackoverflow for related issue, but can't find. do assist. Thanks alot!
You have used WHERE clause twice, use AND instead
Use like this
SELECT column_name, DATA_TYPE from user_tab_columns WHERE table_name = 'ACCOUNT' AND DATA_TYPE IN('DATE','TIMESTAMP(6)');
Terms in a WHERE clause should be separated by AND, and also WHERE only appears once:
select column_name,
DATA_TYPE
from user_tab_columns
where table_name = 'ACCOUNT' and
DATA_TYPE IN('DATE','TIMESTAMP(6)');

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

Finding data type with query

I need to find which fields are of CLOB data type in table with SQL query?
I had tried the below query to fetch the data type but it is giving me error:
ORA-00942: table or view does not exist
Please suggest!!!
SELECT data_type
FROM SYS.COLUMNS
WHERE OBJECT_ID = OBJECT_ID('PS_P1_EPA_EMPLOYEE');
The data dictionary for Oracle isn't the same as for other RDBMS. If this is your own schema:
select data_type
from user_tab_columns
where column_name = 'PS_P1_EPA_EMPLOYEE'
... although that looks more like a table name, so maybe:
select column_name, data_type
from user_tab_columns
where table_name = 'PS_P1_EPA_EMPLOYEE'
You can also restrict on data_type ='CLOB'.
If it isn't in your schema, you can look in all_tab_colmns or dba_tab_columns. Documentation for all three views is here.

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')