<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Journey to SQL Authority with Pinal Dave &#187; SQL Function</title>
	<atom:link href="http://blog.sqlauthority.com/category/sql-function/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com</link>
	<description>Notes of a SQL Server MVP and Database Administrator</description>
	<lastBuildDate>Sat, 21 Nov 2009 01:30:19 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='blog.sqlauthority.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/08e35387c05b61340e885b1763a69d9f?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Journey to SQL Authority with Pinal Dave &#187; SQL Function</title>
		<link>http://blog.sqlauthority.com</link>
	</image>
			<item>
		<title>SQL SERVER &#8211; Recently Executed T-SQL Query</title>
		<link>http://blog.sqlauthority.com/2009/10/16/sql-server-recently-executed-t-sql-query/</link>
		<comments>http://blog.sqlauthority.com/2009/10/16/sql-server-recently-executed-t-sql-query/#comments</comments>
		<pubDate>Fri, 16 Oct 2009 01:30:59 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL DMV]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=7073</guid>
		<description><![CDATA[About a year ago, I wrote blog post about SQL SERVER – 2005 – Last Ran Query – Recently Ran Query.  Since, then I have received many question regarding how this is better than fn_get_sql() or DBCC INPUTBUFFER.
The Short Answer in is both of them will be deprecated. 
Please refer to following update query to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7073&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">About a year ago, I wrote blog post about <a href="http://blog.sqlauthority.com/2008/01/03/sql-server-2005-last-ran-query-recently-ran-query/" target="_blank">SQL SERVER – 2005 – Last Ran Query – Recently Ran Query</a>.  Since, then I have received many question regarding how this is better than fn_get_sql() or DBCC INPUTBUFFER.</p>
<p style="text-align:justify;"><em>The Short Answer in is both of them will be deprecated. </em></p>
<p style="text-align:justify;">Please refer to following update query to recently executed T-SQL query on database.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:black;">deqs.last_execution_time </span><span style="color:blue;">AS </span><span style="color:black;">[Time]</span><span style="color:gray;">, </span><span style="color:black;">dest.</span><span style="color:blue;">TEXT AS </span><span style="color:black;">[Query]<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.dm_exec_query_stats </span><span style="color:blue;">AS </span><span style="color:black;">deqs<br />
</span><span style="color:gray;">CROSS </span><span style="color:black;">APPLY sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:black;">deqs.sql_handle</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">dest<br />
</span><span style="color:blue;">ORDER BY </span><span style="color:black;">deqs.last_execution_time </span><span style="color:blue;">DESC</span></code></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
<p style="text-align:justify;">
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL System Table, SQL Tips and Tricks, T SQL, Technology Tagged: SQL DMV <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/7073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/7073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/7073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/7073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/7073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/7073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/7073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/7073/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/7073/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/7073/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=7073&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/10/16/sql-server-recently-executed-t-sql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Insert Values of Stored Procedure in Table &#8211; Use Table Valued Function</title>
		<link>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/</link>
		<comments>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 01:30:24 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6791</guid>
		<description><![CDATA[
I recently got many emails requesting to write a simple article.  I also got a request to explain different ways to insert the values from a stored procedure into a table. Let us quickly look at the conventional way of doing the same.
Please note that this only works with the stored procedure with only one [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6791&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">
<p style="text-align:justify;">I recently got many emails requesting to write a simple article.  I also got a request to explain different ways to insert the values from a stored procedure into a table. Let us quickly look at the conventional way of doing the same.</p>
<p style="text-align:justify;">Please note that this only works with the stored procedure with only one resultset. Let us create a stored procedure that returns one resultset.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Stored Procedure */<br />
</span><span style="color:blue;">CREATE PROCEDURE </span><span style="color:black;">TestSP<br />
</span><span style="color:blue;">AS<br />
SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()+</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
GO</span></code></p>
<p style="text-align:justify;"><strong>Traditional Method:<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create TempTable */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:#434343;">#tempTable </span><span style="color:gray;">(</span><span style="color:black;">MyDate SMALLDATETIME</span><span style="color:gray;">, </span><span style="color:black;">IntValue </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Run SP and Insert Value in TempTable */<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">#tempTable </span><span style="color:gray;">(</span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">IntValue</span><span style="color:gray;">)<br />
</span><span style="color:blue;">EXEC </span><span style="color:black;">TestSP<br />
GO<br />
</span><span style="color:green;">/* SELECT from TempTable */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:#434343;">#tempTable<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Clean up */<br />
</span><span style="color:blue;">DROP TABLE </span><span style="color:#434343;">#tempTable<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><strong>Alternate Method: Table Valued Function<br />
</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create table valued function*/<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.TestFn</span><span style="color:gray;">()<br />
</span><span style="color:blue;">RETURNS </span><span style="color:#434343;">@retTestFn </span><span style="color:blue;">TABLE<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">MyDate SMALLDATETIME</span><span style="color:gray;">,<br />
</span><span style="color:black;">IntValue </span><span style="color:blue;">INT<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@MyDate </span><span style="color:black;">SMALLDATETIME<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@IntValue </span><span style="color:blue;">INT<br />
INSERT INTO </span><span style="color:#434343;">@retTestFn<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()+</span><span style="color:black;">1 </span><span style="color:blue;">AS </span><span style="color:black;">MyDate</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:blue;">AS </span><span style="color:black;">IntValue<br />
</span><span style="color:blue;">RETURN</span><span style="color:gray;">;<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Select data from Table Valued Function */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">dbo.TestFn</span><span style="color:gray;">()<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">It is clear from the resultset that option 2, where I have converted stored procedures logic into the table valued function, is much better in terms of logic as it saves a large number of operations. However, this option should be used carefully. Performance of the stored procedure is “usually” better than that of functions.</p>
<p style="text-align:justify;">We will discuss in another post regarding the type of stored procedure that can be converted into a table valued function. Let me know what you all think about this post.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6791/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6791/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6791/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6791&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/23/sql-server-insert-values-of-stored-procedure-in-table-use-table-valued-function/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Converting Stored Procedure into Table Valued Function</title>
		<link>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/</link>
		<comments>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Optimization]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6798</guid>
		<description><![CDATA[In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">In one of my recent articles, I mentioned the use of Table Valued Function (TVF) instead of Stored Procedure (SP). I received a follow up email asking what type of SP can be converted into a TVF. This is indeed a very interesting question! In fact, not all the SPs qualify to be converted to a TVF.</p>
<p style="text-align:justify;">Please note that I am not encouraging to convert all the SPs to TVFs. Each SPs have their own usage and need. Here, I shall discuss about the type of SP that can be converted to a TVF.</p>
<p style="text-align:justify;">First of all, you need to convert the SP that you are using to retrieve data and to insert it into any other table. Please read my original post wherein I have explained the scenario for the same. If you do not have to insert the data from the SP into another table, then there is no need to convert the SP to a TVF. This conversion will not necessarily enhance the performance.</p>
<p style="text-align:justify;">In general, SP that returns only one resultset qualifies for conversion to TVF. If there are more than one resultset, then the SP cannot be converted to TVF as the TVF always returns one resultset. Moreover, if the SP has DML statements such as DELETE, UPDATE, INSERT or MERGE, it cannot be converted to a TVF. The usage of TVF is limited to the SELECT statement only. If the SP must be executed by using EXEC, it cannot be converted to TVF.</p>
<p style="text-align:justify;">Do you know any other reason why a SP needs to be converted to a TVF?</p>
<p style="text-align:justify;">Reference: <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.sqlauthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Optimization, SQL Performance, SQL Query, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6798/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6798/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6798/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6798&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/09/17/sql-server-converting-stored-procedure-into-table-valued-function/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Time in Hour:Minute Format from a Datetime &#8211; Get Date Part Only from Datetime</title>
		<link>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/</link>
		<comments>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 01:30:17 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5940</guid>
		<description><![CDATA[I have seen scores of expert developers getting perplexed with SQL Server in finding time only from datetime datatype. Let us have a quick glance look at the solution.
SQL Server 2000/2005
SELECT
CONVERT(VARCHAR(8),GETDATE(),108) AS HourMinuteSecond,
CONVERT(VARCHAR(8),GETDATE(),101) AS DateOnly
GO



SQL Server 2008
SELECT
CONVERT(TIME,GETDATE()) AS HourMinuteSecond,
CONVERT(DATE,GETDATE(),101) AS DateOnly
GO



I hope the above solution is clear to you all.
Reference : Pinal Dave (http://blog.SQLAuthority.com)
Posted in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5940&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I have seen scores of expert developers getting perplexed with SQL Server in finding time only from datetime datatype. Let us have a quick glance look at the solution.</p>
<p style="text-align:justify;"><strong>SQL Server 2000/2005</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">108</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">HourMinuteSecond</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">8</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">101</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DateOnly<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetime2005.jpg" alt="" width="489" height="188" /><br />
</span>
</p>
<p style="text-align:justify;"><strong>SQL Server 2008</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:black;">TIME</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()) </span><span style="color:blue;">AS </span><span style="color:black;">HourMinuteSecond</span><span style="color:gray;">,<br />
</span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:black;">DATE</span><span style="color:gray;">,</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">101</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DateOnly<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/datetime2008.jpg" alt="" width="413" height="184" /><br />
</span>
</p>
<p style="text-align:justify;">I hope the above solution is clear to you all.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5940/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5940/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5940/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5940&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/08/06/sql-server-get-time-in-hourminute-format-from-a-datetime-get-date-part-only-from-datetime/feed/</wfw:commentRss>
		<slash:comments>30</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/datetime2005.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/datetime2008.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; List All Missing Identity Values of Table in Database</title>
		<link>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/</link>
		<comments>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/#comments</comments>
		<pubDate>Mon, 27 Jul 2009 01:30:11 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Identity]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=6339</guid>
		<description><![CDATA[The best part of any blog is when readers ask each other questions. Better still, is when a reader takes the time to provide a detailed response.
A few days ago, one of my readers, Yasmin, asked a very interesting question:
How we can find the list of tables whose identity was missed (not is sequential order) [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6339&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">The best part of any blog is when readers ask each other questions. Better still, is when a reader takes the time to provide a detailed response.</p>
<p style="text-align:justify;">A few days ago, one of my readers, <a href="http://blog.sqlauthority.com/2009/04/01/sql-server-reseed-identity-of-table-table-missing-identity-values-gap-in-identity-column/#comment-53353" target="_blank">Yasmin, </a>asked a very interesting question:</p>
<p style="text-align:justify;"><strong><em>How we can find the list of tables whose identity was missed (not is sequential order) within the entire database?</em></strong></p>
<p style="text-align:justify;">A big thank you to SQL Server Expert, <strong>Imran Mohammed</strong>, for his excellent response to this question. He also provided an extremely impressive script, which is well described and contains inline comments.</p>
<p style="text-align:justify;">This script is well worth keeping in your archive for future reference.</p>
<p style="text-align:justify;"><a href="http://www.pinaldave.com/best-sql-server-download.cfm?download=Get_Missing_Identity_Value_Information" target="_blank"><strong>Download Script List All Missing Identity Values of Table in Database</strong></a></p>
<p style="text-align:justify;">If you and use the script, please comment here and let me know about your experience.</p>
<p style="text-align:justify;">If you know any other way that this can be accomplished or have a different solution, please post your comment here, or send me an email and I will publish it with due credit.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Stored Procedure, SQL Tips and Tricks, T SQL, Technology Tagged: Identity <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/6339/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/6339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/6339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=6339&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/27/sql-server-list-all-missing-identity-values-of-table-in-database/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Last Running Query Based on SPID</title>
		<link>http://blog.sqlauthority.com/2009/07/19/sql-server-get-last-running-query-based-on-spid/</link>
		<comments>http://blog.sqlauthority.com/2009/07/19/sql-server-get-last-running-query-based-on-spid/#comments</comments>
		<pubDate>Sun, 19 Jul 2009 01:30:13 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></category>
		<category><![CDATA[SQL System Table]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQLServer]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5934</guid>
		<description><![CDATA[We often need to find the last running query or based on SPID need to know which query was executed. SPID is returns sessions ID of the current user process. The acronym SPID comes from the name of its earlier version, Server Process ID.
To know which sessions are running currently, run the following command:
SELECT @@SPID
GO

In [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5934&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">We often need to find the last running query or based on SPID need to know which query was executed. SPID is returns sessions ID of the current user process. The acronym SPID comes from the name of its earlier version, Server Process ID.</p>
<p style="text-align:justify;">To know which sessions are running currently, run the following command:</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:#434343;">@@SPID<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">In our case, we got SPID 57, which means the session that is running this command has ID of 57.</p>
<p style="text-align:justify;">Now, let us open another session and run the same command. Here we get different IDs for different sessions.</p>
<p style="text-align:justify;">In our case, we got SPID 61. Please note here that this ID may or may not be sequential.</p>
<p style="text-align:justify;">In session with SPID 61, we will run any query. In session with SPID 57, we will see which query was run in session with SPID 61.</p>
<p style="text-align:justify;">Let us run a simple SELECT statement in session with SPID 61 and in session with SPID 57 run the following command.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DBCC </span><span style="color:black;">INPUTBUFFER</span><span style="color:gray;">(</span><span style="color:black;">61</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">Now, here in DBCC command we have passed the SPID of previous session; we will see the text below. The following image illustrates that we get the latest run query in our input buffer.</p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spid1.png" alt="" width="500" height="206" /><br />
</span>
</p>
<p style="text-align:justify;">There are several ways to find out what is the latest run query from system table sys.sysprocesses.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@sqltext </span><span style="color:black;">VARBINARY</span><span style="color:gray;">(</span><span style="color:black;">128</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sqltext </span><span style="color:blue;">= </span><span style="color:black;">sql_handle<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.sysprocesses<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">spid </span><span style="color:blue;">= </span><span style="color:black;">61<br />
</span><span style="color:blue;">SELECT TEXT<br />
FROM </span><span style="color:black;">sys.dm_exec_sql_text</span><span style="color:gray;">(</span><span style="color:#434343;">@sqltext</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span></code>
</p>
<p style="text-align:justify;">The following image portrays that we get the latest run query in our input buffer.</p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spid2.png" alt="" width="500" height="298" /><br />
</span></p>
<p>Please note that in any session there may be multiple running queries, but the buffer only saves the last query and that is what we will be able to retrieve.</p>
<p>There is one more way to achieve the same thing &#8211; using function fn_get_sql
</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">DECLARE </span><span style="color:#434343;">@sqltext </span><span style="color:black;">VARBINARY</span><span style="color:gray;">(</span><span style="color:black;">128</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SELECT </span><span style="color:#434343;">@sqltext </span><span style="color:blue;">= </span><span style="color:black;">sql_handle<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">sys.sysprocesses<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">spid </span><span style="color:blue;">= </span><span style="color:black;">61<br />
</span><span style="color:blue;">SELECT TEXT<br />
FROM </span><span style="color:gray;">::</span><span style="color:darkred;">fn_get_sql</span><span style="color:gray;">(</span><span style="color:#434343;">@sqltext</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO </span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spid3.png" alt="" width="500" height="321" /><br />
</span></p>
<p>All the three methods are same but I always prefer method 2 where I have used sys.sysprocesses.</p>
<p>Today, we have explored a very simple topic. Let me know if you find it useful.
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.sqlauthority.com</a>)</strong></p>
<p style="text-align:justify;">
<p style="text-align:justify;"><span style="color:black;"> </span></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, SQL System Table, SQL Tips and Tricks, SQLServer, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5934/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5934/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5934/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5934/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5934/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5934/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5934/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5934/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5934/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5934/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5934&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/07/19/sql-server-get-last-running-query-based-on-spid/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/spid1.png" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/spid2.png" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/spid3.png" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2005 &#8211; 2008 &#8211; Delete Duplicate Rows</title>
		<link>http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/</link>
		<comments>http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/#comments</comments>
		<pubDate>Tue, 23 Jun 2009 01:30:01 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Duplicate Records]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5631</guid>
		<description><![CDATA[I had previously penned down two popular snippets regarding deleting duplicate rows and counting duplicate rows. Today, we will examine another very quick code snippet where we will delete duplicate rows using CTE and ROW_NUMBER() feature of SQL Server 2005 and SQL Server 2008.
This method is improved over the earlier method as it not only [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5631&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I had previously penned down two popular snippets regarding deleting duplicate rows and counting duplicate rows. Today, we will examine another very quick code snippet where we will delete duplicate rows using CTE and ROW_NUMBER() feature of SQL Server 2005 and SQL Server 2008.</p>
<p style="text-align:justify;">This method is improved over the earlier method as it not only uses CTE and ROW_NUMBER, but also demonstrates the power of CTE with DELETE statement. We will have a comprehensive discussion about it later in this article. For now, let us first create a sample table from which we will delete records.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Create Table with 7 entries - 3 are duplicate entries */<br />
</span><span style="color:blue;">CREATE TABLE </span><span style="color:black;">DuplicateRcordTable </span><span style="color:gray;">(</span><span style="color:black;">Col1 </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:black;">Col2 </span><span style="color:blue;">INT</span><span style="color:gray;">)<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:black;">DuplicateRcordTable<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:green;">--duplicate<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">1 </span><span style="color:green;">--duplicate<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">2<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">2 </span><span style="color:green;">--duplicate<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">3<br />
</span><span style="color:blue;">UNION </span><span style="color:gray;">ALL<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:black;">4<br />
GO</span></code>
</p>
<p style="text-align:justify;">The above table has total 7 records, out of which 3 are duplicate records. Once the duplicates are removed we will have only 4 records left.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* It should give you 7 rows */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">DuplicateRcordTable<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/dup1.png" alt="" width="299" height="290" /><br />
</span>
</p>
<p style="text-align:justify;">The most interesting part of this is yet to come. We will use CTE that will re-generate the same table with additional column, which is row number. In our case, we have Col1 and Col2 and both the columns qualify as duplicate rows. It may be a different set of rows for each different query like this. Another point to note here is that once CTE is created  DELETE statement can be run on it. We will put a condition here &#8211; when we receive more than one rows of record, we will remove the row which is not the first one. When DELETE command is executed over CTE it in fact deletes from the base table used in CTE.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Delete Duplicate records */<br />
</span><span style="color:blue;">WITH </span><span style="color:black;">CTE </span><span style="color:gray;">(</span><span style="color:black;">COl1</span><span style="color:gray;">,</span><span style="color:black;">Col2</span><span style="color:gray;">, </span><span style="color:black;">DuplicateCount</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
</span><span style="color:gray;">(<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">COl1</span><span style="color:gray;">,</span><span style="color:black;">Col2</span><span style="color:gray;">,<br />
</span><span style="color:black;">ROW_NUMBER</span><span style="color:gray;">() </span><span style="color:blue;">OVER</span><span style="color:gray;">(</span><span style="color:black;">PARTITION </span><span style="color:blue;">BY </span><span style="color:black;">COl1</span><span style="color:gray;">,</span><span style="color:black;">Col2 </span><span style="color:blue;">ORDER BY </span><span style="color:black;">Col1</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">DuplicateCount<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">DuplicateRcordTable<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">DELETE<br />
FROM </span><span style="color:black;">CTE<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">DuplicateCount </span><span style="color:gray;">&gt; </span><span style="color:black;">1<br />
GO</span></code>
</p>
<p style="text-align:justify;">It is apparent that after delete command has been run, we will have only 4 records, which is almost the same result which we would have got with DISTINCT, with this resultset. If we had more than 2 columns and we had to run unique on only two columns, our distinct might have not worked here . In this case, we would have to use above the mentioned method.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* It should give you Distinct 4 records */<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">*<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">DuplicateRcordTable<br />
GO</span></code></p>
<p style="text-align:justify;"><span style="color:black;"><img class="alignnone" src="http://www.pinaldave.com/bimg/dup2.png" alt="" width="379" height="228" /><br />
</span>
</p>
<p style="text-align:justify;">This method is a breeze and we can use this for SQL Server version 2005 and the later versions.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Duplicate Records <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5631/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5631/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5631/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5631/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5631/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5631/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5631/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5631/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5631/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5631/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5631&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/23/sql-server-2005-2008-delete-duplicate-rows/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/dup1.png" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/dup2.png" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Connect Item &#8211; Vote for Feature Request Function TRIM</title>
		<link>http://blog.sqlauthority.com/2009/06/03/sql-server-connect-item-vote-for-feature-request-function-trim/</link>
		<comments>http://blog.sqlauthority.com/2009/06/03/sql-server-connect-item-vote-for-feature-request-function-trim/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 01:30:40 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Microsoft Connect]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5366</guid>
		<description><![CDATA[Till date, I have met the SQL Server Product Team twice: first time at SQL Server MVP Meet, Seattle, and second time at TechEd India 2009, Hyderabad. At both the times, I have put forth one request to the product team regarding implementing of function Trim(). As per my opinion, this is the most demanded [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5366&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Till date, I have met the SQL Server Product Team twice: first time at SQL Server MVP Meet, Seattle, and second time at TechEd India 2009, Hyderabad. At both the times, I have put forth one request to the product team regarding implementing of function Trim(). As per my opinion, this is the most demanded feature of SQL Server. Almost all the programming languages have function TRIM() which removes space leading and any word that follows. However, SQL Server does not have TRIM() function. It has LTRIM() and RTRIM() functions, which when combined together LTRIM(RTRIM()) works like the expected TRIM() function &#8211; which I am proposing.</p>
<p style="text-align:justify;">I have submitted the connect item for requesting TRIM() function. I am requesting all the users to vote for connect item and bring it up to the notice of SQL Server Product Group.</p>
<p style="text-align:justify;"><a href="https://connect.microsoft.com/onecare/feedback/ViewFeedback.aspx?FeedbackID=459062" target="_blank"><strong>Microsoft Connect SQL Server &#8211; Feature Request TRIM()</strong></a><strong></strong></p>
<p style="text-align:justify;">If you are a SQL Server DBA you can think of the benefits you will gain after this function is implemented; you will save time and energy and there will be no more frustration! It is our moral duty to spread the word for this item and vote for implementation of this feature.</p>
<p style="text-align:justify;">I had previously written the following three articles, which covers workaround, tricks and enhanced functions related to TRIM().</p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2007/04/24/sql-server-trim-function-udf-trim/" target="_blank"><strong>SQL SERVER – TRIM() Function – UDF TRIM()</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/10/09/sql-server-2008-trim-function-user-defined-function/" target="_blank"><strong>SQL SERVER – 2008 – TRIM() Function – User Defined Function</strong></a></p>
<p style="text-align:justify;"><a href="http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/" target="_blank"><strong>SQL SERVER – 2008 – Enhenced TRIM() Function – Remove Trailing Spaces, Leading Spaces, White Space, Tabs, Carriage Returns, Line Feeds</strong></a></p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Microsoft Connect <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5366/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5366/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5366/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5366&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/06/03/sql-server-connect-item-vote-for-feature-request-function-trim/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Difference Between Candidate Keys and Primary Key</title>
		<link>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/</link>
		<comments>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/#comments</comments>
		<pubDate>Sat, 30 May 2009 01:30:27 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Constraint and Keys]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5208</guid>
		<description><![CDATA[Let us first try to grasp the definition of the two keys.
Candidate Key  &#8211; A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.
Primary Key  &#8211; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5208&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Let us first try to grasp the definition of the two keys.</p>
<p style="text-align:justify;"><strong>Candidate Key </strong> &#8211; A Candidate Key can be any column or a combination of columns that can qualify as unique key in database. There can be multiple Candidate Keys in one table. Each Candidate Key can qualify as Primary Key.</p>
<p style="text-align:justify;"><strong>Primary Key </strong> &#8211; A Primary Key is a column or a combination of columns that uniquely identify a record. Only one Candidate Key can be Primary Key.</p>
<p style="text-align:justify;">One needs to be very careful in selecting the Primary Key as an incorrect selection can adversely impact the database architect and future normalization. For a Candidate Key to qualify as a Primary Key, it should be Non-NULL and unique in any domain. I have observed quite often that Primary Keys are seldom changed. I would like to have your feedback on not changing a Primary Key.</p>
<p style="text-align:justify;">I have <span>illustrates the difference between a candidate key and a primary key in SQL Server.</span></p>
<p style="text-align:justify;">1 Introduction<br />
2 An Example to Understand Keys<br />
2.1 Select a key that does not contain NULL<br />
2.2 Select a key that is unique and does not repeat<br />
2.3 Make sure that Primary Key does not keep changing<br />
3 Selection of Primary Key<br />
4 Quick Note on Other Kinds of Keys<br />
5 Summary</p>
<h3 style="text-align:justify;"><strong><a href="http://dotnetslackers.com/articles/sql/Difference-Between-Candidate-Keys-and-Primary-Key.aspx" target="_blank">Read Complete Article Here</a></strong></h3>
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Constraint and Keys, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5208/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5208/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5208/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5208&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/05/30/sql-server-difference-between-candidate-keys-and-primary-key/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Find Hostname and Current Logged In User Name</title>
		<link>http://blog.sqlauthority.com/2009/05/26/sql-server-find-hostname-and-current-logged-in-user-name/</link>
		<comments>http://blog.sqlauthority.com/2009/05/26/sql-server-find-hostname-and-current-logged-in-user-name/#comments</comments>
		<pubDate>Tue, 26 May 2009 01:30:12 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Security]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Username]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=5166</guid>
		<description><![CDATA[I work in an environment wherein I connect to multiple servers across the world. Time and again, my SSMS is connected to a myriad of servers that kindles a lot of confusion. I frequently use the following trick to separate different connections, which I mentioned in my blog sometime back SQL SERVER – 2008 – [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5166&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I work in an environment wherein I connect to multiple servers across the world. Time and again, my SSMS is connected to a myriad of servers that kindles a lot of confusion. I frequently use the following trick to separate different connections, which I mentioned in my blog sometime back <strong><a href="http://blog.sqlauthority.com/2009/01/04/sql-server-2008-change-color-of-status-bar-of-ssms-query-editor/">SQL SERVER – 2008 – Change Color of Status Bar of SSMS Query Editor</a></strong>. However, this trick does not help when a huge number of different connections are open. In such a case, I use the following handy script. Do not go by the length of the script; it might be very short but always works great!</p>
<p style="text-align:justify;">Now, let’s take a look at the execution of this script in two different scenarios.</p>
<p style="text-align:justify;"><strong>1) Logged in using SQL Authentication</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">HOST_NAME</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">HostName</span><span style="color:gray;">, </span><span style="color:black;">SUSER_NAME</span><span style="color:gray;">() </span><span style="color:black;">LoggedInUser</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/loginsa.jpg" alt="" width="431" height="154" /></p>
<p style="text-align:justify;">
<p style="text-align:justify;"><strong>2) Logged in using Windows Authentication</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">HOST_NAME</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">HostName</span><span style="color:gray;">, </span><span style="color:black;">SUSER_NAME</span><span style="color:gray;">() </span><span style="color:black;">LoggedInUser</span></code></p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/loginwin.jpg" alt="" width="338" height="149" /></p>
<p style="text-align:justify;">
<p style="text-align:justify;">It is quite evident from both the above cases that we get correct logged-in username and hostname. Let me know if this script is helpful to you when you face a similar situation.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Security, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: Username <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/5166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/5166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/5166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/5166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/5166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/5166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/5166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/5166/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/5166/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/5166/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=5166&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/05/26/sql-server-find-hostname-and-current-logged-in-user-name/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/loginsa.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/loginwin.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Leading Zero to Number</title>
		<link>http://blog.sqlauthority.com/2009/04/24/sql-server-leading-zero-to-number/</link>
		<comments>http://blog.sqlauthority.com/2009/04/24/sql-server-leading-zero-to-number/#comments</comments>
		<pubDate>Fri, 24 Apr 2009 02:00:50 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=4520</guid>
		<description><![CDATA[I have received few emails asking how to prefix any number with zero. I have previously written two articles for the same subject. Please refer to my previous articles.
SQL SERVER &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display
SQL SERVER &#8211; UDF &#8211; Pad Ride Side of Number with 0 &#8211; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4520&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I have received few emails asking how to prefix any number with zero. I have previously written two articles for the same subject. Please refer to my previous articles.</p>
<p style="text-align:justify;"><strong><a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/" target="_blank">SQL SERVER &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display</a></strong></p>
<p><strong><a href="http://blog.sqlauthority.com/2009/03/16/sql-server-udf-pad-ride-side-of-number-with-0-fixed-width-number-display/" target="_blank">SQL SERVER &#8211; UDF &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display</a></strong></p>
<p style="text-align:justify;">Let me know if you are aware of any other method.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/4520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/4520/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/4520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/4520/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/4520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/4520/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/4520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/4520/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/4520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/4520/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=4520&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/04/24/sql-server-leading-zero-to-number/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Find All Servers From Local Network &#8211; Using sqlcmd &#8211; Detect Installed SQL Server on Network</title>
		<link>http://blog.sqlauthority.com/2009/03/18/sql-server-find-all-servers-from-local-network-using-sqlcmd/</link>
		<comments>http://blog.sqlauthority.com/2009/03/18/sql-server-find-all-servers-from-local-network-using-sqlcmd/#comments</comments>
		<pubDate>Wed, 18 Mar 2009 01:30:45 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[sqlcmd]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3786</guid>
		<description><![CDATA[I recently had requirement to create list of all the SQL Server on local network. I remembered that I had written similar script a year ago SQL SERVER &#8211; Script to Find SQL Server on Network. When I looked at it, I realize that I had written it for SQL Server 2000 and used &#8220;isql&#8221; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3786&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I recently had requirement to create list of all the SQL Server on local network. I remembered that I had written similar script a year ago <a href="http://blog.sqlauthority.com/2007/04/13/sql-server-script-to-find-sql-server-on-network/" target="_blank">SQL SERVER &#8211; Script to Find SQL Server on Network</a>. When I looked at it, I realize that I had written it for SQL Server 2000 and used &#8220;isql&#8221; utility, which is deprecated now. I quickly wrote down updated script using &#8220;sqlcmd&#8221;. Command &#8220;osql&#8221; still works in SQL Server 2008.</p>
<p style="text-align:justify;">Go to command prompt and type in &#8220;osql -L&#8221; or &#8220;sqlcmd -L&#8221;.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/sqlcmdl.jpg" alt="" width="437" height="333" /></p>
<p style="text-align:justify;">Note one change between osql and sqlcmd is that osql has additional server &#8220;(local)&#8221; listed in the servers list which is in fact same as &#8220;SQL&#8221; in my case. While &#8220;sqlcmd&#8221; gives accurate result.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.sqlauthority.com/" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology Tagged: sqlcmd <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3786/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3786/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3786/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3786&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/18/sql-server-find-all-servers-from-local-network-using-sqlcmd/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/sqlcmdl.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; UDF &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display</title>
		<link>http://blog.sqlauthority.com/2009/03/16/sql-server-udf-pad-ride-side-of-number-with-0-fixed-width-number-display/</link>
		<comments>http://blog.sqlauthority.com/2009/03/16/sql-server-udf-pad-ride-side-of-number-with-0-fixed-width-number-display/#comments</comments>
		<pubDate>Mon, 16 Mar 2009 01:30:42 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3764</guid>
		<description><![CDATA[I had written article SQL SERVER &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display few days ago. Previous article was answer to the email which I had received:
Dear Pinal,
I have situation where I need to display my numbers in fixed format. Like
1 as 0000001
109 as 0000109
0987 as 0000987

Is there any [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3764&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I had written article <strong><a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/" target="_blank">SQL SERVER &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display</a></strong> few days ago. Previous article was answer to the email which I had received:</p>
<p style="padding-left:30px;text-align:justify;"><em>Dear Pinal,</em></p>
<p style="padding-left:30px;text-align:justify;"><em>I have situation where I need to display my numbers in fixed format. Like</em></p>
<p style="padding-left:30px;text-align:justify;"><strong><em>1 as 0000001<br />
109 as 0000109</em><em><br />
0987 as 0000987</em></strong>
</p>
<p style="padding-left:30px;text-align:justify;"><em>Is there any string function in SQL Server which can do this?</em></p>
<p style="text-align:justify;">I had asked my blog readers to participate at the end of the article and I have received three very good suggestions. I am going to list all the three suggestions here. For me all the three suggestions are equally good. I suggest my readers to use any suggestion which they find easy to understand. I suggest to read my <a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/" target="_blank">previous article</a> before continuing this article further.</p>
<p style="text-align:justify;"><strong>Solution 1 by <a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/#comment-48722" target="_blank">Imran Mohammed</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.FnExample<br />
</span><span style="color:gray;">(<br />
</span><span style="color:#434343;">@Size </span><span style="color:blue;">INT</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@Column_Value </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">RETURNS TABLE<br />
AS<br />
RETURN<br />
</span><span style="color:gray;">(<br />
</span><span style="color:green;">-- Add the SELECT statement with parameter references here<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">RIGHT</span><span style="color:gray;">(</span><span style="color:magenta;">REPLICATE</span><span style="color:gray;">(</span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:#434343;">@Size</span><span style="color:gray;">)<br />
+ </span><span style="color:magenta;">CONVERT </span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">), </span><span style="color:#434343;">@Column_Value</span><span style="color:gray;">), </span><span style="color:#434343;">@Size</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:black;">ID<br />
</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Run following script to see output<br />
</span><span style="color:blue;">SELECT </span><span style="color:gray;">* </span><span style="color:blue;">FROM </span><span style="color:black;">dbo.FnExample </span><span style="color:gray;">(</span><span style="color:black;">7</span><span style="color:gray;">, </span><span style="color:black;">123</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;"><strong>Solution 2 by <a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/#comment-48725" target="_blank">Fenil Desai</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">[dbo].[f_pad_before]<br />
</span><span style="color:gray;">( </span><span style="color:#434343;">@string </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">255</span><span style="color:gray;">),<br />
</span><span style="color:#434343;">@desired_length </span><span style="color:black;">INTEGER</span><span style="color:gray;">,<br />
</span><span style="color:#434343;">@pad_character </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">))<br />
</span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">255</span><span style="color:gray;">) </span><span style="color:blue;">AS<br />
BEGIN<br />
</span><span style="color:green;">/* Prefix the required number of spaces to bulk up the string and<br />
then replace the spaces with the desired character */<br />
</span><span style="color:blue;">RETURN </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(<br />
</span><span style="color:magenta;">CASE<br />
</span><span style="color:blue;">WHEN </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@string</span><span style="color:gray;">) &lt; </span><span style="color:#434343;">@desired_length<br />
</span><span style="color:blue;">THEN </span><span style="color:magenta;">REPLACE</span><span style="color:gray;">(</span><span style="color:magenta;">SPACE</span><span style="color:gray;">(</span><span style="color:#434343;">@desired_length </span><span style="color:gray;">- </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@string</span><span style="color:gray;">)), </span><span style="color:red;">' '</span><span style="color:gray;">, </span><span style="color:#434343;">@pad_character</span><span style="color:gray;">) + </span><span style="color:#434343;">@string<br />
</span><span style="color:blue;">ELSE </span><span style="color:#434343;">@string<br />
</span><span style="color:blue;">END<br />
</span><span style="color:gray;">))<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Run following script to see output<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.f_pad_before</span><span style="color:gray;">(</span><span style="color:red;">'123'</span><span style="color:gray;">,</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:red;">'0'</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code></p>
<p><strong>Solution 3 by <a href="http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/#comment-48766" target="_blank">CodeMonkey</a></strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.PadLeft<br />
</span><span style="color:gray;">(</span><span style="color:#434343;">@Value </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@PadWidth </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@PadChar </span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">1</span><span style="color:gray;">))<br />
</span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">255</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
RETURN </span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:magenta;">REPLICATE</span><span style="color:gray;">(</span><span style="color:#434343;">@PadChar</span><span style="color:gray;">, </span><span style="color:#434343;">@PadWidth </span><span style="color:gray;">- </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@Value</span><span style="color:gray;">)), </span><span style="color:red;">''</span><span style="color:gray;">)<br />
+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@Value </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">-- Run following script to see output<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.PadLeft</span><span style="color:gray;">(</span><span style="color:black;">123</span><span style="color:gray;">,</span><span style="color:black;">7</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">Answer of all of the above script will be <strong>&#8216;0000123&#8242;</strong>.</p>
<p style="text-align:justify;">Again, I want to congratulate all three of my reader to participate in this interesting puzzle and would like to encourage other readers to participate in similar puzzles like this.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.sqlauthority.com/" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Best Practices, Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3764/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3764/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3764/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3764&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/16/sql-server-udf-pad-ride-side-of-number-with-0-fixed-width-number-display/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Pad Ride Side of Number with 0 &#8211; Fixed Width Number Display</title>
		<link>http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/</link>
		<comments>http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 01:30:32 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=3719</guid>
		<description><![CDATA[Today we will look something which is very quick and but quite frequently useful string operation over numeric datatype. This article is written based on question asked by one of the user (name not disclosed as per request).
Email was received as following:
Dear Pinal,
I have situation where I need to display my numbers in fixed format. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3719&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Today we will look something which is very quick and but quite frequently useful string operation over numeric datatype. This article is written based on question asked by one of the user (name not disclosed as per request).</p>
<p style="text-align:justify;">Email was received as following:</p>
<p style="padding-left:30px;text-align:justify;"><em>Dear Pinal,</em></p>
<p style="padding-left:30px;text-align:justify;"><em>I have situation where I need to display my numbers in fixed format. Like</em></p>
<p style="padding-left:30px;text-align:justify;"><strong><em>1 as 0000001<br />
109 as 0000109</em><em><br />
0987 as 0000987</em></strong>
</p>
<p style="padding-left:30px;text-align:justify;"><em>Is there any string function in SQL Server which can do this?</em></p>
<p style="text-align:justify;">There is no ready made function in SQL Server which offers this solution but we can quickly write up something very simple. I have use AdventureWorks database for our example purpose. I have noticed that some developers does not use CAST function but I strongly suggest to use it as if CAST function is not used and your column is of numeric datatype it will give errors.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">10 ContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO<br />
</span><span style="color:green;">/* Method 1 Using RIGHT function*/<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">10 </span><span style="color:magenta;">RIGHT</span><span style="color:gray;">(</span><span style="color:red;">'0000000'<br />
</span><span style="color:gray;">+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">ContactID </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">7</span><span style="color:gray;">)), </span><span style="color:black;">7</span><span style="color:gray;">) </span><span style="color:black;">PaddedContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO<br />
</span><span style="color:green;">/* Method 2 Using RIGHT AND REPLICATE function*/<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">10 </span><span style="color:magenta;">RIGHT</span><span style="color:gray;">(</span><span style="color:magenta;">REPLICATE</span><span style="color:gray;">(</span><span style="color:red;">'0'</span><span style="color:gray;">, </span><span style="color:black;">7</span><span style="color:gray;">)<br />
+ </span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:black;">ContactID </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">7</span><span style="color:gray;">)), </span><span style="color:black;">7</span><span style="color:gray;">) </span><span style="color:black;">PaddedContactID<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Contact<br />
GO</span></code>
</p>
<p style="text-align:justify;">Following image demonstrates the result of above two methods.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/pading0.jpg" alt="" width="500" height="772" /></p>
<p style="text-align:justify;">If you prefer you can create UDF for the same. I would encourage my readers to submit the UDF for satisfying following criteria using code provided above.<br />
UDF should take a parameter &#8216;Total Width of Number (in our example it is 7).
</p>
<p style="text-align:justify;">I will publish the reader who posts first correct answer to this UDF on this blog with due credit.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Best Practices, Pinal Dave, Readers Question, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/3719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/3719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/3719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/3719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/3719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/3719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/3719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/3719/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/3719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/3719/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=3719&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/03/10/sql-server-pad-ride-side-of-number-with-0-fixed-width-number-display/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/pading0.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Generate A Single Random Number for Range of Rows of Any Table &#8211; Very interesting Question from Reader</title>
		<link>http://blog.sqlauthority.com/2009/01/28/sql-server-generate-a-single-random-number-for-range-of-rows-of-any-table-very-interesting-question-from-reader/</link>
		<comments>http://blog.sqlauthority.com/2009/01/28/sql-server-generate-a-single-random-number-for-range-of-rows-of-any-table-very-interesting-question-from-reader/#comments</comments>
		<pubDate>Wed, 28 Jan 2009 01:30:43 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Question]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[SQL Random]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2140</guid>
		<description><![CDATA[Just a day ago I received email from reader how to get single random number for range of rows of any table. The question was not very clear to me so I had asked him to send me question in simpler words. He sent me question back in simple words. Let us understand this problem [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2140&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Just a day ago I received email from reader how to get single random number for range of rows of any table. The question was not very clear to me so I had asked him to send me question in simpler words. He sent me question back in simple words. Let us understand this problem using database AdventureWorks.</p>
<p style="text-align:justify;">In AdventureWorks database we have table called Person.Address. How to get single random number generated for PostalCode &#8216;98011&#8242; and another single random number for  PostalCode &#8216;98033&#8242;. So far I have never received scenario like this. I had previously faced situation where I had to genrate new random number for each row of table.</p>
<p style="text-align:justify;">Let us understand two scenarios here.</p>
<p style="text-align:justify;"><strong>Different Random Number for Different Rows (Using NEWID())</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">5 </span><span style="color:magenta;">NEWID</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">GUID</span><span style="color:gray;">, *<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Address<br />
GO</span></code>
</p>
<p style="text-align:justify;"><strong>Single Random Number for Set of Rows (Using RAND())</strong></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">5 </span><span style="color:magenta;">RAND</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">RandomNumber</span><span style="color:gray;">, *<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Address<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">PostalCode </span><span style="color:blue;">= </span><span style="color:red;">'98011'<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT TOP </span><span style="color:black;">5 </span><span style="color:magenta;">RAND</span><span style="color:gray;">() </span><span style="color:blue;">AS </span><span style="color:black;">RandomNumber</span><span style="color:gray;">, *<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Person.Address<br />
</span><span style="color:blue;">WHERE </span><span style="color:black;">PostalCode </span><span style="color:blue;">= </span><span style="color:red;">'98033'<br />
</span><span style="color:black;">GO</span></code>
</p>
<p style="text-align:justify;">I have sent email back to my reader asking his need for single random number for set of rows in real world example but I have not received his reply. I want to bring this to my readers and ask them what do you think can be usage of this concept in real life application.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, Readers Question, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology Tagged: SQL Random <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2140/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2140/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2140/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2140&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/28/sql-server-generate-a-single-random-number-for-range-of-rows-of-any-table-very-interesting-question-from-reader/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL SERVER &#8211; Remove Duplicate Entry from Comma Delimited String &#8211; UDF</title>
		<link>http://blog.sqlauthority.com/2009/01/15/sql-server-remove-duplicate-entry-from-comma-delimited-string-udf/</link>
		<comments>http://blog.sqlauthority.com/2009/01/15/sql-server-remove-duplicate-entry-from-comma-delimited-string-udf/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 01:30:44 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[Readers Contribution]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL String]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2035</guid>
		<description><![CDATA[I love reader&#8217;s contribution this blog as that brings variety in articles. I encourage my readers to provide their contribution and I will publish then with their name.
Blog Reader Ashish Jain has posted very simple script which will remove duplicate entry from comma delimited string. User Defined Function has very simple logic behind it. It [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2035&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">I love reader&#8217;s contribution this blog as that brings variety in articles. I encourage my readers to provide their contribution and I will publish then with their name.</p>
<p style="text-align:justify;">Blog Reader <a href="http://blog.sqlauthority.com/2007/12/30/sql-server-remove-duplicate-characters-from-a-string/#comment-45626" target="_blank"><strong>Ashish Jain</strong></a> has posted very simple script which will remove duplicate entry from comma delimited string. User Defined Function has very simple logic behind it. It takes comma delimited string and then converts it to table and runs DISTINCT operation on the table. DISTINCT operation removes duplicate value. After that it converts the table again into the string and it can be used.</p>
<p style="text-align:justify;">I have modified original contribution from Ashish so now it completely covers the subject intended to cover. I would suggest that this UDF should be kept handy to perform this tedious task easily.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.DistinctList<br />
</span><span style="color:gray;">(<br />
</span><span style="color:#434343;">@List </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">),<br />
</span><span style="color:#434343;">@Delim </span><span style="color:blue;">CHAR<br />
</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURNS<br />
VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@ParsedList </span><span style="color:blue;">TABLE<br />
</span><span style="color:gray;">(<br />
</span><span style="color:black;">Item </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
)<br />
</span><span style="color:blue;">DECLARE </span><span style="color:#434343;">@list1 </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">), </span><span style="color:#434343;">@Pos </span><span style="color:blue;">INT</span><span style="color:gray;">, </span><span style="color:#434343;">@rList </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@list </span><span style="color:blue;">= </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(</span><span style="color:#434343;">@list</span><span style="color:gray;">)) + </span><span style="color:#434343;">@Delim<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@pos </span><span style="color:blue;">= CHARINDEX</span><span style="color:gray;">(</span><span style="color:#434343;">@delim</span><span style="color:gray;">, </span><span style="color:#434343;">@list</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">WHILE </span><span style="color:#434343;">@pos </span><span style="color:gray;">&gt; </span><span style="color:black;">0<br />
</span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@list1 </span><span style="color:blue;">= </span><span style="color:magenta;">LTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">RTRIM</span><span style="color:gray;">(</span><span style="color:magenta;">LEFT</span><span style="color:gray;">(</span><span style="color:#434343;">@list</span><span style="color:gray;">, </span><span style="color:#434343;">@pos </span><span style="color:gray;">- </span><span style="color:black;">1</span><span style="color:gray;">)))<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@list1 </span><span style="color:gray;">&lt;&gt; </span><span style="color:red;">''<br />
</span><span style="color:blue;">INSERT INTO </span><span style="color:#434343;">@ParsedList </span><span style="color:blue;">VALUES </span><span style="color:gray;">(</span><span style="color:magenta;">CAST</span><span style="color:gray;">(</span><span style="color:#434343;">@list1 </span><span style="color:blue;">AS VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)))<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@list </span><span style="color:blue;">= </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@list</span><span style="color:gray;">, </span><span style="color:#434343;">@pos</span><span style="color:gray;">+</span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:magenta;">LEN</span><span style="color:gray;">(</span><span style="color:#434343;">@list</span><span style="color:gray;">))<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@pos </span><span style="color:blue;">= CHARINDEX</span><span style="color:gray;">(</span><span style="color:#434343;">@delim</span><span style="color:gray;">, </span><span style="color:#434343;">@list</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
SELECT </span><span style="color:#434343;">@rlist </span><span style="color:blue;">= </span><span style="color:magenta;">COALESCE</span><span style="color:gray;">(</span><span style="color:#434343;">@rlist</span><span style="color:gray;">+</span><span style="color:red;">','</span><span style="color:gray;">,</span><span style="color:red;">''</span><span style="color:gray;">) + </span><span style="color:black;">item<br />
</span><span style="color:blue;">FROM </span><span style="color:gray;">(</span><span style="color:blue;">SELECT DISTINCT </span><span style="color:black;">Item </span><span style="color:blue;">FROM </span><span style="color:#434343;">@ParsedList</span><span style="color:gray;">) </span><span style="color:black;">t<br />
</span><span style="color:blue;">RETURN </span><span style="color:#434343;">@rlist<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.DistinctList</span><span style="color:gray;">(</span><span style="color:red;">'342,34,456,34,3454,456,aa,bb,cc,aa'</span><span style="color:gray;">,</span><span style="color:red;">','</span><span style="color:gray;">) </span><span style="color:black;">DistinctList<br />
GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/distinctstring.jpg" alt="" width="500" height="569" /></p>
<p style="text-align:justify;">I encourage my readers to send their contribution as well so I can include their contribution as well.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, Readers Contribution, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL String, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2035/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2035/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2035/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2035&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/15/sql-server-remove-duplicate-entry-from-comma-delimited-string-udf/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/distinctstring.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Find Number of Rows and Disk Space Reserved &#8211; Using sp_spaceused Interesting Observation</title>
		<link>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/</link>
		<comments>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/#comments</comments>
		<pubDate>Wed, 14 Jan 2009 01:30:24 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Data Storage]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server DBCC]]></category>
		<category><![CDATA[SQL Stored Procedure]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://blog.sqlauthority.com/?p=2016</guid>
		<description><![CDATA[Previously I posted SQL SERVER &#8211; Find Row Count in Table &#8211; Find Largest Table in Database &#8211; T-SQL. Today we will look into the same issue but with some additional interesting detail.
We can find the row count using another system SP sp_spaceused. This SP gives additional information regarding disk space reserved on database as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2016&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">Previously I posted <strong><a href="http://blog.sqlauthority.com/2008/12/09/sql-server-find-table-rowcount-without-using-t-sql-and-without-opening-table/" target="_blank">SQL SERVER &#8211; Find Row Count in Table &#8211; Find Largest Table in Database &#8211; T-SQL</a></strong>. Today we will look into the same issue but with some additional interesting detail.<br />
We can find the row count using another system SP sp_spaceused. This SP gives additional information regarding disk space reserved on database as well. Well, when I ran the SP on AdventureWorks first time, I suspected that database SP is not providing me correct results.
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spaceused.jpg" alt="" width="330" height="257" /></p>
<p style="text-align:justify;">After a bit investigating I found that it may be possible that due to any reason may be the usage on AdventureWorks database might not be updated. I ran same SP with additional param as shown below and it provided me updated information for the same. Once I ran the SP with @updateusage param, it updated the status of the database. When I ran it again next time without param it gave the same results.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/bimg/spaceused1.jpg" alt="" width="367" height="244" /></p>
<p style="text-align:justify;">The reason for above behavior is when dellocation of space happened in database (e.g. dropping table or truncating table) it does not release the used space right away. However, when @updateusage param is used database engine scans whole database and update the sapce usage in respective tables.<br />
Let me know if you guys have any interesting observation like this.
</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (<a href="http://blog.SQLAuthority.com" target="_blank">http://blog.SQLAuthority.com</a>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Data Storage, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL Server DBCC, SQL Stored Procedure, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/2016/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/2016/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/2016/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=2016&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2009/01/14/sql-server-find-number-of-rows-and-disk-space-reserved-using-sp_spaceused-interesting-observation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/bimg/spaceused.jpg" medium="image" />

		<media:content url="http://www.pinaldave.com/bimg/spaceused1.jpg" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice &#8211; Part 2</title>
		<link>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/</link>
		<comments>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 01:30:28 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL DateTime]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1383</guid>
		<description><![CDATA[A year ago I wrote post about SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice where I have discussed two different methods of getting datepart from datetime.

Method 1:
SELECT DATEADD(D, 0, DATEDIFF(D, 0, GETDATE()))
Method 2:
SELECT CONVERT(VARCHAR(10),GETDATE(),111)

I have summarized my post suggesting that either method works fine and I prefer to [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1383&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">A year ago I wrote post about <a href="http://blog.sqlauthority.com/2007/06/10/sql-server-retrieve-select-only-date-part-from-datetime-best-practice/" target="_blank">SQL SERVER &#8211; Retrieve &#8211; Select Only Date Part From DateTime &#8211; Best Practice</a> where I have discussed two different methods of getting datepart from datetime.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/datetimecs2.gif" alt="" width="382" height="395" /></p>
<p style="text-align:justify;">Method 1:<br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">D</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">D</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">GETDATE</span><span style="color:gray;">()))</span></code></p>
<p style="text-align:justify;">Method 2:<br />
<code style="font-size:12px;"><span style="color:blue;">SELECT </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:magenta;">GETDATE</span><span style="color:gray;">(),</span><span style="color:black;">111</span><span style="color:gray;">)</span></code>
</p>
<p style="text-align:justify;">I have summarized my post suggesting that either method works fine and I prefer to use Method 2. However, with additional tests and looking at SQL Server internals very carefully, I want to suggest that Method 1 is better in terms of performance. While running on GETDATE() both of the above functions are equally fast and efficient. However, when above query is ran on very large data table performance different is visible.</p>
<p style="text-align:justify;">For testing purpose I have ran few queries where I am getting same result.</p>
<p style="text-align:justify;">Run Following query 10 times with client statistics on ( Shift+Alter+S) in SSMS.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">DATEADD</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:magenta;">DATEDIFF</span><span style="color:gray;">(</span><span style="color:black;">d</span><span style="color:gray;">, </span><span style="color:black;">0</span><span style="color:gray;">, </span><span style="color:black;">StartDate</span><span style="color:gray;">))<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.WorkOrder<br />
GO</span></code>
</p>
<p style="text-align:justify;">Now run another query 10 times with client statistics on.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">USE </span><span style="color:black;">AdventureWorks<br />
GO<br />
</span><span style="color:blue;">SELECT </span><span style="color:magenta;">CONVERT</span><span style="color:gray;">(</span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">),</span><span style="color:black;">StartDate</span><span style="color:gray;">,</span><span style="color:black;">111</span><span style="color:gray;">)<br />
</span><span style="color:blue;">FROM </span><span style="color:black;">Production.WorkOrder<br />
GO</span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/datetimecs1.gif" alt="" width="382" height="395" /></p>
<p style="text-align:justify;">While looking at the average of statistics in it is clear that first query with DATEADD is much more efficient.</p>
<p style="text-align:justify;">Let us compare both of the above script together and see the client statistics.</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/datetimecs.gif" alt="" width="549" height="580" /></p>
<p style="text-align:justify;">While comparing Numbers of Bytes sent across client and server as well client processing time, and total execution time in all case DATEADD is better. I suggest all my readers to use DATEADD when only DATEPART is required.</p>
<p style="text-align:justify;">If you are using SQL Server 2008 it has new DATE datatype which does not have time part. I suggest to use DATE datatype or TIME datatype for users of SQL Server 2008. Read more <a href="http://blog.sqlauthority.com/2007/12/23/sql-server-2008-new-datatypes-date-and-time/" target="_blank">SQL SERVER &#8211; 2008 &#8211; New DataTypes DATE and TIME</a><a href="http://blog.sqlauthority.com/2007/12/23/sql-server-2008-new-datatypes-date-and-time/" target="_blank">.</a></p>
<p style="text-align:justify;">If you want your date in any other format refer the UDF <a href="http://blog.sqlauthority.com/2008/08/14/sql-server-get-date-time-in-any-format-udf-user-defined-functions/" target="_blank">SQL SERVER &#8211; Get Date Time in Any Format &#8211; UDF &#8211; User Defined Functions</a>.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Best Practices, Pinal Dave, SQL, SQL Authority, SQL DateTime, SQL Function, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1383/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1383/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1383/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1383&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/18/sql-server-retrieve-select-only-date-part-from-datetime-best-practice-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/blogfolder/datetimecs2.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/datetimecs1.gif" medium="image" />

		<media:content url="http://www.pinaldave.com/blogfolder/datetimecs.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; Get Numeric Value From Alpha Numeric String &#8211; UDF for Get Numeric Numbers Only</title>
		<link>http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/</link>
		<comments>http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 01:30:40 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL String]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1360</guid>
		<description><![CDATA[SQL is great with String operations. Many times, I use T-SQL to do my string operation. Let us see User Defined Function, which I wrote few days ago, which will return only Numeric values from AlphaNumeric values.
CREATE FUNCTION dbo.udf_GetNumeric
(@strAlphaNumeric VARCHAR(256))
RETURNS VARCHAR(256)
AS
BEGIN
DECLARE @intAlpha INT
SET @intAlpha = PATINDEX('%[^0-9]%', @strAlphaNumeric)
BEGIN
WHILE @intAlpha &#62; 0
BEGIN
SET @strAlphaNumeric = STUFF(@strAlphaNumeric, @intAlpha, 1, '' )
SET [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1360&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">SQL is great with String operations. Many times, I use T-SQL to do my string operation. Let us see User Defined Function, which I wrote few days ago, which will return only Numeric values from AlphaNumeric values.</p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.udf_GetNumeric<br />
</span><span style="color:gray;">(</span><span style="color:#434343;">@strAlphaNumeric </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">256</span><span style="color:gray;">))<br />
</span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">256</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@intAlpha </span><span style="color:blue;">INT<br />
SET </span><span style="color:#434343;">@intAlpha </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^0-9]%'</span><span style="color:gray;">, </span><span style="color:#434343;">@strAlphaNumeric</span><span style="color:gray;">)<br />
</span><span style="color:blue;">BEGIN<br />
WHILE </span><span style="color:#434343;">@intAlpha </span><span style="color:gray;">&gt; </span><span style="color:black;">0<br />
</span><span style="color:blue;">BEGIN<br />
SET </span><span style="color:#434343;">@strAlphaNumeric </span><span style="color:blue;">= </span><span style="color:magenta;">STUFF</span><span style="color:gray;">(</span><span style="color:#434343;">@strAlphaNumeric</span><span style="color:gray;">, </span><span style="color:#434343;">@intAlpha</span><span style="color:gray;">, </span><span style="color:black;">1</span><span style="color:gray;">, </span><span style="color:red;">'' </span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@intAlpha </span><span style="color:blue;">= </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^0-9]%'</span><span style="color:gray;">, </span><span style="color:#434343;">@strAlphaNumeric </span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
END<br />
RETURN </span><span style="color:magenta;">ISNULL</span><span style="color:gray;">(</span><span style="color:#434343;">@strAlphaNumeric</span><span style="color:gray;">,</span><span style="color:black;">0</span><span style="color:gray;">)<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO</span></code></p>
<p style="text-align:justify;"><code style="font-size:12px;"><span style="color:green;">/* Run the UDF with different test values */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_GetNumeric</span><span style="color:gray;">(</span><span style="color:red;">''</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:red;">'EmptyString'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_GetNumeric</span><span style="color:gray;">(</span><span style="color:red;">'asdf1234a1s2d3f4@@@'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:red;">'asdf1234a1s2d3f4@@@'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_GetNumeric</span><span style="color:gray;">(</span><span style="color:red;">'123456'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:red;">'123456'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_GetNumeric</span><span style="color:gray;">(</span><span style="color:red;">'asdf'</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:red;">'asdf'</span><span style="color:gray;">;<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.udf_GetNumeric</span><span style="color:gray;">(NULL) </span><span style="color:blue;">AS </span><span style="color:red;">'NULL'</span><span style="color:gray;">;<br />
</span><span style="color:black;">GO </span></code>
</p>
<p style="text-align:justify;"><img class="alignnone" src="http://www.pinaldave.com/blogfolder/parseint.gif" alt="" width="297" height="383" /></p>
<p style="text-align:justify;">As result of above script self explainary, I will be not describing it in detail. If you use any alternative method to do the same task, please let me know.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Query, SQL Scripts, SQL Server, SQL String, SQL Tips and Tricks, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1360/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1360/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1360/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1360&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/14/sql-server-get-numeric-value-from-alpha-numeric-string-udf-for-get-numeric-numbers-only/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>

		<media:content url="http://www.pinaldave.com/blogfolder/parseint.gif" medium="image" />
	</item>
		<item>
		<title>SQL SERVER &#8211; 2008 &#8211; Enhenced TRIM() Function &#8211; Remove Trailing Spaces, Leading Spaces, White Space, Tabs, Carriage Returns, Line Feeds</title>
		<link>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/</link>
		<comments>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 01:30:54 +0000</pubDate>
		<dc:creator>pinaldave</dc:creator>
				<category><![CDATA[Pinal Dave]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Authority]]></category>
		<category><![CDATA[SQL Function]]></category>
		<category><![CDATA[SQL Performance]]></category>
		<category><![CDATA[SQL Query]]></category>
		<category><![CDATA[SQL Scripts]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL String]]></category>
		<category><![CDATA[SQL Tips and Tricks]]></category>
		<category><![CDATA[SQL Utility]]></category>
		<category><![CDATA[T SQL]]></category>
		<category><![CDATA[Technology]]></category>

		<guid isPermaLink="false">http://sqlauthority.wordpress.com/?p=1346</guid>
		<description><![CDATA[After reading my article SQL SERVER &#8211; 2008 &#8211; TRIM() Function &#8211; User Defined Function, I have received email and comments where user are asking if it is possible to remove trailing spaces, leading spaces, white space, tabs, carriage returns, line feeds etc.
I found following script posted by Russ and Erik. It is modified a [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1346&subd=sqlauthority&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p style="text-align:justify;">After reading my article <strong><a href="http://blog.sqlauthority.com/2008/10/09/sql-server-2008-trim-function-user-defined-function/" target="_blank">SQL SERVER &#8211; 2008 &#8211; TRIM() Function &#8211; User Defined Function</a></strong>, I have received email and comments where user are asking if it is possible to remove trailing spaces, leading spaces, white space, tabs, carriage returns, line feeds etc.</p>
<p style="text-align:justify;">I found following script posted by <a href="http://blog.sqlauthority.com/2007/04/24/sql-server-trim-function-udf-trim/#comment-35417" target="_blank">Russ </a>and <a href="http://blog.sqlauthority.com/2007/04/24/sql-server-trim-function-udf-trim/#comment-42900" target="_blank">Erik</a>. It is modified a bit from original script.</p>
<p><code style="font-size:12px;"><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.LTrimX</span><span style="color:gray;">(</span><span style="color:#434343;">@str </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)) </span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@trimchars </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@trimchars </span><span style="color:blue;">= CHAR</span><span style="color:gray;">(</span><span style="color:black;">9</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">13</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">32</span><span style="color:gray;">)<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@str </span><span style="color:gray;">LIKE </span><span style="color:red;">'[' </span><span style="color:gray;">+ </span><span style="color:#434343;">@trimchars </span><span style="color:gray;">+ </span><span style="color:red;">']%' </span><span style="color:blue;">SET </span><span style="color:#434343;">@str </span><span style="color:blue;">= </span><span style="color:magenta;">SUBSTRING</span><span style="color:gray;">(</span><span style="color:#434343;">@str</span><span style="color:gray;">, </span><span style="color:magenta;">PATINDEX</span><span style="color:gray;">(</span><span style="color:red;">'%[^' </span><span style="color:gray;">+ </span><span style="color:#434343;">@trimchars </span><span style="color:gray;">+ </span><span style="color:red;">']%'</span><span style="color:gray;">, </span><span style="color:#434343;">@str</span><span style="color:gray;">), </span><span style="color:black;">8000</span><span style="color:gray;">)<br />
</span><span style="color:blue;">RETURN </span><span style="color:#434343;">@str<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.RTrimX</span><span style="color:gray;">(</span><span style="color:#434343;">@str </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)) </span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
DECLARE </span><span style="color:#434343;">@trimchars </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@trimchars </span><span style="color:blue;">= CHAR</span><span style="color:gray;">(</span><span style="color:black;">9</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">10</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">13</span><span style="color:gray;">)+</span><span style="color:blue;">CHAR</span><span style="color:gray;">(</span><span style="color:black;">32</span><span style="color:gray;">)<br />
</span><span style="color:blue;">IF </span><span style="color:#434343;">@str </span><span style="color:gray;">LIKE </span><span style="color:red;">'%[' </span><span style="color:gray;">+ </span><span style="color:#434343;">@trimchars </span><span style="color:gray;">+ </span><span style="color:red;">']'<br />
</span><span style="color:blue;">SET </span><span style="color:#434343;">@str </span><span style="color:blue;">= </span><span style="color:magenta;">REVERSE</span><span style="color:gray;">(</span><span style="color:black;">dbo.LTrimX</span><span style="color:gray;">(</span><span style="color:magenta;">REVERSE</span><span style="color:gray;">(</span><span style="color:#434343;">@str</span><span style="color:gray;">)))<br />
</span><span style="color:blue;">RETURN </span><span style="color:#434343;">@str<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:blue;">CREATE FUNCTION </span><span style="color:black;">dbo.TrimX</span><span style="color:gray;">(</span><span style="color:#434343;">@str </span><span style="color:blue;">VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)) </span><span style="color:blue;">RETURNS VARCHAR</span><span style="color:gray;">(</span><span style="color:blue;">MAX</span><span style="color:gray;">)<br />
</span><span style="color:blue;">AS<br />
BEGIN<br />
RETURN </span><span style="color:black;">dbo.LTrimX</span><span style="color:gray;">(</span><span style="color:black;">dbo.RTrimX</span><span style="color:gray;">(</span><span style="color:#434343;">@str</span><span style="color:gray;">))<br />
</span><span style="color:blue;">END<br />
</span><span style="color:black;">GO<br />
</span><span style="color:green;">/* Run the created function */<br />
</span><span style="color:blue;">SELECT </span><span style="color:black;">dbo.TRIMX</span><span style="color:gray;">(</span><span style="color:red;">'         word leading trailing spaces           '</span><span style="color:gray;">) </span><span style="color:blue;">AS </span><span style="color:red;">'TrimmedWord'<br />
</span><span style="color:black;">GO </span></code></p>
<p style="text-align:justify;">Running above function will give you result where there is no white spaces or any other while space like chars.</p>
<p style="text-align:justify;">Let me know what you think about this and if this can be improved further.</p>
<p style="text-align:justify;">Reference : <strong>Pinal Dave (</strong><a href="http://blog.SQLAuthority.com" target="_blank"><strong>http://blog.SQLAuthority.com</strong></a><strong>)</strong></p>
Posted in Pinal Dave, SQL, SQL Authority, SQL Function, SQL Performance, SQL Query, SQL Scripts, SQL Server, SQL String, SQL Tips and Tricks, SQL Utility, T SQL, Technology  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlauthority.wordpress.com/1346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlauthority.wordpress.com/1346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/sqlauthority.wordpress.com/1346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/sqlauthority.wordpress.com/1346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/sqlauthority.wordpress.com/1346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/sqlauthority.wordpress.com/1346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/sqlauthority.wordpress.com/1346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/sqlauthority.wordpress.com/1346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/sqlauthority.wordpress.com/1346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/sqlauthority.wordpress.com/1346/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.sqlauthority.com&blog=668536&post=1346&subd=sqlauthority&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://blog.sqlauthority.com/2008/10/10/sql-server-2008-enhenced-trim-function-remove-trailing-spaces-leading-spaces-white-space-tabs-carriage-returns-line-feeds/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/48aa5a2264e8a27d802bb22ab6ccf688?s=96&#38;d=identicon" medium="image">
			<media:title type="html">pinaldave</media:title>
		</media:content>
	</item>
	</channel>
</rss>