Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server: Checking the datatype of a column

How to check if a column in a table has a specific datatype?

For example, how to check if a column in SQL Server table is of datatype char(11)?

like image 466
user1274655 Avatar asked Apr 27 '12 15:04

user1274655


People also ask

How do I find the datatype of a column in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

How do I find the column data type in SQL Server Management Studio?

Once you connect to a database in SSMS, you can view these data types by navigating to Programmability-> Types->System Data Types.

How do I find the data type of a variable in SQL Server?

Use TYPE_NAME() to Get the Name of a Data Type in SQL Server In SQL Server, you can use the TYPE_NAME() function to return the name of a data type, based on its ID. This can be useful when querying a system view such as sys. columns that returns the type's ID but not its name.

How can I get column details of a table in SQL?

sp_columns procedure To get the column name of a table we use sp_help with the name of the object or table name. sp_columns returns all the column names of the object. The following query will return the table's column names: sp_columns @table_name = 'News'


2 Answers

select COLUMN_NAME 
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char'
and CHARACTER_MAXIMUM_LENGTH = 11
and TABLE_NAME = 'your_table'

using syscolumns:

SELECT name FROM SYSCOLUMNS
where length = 11
and xtype = 175 --char type
like image 183
juergen d Avatar answered Oct 23 '22 02:10

juergen d


select case when DATA_TYPE= 'char' then 'T' else 'F' end,    
case when CHARACTER_MAXIMUM_LENGTH = 11 then 'T' else 'F' end    
from INFORMATION_SCHEMA.COLUMNS     
where COLUMN_NAME = 'MY_COLUMN_NAME'     
and TABLE_NAME = 'MY_TABLE_NAME'
like image 42
Sushil Avatar answered Oct 23 '22 01:10

Sushil