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

« SQL Friday, The Best SQL Server Links Of The Past Week Episode 8SQL Friday, The Best SQL Server Links Of The Past Week Episode 7 »
comments
Rate Post:
submit to reddit Digg!FacebookDotnetkicks

I posted a puzzle here Prove that this is an integer asking people to prove that the following code is indeed an int

  1. DECLARE @d INT
  2. SELECT @d = 500


or this the number 5 itself, how do you know that this is an integer?
Well this is pretty easy, you can sql_variant_property with the BaseType property. Run this code

  1. IF CAST(SQL_VARIANT_PROPERTY(@d,'BaseType') AS VARCHAR(20)) = 'int'
  2. PRINT 'yes'
  3. ELSE
  4. PRINT 'no'

As you can see yes is printed, the code below will return int for the number 5

  1. SELECT CAST(SQL_VARIANT_PROPERTY(5,'BaseType') AS VARCHAR(20))

what about scale and precision?
Here run this

  1. SELECT    
  2.             CAST(SQL_VARIANT_PROPERTY(14400195639.123,'BaseType') AS VARCHAR(20)) + '(' +
  3.             CAST(SQL_VARIANT_PROPERTY(14400195639.123,'Precision') AS VARCHAR(10)) + ',' +
  4.             CAST(SQL_VARIANT_PROPERTY(14400195639.123,'Scale') AS VARCHAR(10)) + ')'

Running that code will return numeric(14,3)

Now, why would you ever need this? Sometimes it is handy whene you have a table like this

  1. DECLARE TABLE Foo(bar SMALLINT ,col1 VARCHAR(40), col2..........)

when you run a query like this

  1. SELECT * FROM Foo WHERE bar = 3

you might get an index scan because 3 is not a smallint and you get a conversion. Here is a way to get around that

  1. SELECT * FROM Foo WHERE bar = CONVERT(SMALLINT,3)




*** 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
1236 views
submit to reddit Digg!FacebookDotnetkicks

Comments and Feedback

1 comment

Comment from: Naomi [Member] Email
*****
Thanks.
09/07/09 @ 17:35

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.)