<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://vstoolsforum.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">SQL Server (T-SQL)</title><subtitle type="html">Comments and notes on SQL Server 2000, 2005, and T-SQL</subtitle><id>http://vstoolsforum.com/blogs/sqlserver/atom.aspx</id><link rel="alternate" type="text/html" href="http://vstoolsforum.com/blogs/sqlserver/default.aspx" /><link rel="self" type="application/atom+xml" href="http://vstoolsforum.com/blogs/sqlserver/atom.aspx" /><generator uri="http://communityserver.org" version="3.0.20611.960">Community Server</generator><updated>2008-10-23T17:21:00Z</updated><entry><title>How to avoid the dreaded 'divide by zero' error</title><link rel="alternate" type="text/html" href="http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/27/how-to-avoid-the-dreaded-divide-by-zero-error.aspx" /><id>http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/27/how-to-avoid-the-dreaded-divide-by-zero-error.aspx</id><published>2008-10-27T14:34:00Z</published><updated>2008-10-27T14:34:00Z</updated><content type="html">&lt;p&gt;As a Dynamics developer, I&amp;#39;m frequently asked to divide by the USCATVLS fields in the IV00101 table. Since they&amp;#39;re CHAR types, there is often no validation on empty values or numeric values. The funtion below will take two VARCHAR values and divide them, returning &amp;#39;0&amp;#39; if there is some validation issue. The code at the end is a test script. &lt;/p&gt;
&lt;p&gt;&lt;pre style="overflow: scroll; background-color: #f2f2f2;"&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;OBJECT_ID&lt;/span&gt; (N&lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;'dbo.f_FP_divide'&lt;/span&gt;) &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;IS&lt;/span&gt; &lt;span style="color: Silver;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;NULL&lt;/span&gt;
   &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;DROP&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;FUNCTION&lt;/span&gt; dbo.f_FP_divide
GO


&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;CREATE&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;FUNCTION&lt;/span&gt; f_FP_divide 
&lt;span style="color: Teal;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;/*
select 
    dbo.f_FP_divide('0','0'),
    dbo.f_FP_divide('a','0'),
    dbo.f_FP_divide('0','a'),
    dbo.f_FP_divide('2','0'),
    dbo.f_FP_divide('0','2'),
    dbo.f_FP_divide('15','2')



*/&lt;/span&gt;


(
    @vchrNum1 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;as&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;varchar&lt;/span&gt;(20),
    @vchrNum2 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;as&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;varchar&lt;/span&gt;(20)
)
RETURNS &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;numeric&lt;/span&gt;(19,5)
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;AS&lt;/span&gt;
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;BEGIN&lt;/span&gt;
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;declare&lt;/span&gt; @nNum1 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;as&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;numeric&lt;/span&gt;(19,5),
        @nNum2 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;as&lt;/span&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;numeric&lt;/span&gt;(19,5)


    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;if&lt;/span&gt; &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;isnumeric&lt;/span&gt;(@vchrNum1) = 0 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;begin&lt;/span&gt;
        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;return&lt;/span&gt; 0
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;end&lt;/span&gt;
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;if&lt;/span&gt; &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;isnumeric&lt;/span&gt;(@vchrNum2) = 0 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;begin&lt;/span&gt;
        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;return&lt;/span&gt; 0
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;end&lt;/span&gt;


    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;select&lt;/span&gt; @nNum1 = @vchrNum1,
        @nNum2 = @vchrNum2


    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;if&lt;/span&gt; @nNum2 = 0 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;begin&lt;/span&gt;
        &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;return&lt;/span&gt; 0
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;end&lt;/span&gt;



    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;return&lt;/span&gt; @nNum1 / @nNum2
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;END&lt;/span&gt;
GO&lt;/span&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;To test:&lt;/p&gt;
&lt;p&gt;&lt;pre style="overflow: scroll; background-color: #f2f2f2;"&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;select&lt;/span&gt; amtwork.sopnumbe, inv.itemnmbr,
        amtwork.qtyfulfi,
        inv.uscatvls_4,
        dbo.f_FP_divide(AMTWORK.QTYFULFI,INV.USCATVLS_4) &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;as&lt;/span&gt; CARTONS
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;from&lt;/span&gt; sop30300 amtwork
        &lt;span style="color: Silver;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;join&lt;/span&gt; iv00101 inv &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;on&lt;/span&gt; inv.itemnmbr = amtwork.itemnmbr 


 &lt;/span&gt;&lt;/pre&gt;&lt;/p&gt;&lt;img src="http://vstoolsforum.com/aggbug.aspx?PostID=1360" width="1" height="1"&gt;</content><author><name>Steve Gray</name><uri>http://vstoolsforum.com/members/Steve-Gray.aspx</uri></author></entry><entry><title>Function to 'Proper' case a string</title><link rel="alternate" type="text/html" href="http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/23/function-to-proper-case-a-string.aspx" /><id>http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/23/function-to-proper-case-a-string.aspx</id><published>2008-10-23T21:53:00Z</published><updated>2008-10-23T21:53:00Z</updated><content type="html">&lt;p&gt;Occasionally I&amp;#39;ll need to format a string in SQL with &amp;#39;Proper&amp;#39; casing. In other words, change &amp;#39;steve gray&amp;#39; to &amp;#39;Steve Gray&amp;#39;. &lt;/p&gt;
&lt;p&gt;Here is a function to do that&lt;/p&gt;
&lt;p&gt;&lt;pre style="overflow: scroll; background-color: #f2f2f2;"&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;CREATE&lt;/span&gt;  &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;FUNCTION&lt;/span&gt; [dbo].[f_FP_Capitalize] 
&lt;span style="color: Teal;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;-- select dbo.f_FP_Capitalize ('steve gray')&lt;/span&gt;
( 
@string &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;VARCHAR&lt;/span&gt;(8000)
)
RETURNS &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;VARCHAR&lt;/span&gt;(8000)
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;AS&lt;/span&gt; 
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;BEGIN&lt;/span&gt;


&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;DECLARE&lt;/span&gt; @Next &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;INT&lt;/span&gt;
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;WHILE&lt;/span&gt; 1=1
       &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;BEGIN&lt;/span&gt;
       &lt;span style="color: Teal;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;--find word space followed by lower case letter&lt;/span&gt;
       &lt;span style="color: Teal;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;--This makes assumptions about the language&lt;/span&gt;
       &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;SELECT&lt;/span&gt; @next= 
           &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;PATINDEX&lt;/span&gt;(&lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;'%[^a-zA-Z][abcdefghijklmnopqurstuvwzyz]%'&lt;/span&gt;,
                       &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;' &amp;lt;a href="mailto:'&lt;/span&gt;+@string"&amp;gt;'+@string&amp;lt;/a&amp;gt;  &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;collate&lt;/span&gt; Latin1_General_CS_AI)
       &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;IF&lt;/span&gt; @next =0 &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;BREAK&lt;/span&gt;
       &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;SELECT&lt;/span&gt; @String = 
           &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;STUFF&lt;/span&gt;(@String,@Next,1,&lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;UPPER&lt;/span&gt;(&lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;SUBSTRING&lt;/span&gt;(@String,@Next,1)))
       &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;END&lt;/span&gt;
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;RETURN&lt;/span&gt; @string
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/p&gt;&lt;img src="http://vstoolsforum.com/aggbug.aspx?PostID=1359" width="1" height="1"&gt;</content><author><name>Steve Gray</name><uri>http://vstoolsforum.com/members/Steve-Gray.aspx</uri></author></entry><entry><title>How to tell if a temp table exists in SQL Server</title><link rel="alternate" type="text/html" href="http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/23/how-to-tell-if-a-temp-table-exists-in-sql-server.aspx" /><id>http://vstoolsforum.com/blogs/sqlserver/archive/2008/10/23/how-to-tell-if-a-temp-table-exists-in-sql-server.aspx</id><published>2008-10-23T21:21:00Z</published><updated>2008-10-23T21:21:00Z</updated><content type="html">&lt;p&gt;Tested in SQL 2000 &amp;amp; 2005:&lt;/p&gt;
&lt;p&gt;&lt;pre style="overflow: scroll; background-color: #f2f2f2;"&gt;&lt;span style="color: Black;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt; &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;IF&lt;/span&gt; &lt;span style="color: Fuchsia;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;OBJECT_ID&lt;/span&gt;(&lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;'tempdb..#tmp'&lt;/span&gt;) &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;IS&lt;/span&gt; &lt;span style="color: Silver;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;NOT&lt;/span&gt; &lt;span style="color: Silver;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;NULL&lt;/span&gt; 
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;PRINT&lt;/span&gt; &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;'exists.'&lt;/span&gt; 
&lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;ELSE&lt;/span&gt; 
    &lt;span style="color: Blue;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;PRINT&lt;/span&gt; &lt;span style="color: Red;background-color: Transparent;font-family: Courier New;font-size: 11px;font-weight: normal;"&gt;'does not exist.'&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/p&gt;&lt;img src="http://vstoolsforum.com/aggbug.aspx?PostID=1358" width="1" height="1"&gt;</content><author><name>Steve Gray</name><uri>http://vstoolsforum.com/members/Steve-Gray.aspx</uri></author></entry></feed>