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)
?
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'.
Once you connect to a database in SSMS, you can view these data types by navigating to Programmability-> Types->System Data Types.
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.
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'
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
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With