Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

Authors

Search

XML Feeds

Google Ads

« Dealing with A transport-level error has occurred when sending the request to the server errorsReview Of Beginning Spatial With SQL Server 2008 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

If you have a bunch of User-Defined Data Types in your databases and you would like to get a list of them then you can run the following query

On SQL Server 2000 and up

  1. SELECT * FROM systypes
  2. WHERE xusertype > 256

or

On SQL Server 2005 and up

  1. SELECT * FROM sys.Types
  2. WHERE is_user_defined = 1

Let's take a look how this works by adding a couple of User-Defined Data Types. we will add a birthday type which will be a datetime (on SQL Server 2008 it should be a date) and a StateCode which is a char(2)

  1. USE master
  2. EXEC SP_ADDTYPE birthday, DATETIME, 'NULL'
  3. Go
  4. EXEC SP_ADDTYPE StateCode,'Char(2)' , 'NULL'
  5. Go

Now we can create a table which uses these two types

  1. CREATE TABLE TestType (Birthday birthday,STATE StateCode)

Insert some data

  1. INSERT TestType VALUES('19100101','NY')
  2. INSERT TestType VALUES('19800101','CA')

And we can query the data as usual

  1. SELECT * FROM TestType

To see what data type is actually used to store the data we can run the following query

  1. SELECT column_name,data_type,character_maximum_length
  2. FROM information_schema.columns
  3. WHERE table_name = 'TestType'

output

-----------------
column_name	data_type	character_maximum_length
Birthday	datetime	
State		char		2


As you can see datetime and char(2) are used

We can also use the SysTypes (SQL Server 2000 and up) and Sys.Types system tables/catalog views

  1. SELECT s.name,s2.name,s2.length
  2.  FROM SysTypes s
  3. join SysTypes s2 ON s.xtype = s2.xtype
  4. WHERE s.xusertype > 256
  5. and s2.xusertype <= 256

output

----------------------
name		name		length
birthday	datetime	8
StateCode	char		8000



  1. SELECT s.name,s2.name,s2.max_length
  2. FROM Sys.Types s
  3. join Sys.Types s2 ON s2.user_type_id = s.system_type_id
  4. WHERE s.is_user_defined = 1
  5. and s2.is_user_defined = 0

output

----------------------
name		name		length
birthday	datetime	8
StateCode	char		8000



How do you drop a User-Defined Data Type?
Here is how you do it. Run the following query

  1. USE master
  2. EXEC SP_DROPTYPE 'birthday'
  3. GO

As you can see you get the following error
Server: Msg 15180, Level 16, State 1, Procedure sp_droptype, Line 32
Cannot drop. The data type is being used.

So we first need to drop the table that is using this data type

  1. DROP TABLE TestType

Now we can try again

  1. USE master
  2. EXEC SP_DROPTYPE 'StateCode'
  3.  
  4.  
  5.  
  6. USE master
  7. EXEC SP_DROPTYPE 'birthday'

And that drops the User-Defined Data Types we created




*** If you have a SQL related question try our Microsoft SQL Server Programming forum or our Microsoft SQL Server Admin forum

About the Author

User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
Social SitingsTwitterFacebookLinkedInHomePageLTD RSS Feed
1169 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

No feedback yet

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)