Data type sql database - ssms

I have a sql database and are trying to get the datatype of a column
Database: "dbo.Parts"
Table: "miniParts"
I tried the following using Microsoft SSMS
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = "miniPARTS" AND COLUMN_NAME = "Date"
I get the following errors:
Invalid column name "dbo.PARTS"
Invalid column name "Date"
any ideas??

Try this :
SELECT COLUMN_NAME,
DATA_TYPE,
IS_NULLABLE,
CHARACTER_MAXIMUM_LENGTH,
NUMERIC_PRECISION,
NUMERIC_SCALE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME='your_table_name';

Related

How To Find The BigQuery Table Required Columns

I have a table in the BigQuery that I would like to know what fields are required. How do I do that? Thanks in advance!
I tried the following queries, but none of them tell me the required field information.
SELECT * FROM mydataset.INFORMATION_SCHEMA.TABLES;
SELECT column_name, data_type
FROM `myprojectid`.mydataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'mytablename'
Add is_nullable column:
SELECT column_name, data_type, is_nullable
FROM `myprojectid`.mydataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = 'mytablename'

Postgres - How to retrieve default value in table structure query

I'm trying to retrieve some table's structures with the column default values using this query :
SELECT column_name, is_nullable, character_maximum_length, udt_name, default_value
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position
But I'm unable to find the correct value to select the default values. I tried "default_value" but it doesn't work...
As documented in the manual the default value is available in column_default
SELECT column_name, is_nullable, character_maximum_length, udt_name, column_default
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'USER'
ORDER BY ordinal_position

Postgres raises error when I try filter information_schema.columns.column_name by non-existing column name

I need to filter out columns 'x' from my table and I don't know if this column exists at all. I wrote a query:
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'T'
AND column_name <> "X"
And it throws error:
[42703] ERROR: column "X" does not exist
Why? I tried to convert _column_name_ to string - it didn't work.
What the problem?
$ psql --version
psql (PostgreSQL) 10.4 (Ubuntu 10.4-2.pgdg16.04+1)
Thank in advance!
The problem was the double quote ...
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'T'
AND column_name <> 'X'
This should work ...
Just applied a workaround like this:
select t.column_name
from (SELECT column_name :: text
FROM information_schema.columns
WHERE table_name = 'T') t
where t.column_name <> 'X'

Selecting DataTypes of specific columns Oracle SQl Developer

I am trying to generate data_types of specific columns.
Time Date Msg
34.324, 09/13/2011, thankyou,
I would like to generate the type of that specific value
e.g
VarChar2, Date, varchar2(char 30)
I wrote this sql statement below and it works for all the tables
Select data_type, column_name, table_name, owner from all_tab_columns;
However when I try to access specific tables or column_names or both I only get the headers
like so..
INPUT
select data_type, column_name, owner from all_tab_columns where
table_name = 'MyTable' and column_name= 'ColumnOfInterest'
OUTPUT
data_type column_name table_name, owner
Any ideas on what I am doing wrong ?
OUTPUT From First Query
select data_type, column_name, owner from all_tab_columns
DATA_TYPE COLUMN_NAME OWNER
VARCHAR2 ColumnName1 User21
VARCHAR2 CN2 USER22
NUMBER CN3 USER22
VARCHAR2 CN4 USER21
VARCHAR2 CN5 USER22
VARCHAR2 CN6 USER32

How do I list all the columns in a table?

For the various popular database systems, how do you list all the columns in a table?
For MySQL, use:
DESCRIBE name_of_table;
This also works for Oracle as long as you are using SQL*Plus, or Oracle's SQL Developer.
For Oracle (PL/SQL)
SELECT column_name
FROM user_tab_cols
WHERE table_name = 'myTableName'
For MySQL
SHOW COLUMNS FROM table_name
For MS SQL Server:
select COLUMN_NAME from information_schema.columns where table_name = 'tableName'
(5 years laters, for the Honor of PostgreSQL, the most advanced DDBB of the Kingdom)
In PostgreSQL:
\d table_name
Or, using SQL:
select column_name, data_type, character_maximum_length
from INFORMATION_SCHEMA.COLUMNS
where table_name = 'table_name';
I know it's late but I use this command for Oracle:
select column_name,data_type,data_length from all_tab_columns where TABLE_NAME = 'xxxx' AND OWNER ='xxxxxxxxxx'
SQL Server
SELECT
c.name
FROM
sys.objects o
INNER JOIN
sys.columns c
ON
c.object_id = o.object_id
AND o.name = 'Table_Name'
or
SELECT
COLUMN_NAME
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'Table_Name'
The second way is an ANSI standard and therefore should work on all ANSI compliant databases.
Call below code in MS SQL Server:
sp_columns [tablename]
Microsoft SQL Server Management Studio 2008 R2:
In a query editor, if you highlight the text of table name (ex dbo.MyTable) and hit ALT+F1, you'll get a list of column names, type, length, etc.
ALT+F1 while you've highlighted dbo.MyTable is the equivalent of running EXEC sp_help 'dbo.MyTable' according to this site
I can't get the variations on querying INFORMATION_SCHEMA.COLUMNS to work, so I use this instead.
For SQL Server
sp_help tablename
Just a slight correction on the others in SQL Server (schema prefix is becoming more important!):
SELECT name
FROM sys.columns
WHERE [object_id] = OBJECT_ID(N'dbo.tablename');
SQL Server
To list all the user defined tables of a database:
use [databasename]
select name from sysobjects where type = 'u'
To list all the columns of a table:
use [databasename]
select name from syscolumns where id=object_id('tablename')
Example:
select Table_name as [Table] , column_name as [Column] , Table_catalog as [Database], table_schema as [Schema] from information_schema.columns
where table_schema = 'dbo'
order by Table_name,COLUMN_NAME
Just my code