This site will work and look better in a browser that supports web standards, but it is accessible to any browser or Internet device.
N U T S H E L L | |
Module | GetDataTypeName |
---|---|
Description | Returns data type name for a given field |
Environment | SQL Server 2000/2005 |
Author | Michael Sorens |
Since | CleanCode 0.9.22 |
GetDataTypeName is a small but handy function that returns the name of the data type of a particular field.
GetDataTypeName @TableName, @FieldName where: @TableName ::= table name @FieldName ::= field name
GetDataTypeName looks up a table name and field name in the system tables
sysobjects
and syscolumns
.
If it finds a match to your parameters--and note they are case sensitive!--it
returns the name of the data type of that field.
If no match is found, the function returns null
.
Note that this query does not take into account a schema name, if present. If you use the same table name in two different schemas you may get an invalid result (have not tested that). For example if you have two tables Sales.OrderDetails and Commission.OrderDetails (i.e. the table name OrderDetails is used in both the Sales schema and the Commission schema) GetDataTypeName may not find the correct match.
With reference to the standard AdventureWorks database:
select dbo.GetDataTypeName('PurchaseOrderHeader', 'SubTotal')
select dbo.GetDataTypeName('HumanResources.EmployeePayHistory','ModifiedDate')
select dbo.GetDataTypeName('EmployeePayHistory','ModifiedDate')
select dbo.GetDataTypeName('HumanResources.EmployeePayHistory','modifieddate')