<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Denis Gobo : Tip, sqL Server 2000</title><link>http://sqlblog.com/blogs/denis_gobo/archive/tags/Tip/sqL+Server+2000/default.aspx</link><description>Tags: Tip, sqL Server 2000</description><dc:language>en</dc:language><generator>CommunityServer 2.1 SP2 (Build: 61129.1)</generator><item><title>Return Null If A Value Is A Certain Value</title><link>http://sqlblog.com/blogs/denis_gobo/archive/2007/11/06/3229.aspx</link><pubDate>Tue, 06 Nov 2007 18:28:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:3229</guid><dc:creator>Denis Gobo</dc:creator><slash:comments>7</slash:comments><comments>http://sqlblog.com/blogs/denis_gobo/comments/3229.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=3229</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=3229</wfw:comment><description>&lt;P&gt;You need to return NULL only if the value of your data is a certain value. How do you do this? There are three different ways. &lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;NULLIF&lt;/STRONG&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;NULLIF&lt;/SPAN&gt;(@1&lt;SPAN style="COLOR:#ff0000;"&gt;,'D'&lt;/SPAN&gt;)&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;REPLACE&lt;/STRONG&gt;&lt;BR&gt;This should not really be used, I just added it here to demonstrate that you can in fact use it.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;REPLACE&lt;/SPAN&gt;(@1,&lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt;,NULL)&lt;/P&gt;
&lt;P&gt;&lt;BR&gt;&lt;STRONG&gt;CASE&lt;/STRONG&gt;&lt;BR&gt;With case you can test for a range of values. You can test for example for values between A and D. If you reverse the logic then you also don't need to provide the ELSE part since it defaults to NULL anyway.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; &lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--No else needed&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 &amp;lt;&amp;gt; &lt;SPAN style="COLOR:#ff0000;"&gt;'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;And this is how you test for a range.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--Null&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'D' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 BETWEEN &lt;SPAN style="COLOR:#ff0000;"&gt;'A'&lt;/SPAN&gt; AND&lt;SPAN style="COLOR:#ff0000;"&gt; 'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#009900;"&gt;--E&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;DECLARE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;char&lt;/SPAN&gt;(1)&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; @1 =&lt;SPAN style="COLOR:#ff0000;"&gt;'E' &lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CASE&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;WHEN&lt;/SPAN&gt; @1 BETWEEN &lt;SPAN style="COLOR:#ff0000;"&gt;'A'&lt;/SPAN&gt; AND&lt;SPAN&gt; 'D'&lt;/SPAN&gt; &lt;SPAN style="COLOR:#3333ff;"&gt;THEN&lt;/SPAN&gt; NULL &lt;SPAN style="COLOR:#3333ff;"&gt;ELSE&lt;/SPAN&gt; @1 &lt;SPAN style="COLOR:#3333ff;"&gt;END&lt;/SPAN&gt; &lt;/P&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=3229" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/NULLIF/default.aspx">NULLIF</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Nulls/default.aspx">Nulls</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/sqL+Server+2000/default.aspx">sqL Server 2000</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2005/default.aspx">SQL Server 2005</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Tip/default.aspx">Tip</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/trick/default.aspx">trick</category></item><item><title>Increase Your Productivity With Query Analyzer/SQL Server Management Studio</title><link>http://sqlblog.com/blogs/denis_gobo/archive/2007/09/24/2664.aspx</link><pubDate>Mon, 24 Sep 2007 15:52:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:2664</guid><dc:creator>Denis Gobo</dc:creator><slash:comments>5</slash:comments><comments>http://sqlblog.com/blogs/denis_gobo/comments/2664.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=2664</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=2664</wfw:comment><description>&lt;DIV&gt;&lt;STRONG&gt;Drag And Drop Column Names&lt;/STRONG&gt; &lt;BR&gt;In Query Analyzer you can save a lot of time by using this trick instead of typing all the column names of a table &lt;BR&gt;Hit F8, this will open Object Browser &lt;BR&gt;Navigate to DatabaseName/TableName/Columns &lt;BR&gt;Click on the column folder and drag the column folder into the Code Window &lt;BR&gt;Upon release you will see that all the column names are in the Code Window &lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Use Shortcuts&lt;/STRONG&gt; &lt;BR&gt;Did you know that you can have CTRL + Number key combinations/shortcuts in Query Analyzer? You can set it up so that CTRL + 5 executes sp_who2 for example. I thought everyone already knew this but apparently not; I showed this to 3 coworkers yesterday and they didn't know about it. So how do you set this up? It is pretty easy, select &lt;STRONG&gt;Tools --&amp;gt;Customize&lt;/STRONG&gt; from the toolbar. In the Customize tab you will see 3 shortcuts already: &lt;BR&gt;&lt;BR&gt;ALT + F1 sp_help &lt;BR&gt;&lt;BR&gt;CTRL + 1 sp_who &lt;BR&gt;&lt;BR&gt;CTRL + 2 sp_lock &lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;To add your own queries/procedures type or paste the query or stored procedure that you want to execute into one of the empty ones (see picture below) &lt;BR&gt;&lt;BR&gt;&lt;A href="http://photos1.blogger.com/x/blogger/2193/1601/1600/693600/CustomizeQA.jpg"&gt;&lt;IMG alt="" src="http://photos1.blogger.com/x/blogger/2193/1601/400/432547/CustomizeQA.jpg" border=0&gt;&lt;/A&gt; &lt;BR&gt;&lt;BR&gt;If you have 4 Query Analyzers applications open it will be available in all 4 of them. I usually have 4 Query Analyzer applications open, one for staging, one for development and one each for one of the production boxes. so if I have to quickly check that a table is the same on all 4 machines I just hit CTRL + 6 in all 4 Query Analyzer applications and I am done. &lt;BR&gt;&lt;BR&gt;I created a Word document that looks like this &lt;BR&gt;3 - errorlog &lt;BR&gt;4 - product table &lt;BR&gt;5 - country table &lt;BR&gt;6 - vendor codes &lt;BR&gt;7 - sp_who2 &lt;BR&gt;etc etc etc &lt;BR&gt;&lt;BR&gt;The font-size is 32, I printed this out and put it next to my monitor so that I can quickly see which key combination I have to hit. Once you set this up you will see how much time it will save you.&amp;nbsp;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;In SSMS 2005. Click Tools -&amp;gt; Customize -&amp;gt; "Commands" tab -&amp;gt; "Keyboard" button (Lower left corner of screen).On the options window, click the Keyboard option (under Environment). See pictures below: &lt;BR&gt;&lt;BR&gt;&lt;A href="http://photos1.blogger.com/x/blogger/2193/1601/1600/392943/ss2005Keyboardoption.jpg"&gt;&lt;IMG src="http://photos1.blogger.com/x/blogger/2193/1601/400/621497/ss2005Keyboardoption.jpg" border=0&gt;&lt;/A&gt; &lt;BR&gt;&lt;A href="http://photos1.blogger.com/x/blogger/2193/1601/1600/872181/ss2005Keyboardoption2.jpg"&gt;&lt;IMG src="http://photos1.blogger.com/x/blogger/2193/1601/400/356770/ss2005Keyboardoption2.jpg" border=0&gt;&lt;/A&gt; &lt;/DIV&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=2664" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Productivity/default.aspx">Productivity</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Shortcut/default.aspx">Shortcut</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/sqL+Server+2000/default.aspx">sqL Server 2000</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2005/default.aspx">SQL Server 2005</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/SQL+Server+2008/default.aspx">SQL Server 2008</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Tip/default.aspx">Tip</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Tips+and+Tricks/default.aspx">Tips and Tricks</category></item><item><title>SQL Gotcha: Do you know what data type is used when running ad-hoc queries?</title><link>http://sqlblog.com/blogs/denis_gobo/archive/2007/09/10/2511.aspx</link><pubDate>Mon, 10 Sep 2007 14:31:00 GMT</pubDate><guid isPermaLink="false">21093a07-8b3d-42db-8cbf-3350fcbf5496:2511</guid><dc:creator>Denis Gobo</dc:creator><slash:comments>1</slash:comments><comments>http://sqlblog.com/blogs/denis_gobo/comments/2511.aspx</comments><wfw:commentRss>http://sqlblog.com/blogs/denis_gobo/commentrss.aspx?PostID=2511</wfw:commentRss><wfw:comment>http://sqlblog.com/blogs/denis_gobo/rsscomments.aspx?PostID=2511</wfw:comment><description>&lt;P&gt;This is for SQL Server 2000 only, SQL Server 2005 is a lot smarter which is another reason to upgrade.&lt;BR&gt;When running the following query you probably already know that 2 is converted to an int datatype.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; *&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;FROM&lt;/SPAN&gt; Table&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;WHERE&lt;/SPAN&gt; ID =2&lt;BR&gt;&lt;BR&gt;What about the value 2222222222? Do you think since it can't fit into an int that it will be a bigint? Let's test that out.&lt;BR&gt;First create this table.&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;CREATE TABLE&lt;/SPAN&gt; TestAdHoc (id &lt;SPAN style="COLOR:#3333ff;"&gt;bigint primary key&lt;/SPAN&gt;)&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;INSERT INTO&lt;/SPAN&gt; TestAdHoc&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; 1 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT&lt;/SPAN&gt; 2433253453453466666 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT &lt;/SPAN&gt;2 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT&lt;/SPAN&gt; 3 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT&lt;/SPAN&gt; 4 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT&lt;/SPAN&gt; 5 &lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;BR&gt;SELECT&lt;/SPAN&gt; 6 &lt;/P&gt;
&lt;P&gt;Now let's run these 2 queries which return the same data&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; *&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;FROM&lt;/SPAN&gt; TestAdHoc&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;WHERE&lt;/SPAN&gt; ID =2433253453453466666&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; *&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;FROM&lt;/SPAN&gt; TestAdHoc&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;WHERE&lt;/SPAN&gt; ID =&lt;SPAN style="COLOR:#cc33cc;"&gt;CONVERT&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#3333ff;"&gt;bigint&lt;/SPAN&gt;,2433253453453466666)&lt;BR&gt;&lt;BR&gt;Now run the following SET statement and run the 2 queries again&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SET SHOWPLAN_TEXT ON &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;&lt;/SPAN&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; *&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;FROM&lt;/SPAN&gt; TestAdHoc&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;WHERE&lt;/SPAN&gt; ID =2433253453453466666&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; *&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;FROM&lt;/SPAN&gt; TestAdHoc&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;WHERE&lt;/SPAN&gt; ID =&lt;SPAN style="COLOR:#cc33cc;"&gt;CONVERT&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#3333ff;"&gt;bigint&lt;/SPAN&gt;,2433253453453466666)&lt;BR&gt;&lt;BR&gt;And what do we see?&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;First Query&lt;/STRONG&gt;&lt;BR&gt;--Nested Loops(Inner Join, OUTER REFERENCES:([Expr1002], [Expr1003], [Expr1004]))&lt;BR&gt;--Compute Scalar(DEFINE:([Expr1002]=Convert([@1])-1,&lt;BR&gt;[Expr1003]=Convert([@1])+1, [Expr1004]=If (Convert([@1])-1=NULL)&lt;BR&gt;then 0 else 6If (Convert([@1])+1=NULL) then 0 else 10))&lt;BR&gt;--Constant Scan&lt;BR&gt;--Clustered Index Seek(OBJECT:([Blog].[dbo].[TestAdHoc].[PK__TestAdHoc__2818EA29]),&lt;BR&gt;SEEK:([TestAdHoc].[id] &amp;gt; [Expr1002] AND [TestAdHoc].[id] &amp;lt; [Expr1003]), WHERE:(Convert([TestAdHoc].[id])=[@1]) ORDERED FORWARD)&lt;BR&gt;&lt;BR&gt;&lt;STRONG&gt;Second Query&lt;BR&gt;&lt;/STRONG&gt;--Clustered Index Seek(OBJECT:([Blog].[dbo].[TestAdHoc].[PK__TestAdHoc__2818EA29]),&lt;BR&gt;SEEK:([TestAdHoc].[id]=2433253453453466666) ORDERED FORWARD)&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;The first query has a much different execution plan than the second query. The first execution plan has a lot more than the second execution plan and will be a little slower.&lt;BR&gt;&lt;BR&gt;So how do you know what dataype the value is converted to? Here is a simple SQL query which I first saw on Louis Davidson's &lt;A class="" href="http://sqlblog.com/blogs/louis_davidson/default.aspx"&gt;blog&lt;/A&gt;. Just run this query.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(2433253453453466666,&lt;SPAN style="COLOR:#ff0000;"&gt;'BaseType'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(20)) +&lt;SPAN style="COLOR:#ff0000;"&gt; '('&lt;/SPAN&gt; +&lt;BR&gt;&lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(2433253453453466666,&lt;SPAN style="COLOR:#ff0000;"&gt;'Precision'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(10)) +&lt;SPAN style="COLOR:#ff0000;"&gt; ','&lt;/SPAN&gt; +&lt;BR&gt;&lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(2433253453453466666,&lt;SPAN style="COLOR:#ff0000;"&gt;'Scale'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(10)) +&lt;SPAN style="COLOR:#ff0000;"&gt; ')'&lt;/SPAN&gt;&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;So the output is numeric(19,0). So instead of a bigint SQL Server converts the value to a numeric data type.&lt;BR&gt;Here is another query which demonstrates the different datatypes used.&lt;BR&gt;&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(2,&lt;SPAN style="COLOR:#ff0000;"&gt;'BaseType'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(20))&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;/SPAN&gt; ALL&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(222222222,&lt;SPAN style="COLOR:#ff0000;"&gt;'BaseType'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(20))&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;UNION&lt;/SPAN&gt; ALL&lt;BR&gt;&lt;SPAN style="COLOR:#3333ff;"&gt;SELECT&lt;/SPAN&gt; &lt;SPAN style="COLOR:#cc33cc;"&gt;CAST&lt;/SPAN&gt;(&lt;SPAN style="COLOR:#cc33cc;"&gt;SQL_VARIANT_PROPERTY&lt;/SPAN&gt;(2222222222,&lt;SPAN style="COLOR:#ff0000;"&gt;'BaseType'&lt;/SPAN&gt;) &lt;SPAN style="COLOR:#3333ff;"&gt;AS varchar&lt;/SPAN&gt;(20))&lt;BR&gt;&lt;BR&gt;So when running ad-hoc queries it is always a good practice to use parameters or inline convert statements. &lt;/P&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=2511" width="1" height="1"&gt;</description><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Gotcha/default.aspx">Gotcha</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/sqL+Server+2000/default.aspx">sqL Server 2000</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/Tip/default.aspx">Tip</category><category domain="http://sqlblog.com/blogs/denis_gobo/archive/tags/trick/default.aspx">trick</category></item></channel></rss>