<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8158036542551301938</id><updated>2011-11-28T11:12:15.880-08:00</updated><category term='Java Script Coding'/><category term='AJAX'/><category term='PHP Coding'/><category term='HTML Coding'/><category term='SQL'/><category term='CSS'/><category term='JQuery Coding'/><category term='SQL Table'/><title type='text'>Micro Codes - "Coding Schools"</title><subtitle type='html'>'Online Coding' for PHP Coding,MySql Coding, HTML Coding,XML Coding, Ajax Coding, JQuery Coding and many more..</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>82</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-1748548492153848546</id><published>2011-07-07T02:01:00.001-07:00</published><updated>2011-07-07T02:01:22.062-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='PHP Coding'/><title type='text'>Sending Group Mail via SMTP in PHP</title><content type='html'>&lt;p&gt;This tutorial will you to send Email via SMTP using PHP coding. First we have to declare and assign the value for variables like First Name, Last Name, Email id to whom we need to send the email&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;&lt;a href="http://lh4.ggpht.com/-9twEjxg9NU8/ThV13UmdrSI/AAAAAAAABug/AMGjBskeP1U/s1600-h/SMTPmail%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SMTPmail" border="0" alt="SMTPmail" src="http://lh3.ggpht.com/-qZH4gUuhczg/ThV14JwBmNI/AAAAAAAABuk/FjwWUyxJfQo/SMTPmail_thumb%25255B1%25255D.png?imgmax=800" width="395" height="190" /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;p&gt;&lt;strong&gt;index.php&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;$to=&amp;quot;user@gmail.com&amp;quot;;     &lt;br /&gt;$fn=&amp;quot;Fisrt Name&amp;quot;;      &lt;br /&gt;$ln=&amp;quot;Last Name&amp;quot;;      &lt;br /&gt;$name=$fn.' '.$ln;      &lt;br /&gt;$from=&amp;quot;support@website.com&amp;quot;;      &lt;br /&gt;$subject = &amp;quot;Welcome to Website&amp;quot;;      &lt;br /&gt;$message = &amp;quot;Dear $name,       &lt;br /&gt;Your Welcome Message.      &lt;br /&gt;Thanks      &lt;br /&gt;www.website.com      &lt;br /&gt;&amp;quot;;      &lt;br /&gt;include('smtpwork.php');&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now we will configure the SMTP, &lt;strong&gt;smtp.php&lt;/strong&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;&amp;lt;?php&lt;/b&gt;      &lt;br /&gt;require(&amp;quot;smtp.php&amp;quot;);      &lt;br /&gt;require(&amp;quot;sasl.php&amp;quot;); //SASL authentication      &lt;br /&gt;$from=&amp;quot;support@yourwebsite.com&amp;quot;;       &lt;br /&gt;$smtp=new smtp_class;      &lt;br /&gt;$smtp-&amp;gt;host_name=&amp;quot;www.website.com&amp;quot;; // Or IP address      &lt;br /&gt;$smtp-&amp;gt;host_port=25;      &lt;br /&gt;$smtp-&amp;gt;ssl=0;      &lt;br /&gt;$smtp-&amp;gt;start_tls=0;      &lt;br /&gt;$smtp-&amp;gt;localhost=&amp;quot;localhost&amp;quot;;      &lt;br /&gt;$smtp-&amp;gt;direct_delivery=0;      &lt;br /&gt;$smtp-&amp;gt;timeout=10;      &lt;br /&gt;$smtp-&amp;gt;data_timeout=0;      &lt;br /&gt;$smtp-&amp;gt;debug=1;      &lt;br /&gt;$smtp-&amp;gt;html_debug=1;      &lt;br /&gt;$smtp-&amp;gt;pop3_auth_host=&amp;quot;&amp;quot;;      &lt;br /&gt;$smtp-&amp;gt;user=&amp;quot;support@website.com&amp;quot;; // SMTP Username      &lt;br /&gt;$smtp-&amp;gt;realm=&amp;quot;&amp;quot;;      &lt;br /&gt;$smtp-&amp;gt;password=&amp;quot;password&amp;quot;; // SMTP Password      &lt;br /&gt;$smtp-&amp;gt;workstation=&amp;quot;&amp;quot;;      &lt;br /&gt;$smtp-&amp;gt;authentication_mechanism=&amp;quot;&amp;quot;;       &lt;br /&gt;&lt;b&gt;if&lt;/b&gt;($smtp-&amp;gt;SendMessage(      &lt;br /&gt;$from,      &lt;br /&gt;array(      &lt;br /&gt;$to      &lt;br /&gt;),      &lt;br /&gt;array(      &lt;br /&gt;&amp;quot;From: $from&amp;quot;,      &lt;br /&gt;&amp;quot;To: $to&amp;quot;,      &lt;br /&gt;&amp;quot;Subject: $subject&amp;quot;,      &lt;br /&gt;&amp;quot;Date: &amp;quot;.strftime(&amp;quot;%a, %d %b %Y %H:%M:%S %Z&amp;quot;)      &lt;br /&gt;),      &lt;br /&gt;&amp;quot;$message&amp;quot;))      &lt;br /&gt;{      &lt;br /&gt;echo &amp;quot;Message sent to $to OK.&amp;quot;;       &lt;br /&gt;}      &lt;br /&gt;else      &lt;br /&gt;echo &amp;quot;Cound not seend the message to $to.\nError: &amp;quot;.$smtp-&amp;gt;error;      &lt;br /&gt;&lt;b&gt;?&amp;gt;&lt;/b&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;That’s all now we can send email to all&lt;/p&gt;  &lt;p&gt;&lt;a title="SMTP files using PHP" href="http://www.mediafire.com/?27jqp5r3w75jvfv" target="_blank"&gt;Download SMTP files using PHP&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-1748548492153848546?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/1748548492153848546/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=1748548492153848546&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1748548492153848546'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1748548492153848546'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/07/sending-group-mail-via-smtp-in-php.html' title='Sending Group Mail via SMTP in PHP'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-qZH4gUuhczg/ThV14JwBmNI/AAAAAAAABuk/FjwWUyxJfQo/s72-c/SMTPmail_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-425885290020693880</id><published>2011-07-03T03:07:00.000-07:00</published><updated>2011-07-03T03:30:52.957-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='JQuery Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Image Cropping using JQuery &amp; PHP Coding</title><content type='html'>&lt;p&gt;Today we are going to see how to crop the Image as we need using JQuery in PHP and we are going to store that cropped image in MySql database.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Here is the sample Screen Shot&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-Fa483sjpIQ8/ThBE1YkR_iI/AAAAAAAABuY/takey7D_Otc/s1600-h/cropimage%25255B4%25255D.jpg"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="cropimage" border="0" alt="cropimage" src="http://lh3.ggpht.com/-uqTsCxYAt8o/ThBE2lGcnZI/AAAAAAAABuc/gMqp1_rcD-I/cropimage_thumb%25255B1%25255D.jpg?imgmax=800" width="368" height="174" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Create the MySql database with needed information like user id, username, password, email, profile image, profile image small for cropped image.&lt;/p&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;blockquote&gt;   &lt;p&gt;CREATE TABLE `&lt;b&gt;users&lt;/b&gt;` (      &lt;br /&gt;`uid` int(11) AUTO_INCREMENT PRIMARY KEY,      &lt;br /&gt;`username` varchar(255) UNIQUE KEY,      &lt;br /&gt;`password` varchar(100),      &lt;br /&gt;`email` varchar(255) UNIQUE KEY,      &lt;br /&gt;`profile_image` varchar(200),      &lt;br /&gt;`profile_image_small` varchar(200),      &lt;br /&gt;)&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now using PHP coding we are going to write the upload script to upload the image and by using HTML coding we are going to design the upload box and so on.&lt;/p&gt;  &lt;p&gt;First we need include the database using include function and then we need to create the session for particular user and we need to specify the file format which it will accept and update the image in database.   &lt;br /&gt;&lt;/p&gt;  &lt;h5&gt;index.php&lt;/h5&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;&amp;lt;?php&lt;/b&gt;      &lt;br /&gt;include('db.php');      &lt;br /&gt;session_start();      &lt;br /&gt;$session_id=$_SESSION['user'];//&amp;#160; Session ID      &lt;br /&gt;$path = &amp;quot;uploads/&amp;quot;;      &lt;br /&gt;$valid_formats = array(&amp;quot;jpg&amp;quot;, &amp;quot;png&amp;quot;, &amp;quot;gif&amp;quot;, &amp;quot;bmp&amp;quot;);      &lt;br /&gt;if(isset($_POST['submit']))      &lt;br /&gt;{      &lt;br /&gt;$name = $_FILES['photoimg']['name'];      &lt;br /&gt;$size = $_FILES['photoimg']['size'];      &lt;br /&gt;if(strlen($name))      &lt;br /&gt;{      &lt;br /&gt;list($txt, $ext) = explode(&amp;quot;.&amp;quot;, $name);      &lt;br /&gt;if(in_array($ext,$valid_formats) &amp;amp;&amp;amp; $size&amp;lt;(250*1024))      &lt;br /&gt;{      &lt;br /&gt;$actual_image_name = time().substr($txt, 5).&amp;quot;.&amp;quot;.$ext;      &lt;br /&gt;$tmp = $_FILES['photoimg']['tmp_name'];      &lt;br /&gt;if(move_uploaded_file($tmp, $path.$actual_image_name))      &lt;br /&gt;{      &lt;br /&gt;mysql_query(&amp;quot;UPDATE users SET profile_image='$actual_image_name' WHERE uid='$session_id'&amp;quot;);      &lt;br /&gt;$image=&amp;quot;&amp;lt;h1&amp;gt;Please drag on the image&amp;lt;/h1&amp;gt;&amp;lt;img src='uploads/&amp;quot;.$actual_image_name.&amp;quot;' id=\&amp;quot;photo\&amp;quot; &amp;quot;;      &lt;br /&gt;}      &lt;br /&gt;else      &lt;br /&gt;echo &amp;quot;failed&amp;quot;;      &lt;br /&gt;}      &lt;br /&gt;else      &lt;br /&gt;echo &amp;quot;Invalid file formats..!&amp;quot;;       &lt;br /&gt;}      &lt;br /&gt;else      &lt;br /&gt;echo &amp;quot;Please select image..!&amp;quot;;      &lt;br /&gt;}      &lt;br /&gt;&lt;b&gt;?&amp;gt;&lt;/b&gt;      &lt;br /&gt;//HTML Body      &lt;br /&gt;&lt;b&gt;&amp;lt;?php&lt;/b&gt; echo $image; &lt;b&gt;?&amp;gt;&lt;/b&gt;      &lt;br /&gt;&amp;lt;div id=&amp;quot;thumbs&amp;quot; &amp;gt;&amp;lt;/div&amp;gt;      &lt;br /&gt;&amp;lt;div &amp;gt;      &lt;br /&gt;&amp;lt;form id=&amp;quot;cropimage&amp;quot; method=&amp;quot;post&amp;quot; enctype=&amp;quot;multipart/form-data&amp;quot;&amp;gt;      &lt;br /&gt;&lt;b&gt;Upload your image&lt;/b&gt; &amp;lt;input type=&amp;quot;file&amp;quot; name=&amp;quot;photoimg&amp;quot; id=&amp;quot;photoimg&amp;quot; /&amp;gt;      &lt;br /&gt;&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;image_name&amp;quot; id=&amp;quot;image_name&amp;quot; value=&amp;quot;&lt;b&gt;&amp;lt;?php&lt;/b&gt; echo($actual_image_name)&lt;b&gt;?&amp;gt;&lt;/b&gt;&amp;quot; /&amp;gt;      &lt;br /&gt;&amp;lt;input type=&amp;quot;submit&amp;quot; name=&amp;quot;submit&amp;quot; value=&amp;quot;Submit&amp;quot; /&amp;gt;&amp;#160; &lt;br /&gt;&amp;lt;/form&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;Java Script Coding&lt;/h5&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://ajax.googleapis.com/ajax/libs/jquery/     &lt;br /&gt;1.4.2/jquery.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;      &lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;scripts/jquery.imgareaselect.pack.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;      &lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;      &lt;br /&gt;function getSizes(im,obj)      &lt;br /&gt;{      &lt;br /&gt;var x_axis = obj.x1;      &lt;br /&gt;var x2_axis = obj.x2;      &lt;br /&gt;var y_axis = obj.y1;      &lt;br /&gt;var y2_axis = obj.y2;      &lt;br /&gt;var thumb_width = obj.width;      &lt;br /&gt;var thumb_height = obj.height;      &lt;br /&gt;if(thumb_width &amp;gt; 0)      &lt;br /&gt;{      &lt;br /&gt;if(confirm(&amp;quot;Do you want to save image..!&amp;quot;))      &lt;br /&gt;{      &lt;br /&gt;$.ajax({      &lt;br /&gt;type:&amp;quot;GET&amp;quot;,      &lt;br /&gt;url:&amp;quot;ajax_image.php?t=ajax&amp;amp;img=&amp;quot;+$(&amp;quot;#image_name&amp;quot;).val()+&amp;quot;&amp;amp;w=&amp;quot;+      &lt;br /&gt;thumb_width+&amp;quot;&amp;amp;h=&amp;quot;+thumb_height+&amp;quot;&amp;amp;x1=&amp;quot;+x_axis+&amp;quot;&amp;amp;y1=&amp;quot;+y_axis,      &lt;br /&gt;cache:&lt;b&gt;false&lt;/b&gt;,      &lt;br /&gt;success:&lt;b&gt;function&lt;/b&gt;(rsponse)      &lt;br /&gt;{      &lt;br /&gt;$(&amp;quot;#cropimage&amp;quot;).hide();      &lt;br /&gt;$(&amp;quot;#thumbs&amp;quot;).html(&amp;quot;&amp;quot;);      &lt;br /&gt;$(&amp;quot;#thumbs&amp;quot;).html(&amp;quot;&amp;lt;img src='uploads/&amp;quot;+rsponse+&amp;quot;' /&amp;gt;&amp;quot;);      &lt;br /&gt;}      &lt;br /&gt;});      &lt;br /&gt;}      &lt;br /&gt;}      &lt;br /&gt;else      &lt;br /&gt;alert(&amp;quot;Please select portion..!&amp;quot;);      &lt;br /&gt;}      &lt;br /&gt;$(document).ready(&lt;b&gt;function&lt;/b&gt; ()       &lt;br /&gt;{      &lt;br /&gt;$('img#photo').imgAreaSelect({      &lt;br /&gt;aspectRatio: '1:1',      &lt;br /&gt;onSelectEnd: getSizes      &lt;br /&gt;});      &lt;br /&gt;});      &lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h5&gt;Ajax Coding (ajax_image.php)&lt;/h5&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;&amp;lt;?php&lt;/b&gt;      &lt;br /&gt;include('db.php');      &lt;br /&gt;session_start();      &lt;br /&gt;$session_id=$_SESSION['user']; // Or Session ID      &lt;br /&gt;$t_width = 100; // Maximum thumbnail width      &lt;br /&gt;$t_height = 100; // Maximum thumbnail height      &lt;br /&gt;$new_name = &amp;quot;small&amp;quot;.$session_id.&amp;quot;.jpg&amp;quot;; // Thumbnail image name      &lt;br /&gt;$path = &amp;quot;uploads/&amp;quot;;      &lt;br /&gt;if(isset($_GET['t']) and $_GET['t'] == &amp;quot;ajax&amp;quot;)      &lt;br /&gt;{      &lt;br /&gt;extract($_GET);      &lt;br /&gt;$ratio = ($t_width/$w);       &lt;br /&gt;$nw = ceil($w * $ratio);      &lt;br /&gt;$nh = ceil($h * $ratio);      &lt;br /&gt;$nimg = imagecreatetruecolor($nw,$nh);      &lt;br /&gt;$im_src = imagecreatefromjpeg($path.$img);      &lt;br /&gt;imagecopyresampled($nimg,$im_src,0,0,$x1,$y1,$nw,$nh,$w,$h);      &lt;br /&gt;imagejpeg($nimg,$path.$new_name,90);      &lt;br /&gt;mysql_query(&amp;quot;UPDATE users SET profile_image_small='$new_name' WHERE uid='$session_id'&amp;quot;);      &lt;br /&gt;echo $new_name.&amp;quot;?&amp;quot;.time();      &lt;br /&gt;exit;      &lt;br /&gt;}      &lt;br /&gt;?&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Add database connectivity and other database option in separate file db.php&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;b&gt;&amp;lt;?php&lt;/b&gt;      &lt;br /&gt;$mysql_hostname = &amp;quot;Host name&amp;quot;;      &lt;br /&gt;$mysql_user = &amp;quot;UserName&amp;quot;;      &lt;br /&gt;$mysql_password = &amp;quot;Password&amp;quot;;      &lt;br /&gt;$mysql_database = &amp;quot;Database Name&amp;quot;;      &lt;br /&gt;$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die(&amp;quot;Could not connect database&amp;quot;);      &lt;br /&gt;mysql_select_db($mysql_database, $bd) or die(&amp;quot;Could not select database&amp;quot;);      &lt;br /&gt;&lt;b&gt;?&amp;gt;&lt;/b&gt;&lt;/p&gt;&lt;/blockquote&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-425885290020693880?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/425885290020693880/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=425885290020693880&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/425885290020693880'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/425885290020693880'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/07/image-cropping-using-jquery-php-coding.html' title='Image Cropping using JQuery &amp;amp; PHP Coding'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh3.ggpht.com/-uqTsCxYAt8o/ThBE2lGcnZI/AAAAAAAABuc/gMqp1_rcD-I/s72-c/cropimage_thumb%25255B1%25255D.jpg?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6063915304408875503</id><published>2011-07-01T10:48:00.000-07:00</published><updated>2011-07-01T10:50:51.835-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Script Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Generate text file using Java Script Coding</title><content type='html'>&lt;p&gt;In this tutorial we are going to see how to create file in web using Java Script. User have to enter the text in text and when they click the submit or download button, then the user can download that typed text in a text document. &lt;/p&gt;  &lt;p&gt;First we will design the web page by using HTML, We need one Textbox and command button to download the text in textbox.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Sample Screenshots&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh4.ggpht.com/-fiJunlt3p_k/Tg4I51zmooI/AAAAAAAABuI/e5lVBZ4Vd8c/s1600-h/dowload-file5.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="dowload file" border="0" alt="dowload file" src="http://lh4.ggpht.com/-nAneXHPvbCE/Tg4I7KYhxDI/AAAAAAAABuM/BUiK_9j81Ak/dowload-file_thumb3.png?imgmax=800" width="323" height="282" /&gt;&lt;/a&gt;&lt;a href="http://lh5.ggpht.com/-wyzi4PCKDHY/Tg4I84aD11I/AAAAAAAABuQ/L__0yt5g9VU/s1600-h/download-file-after5.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="download file after" border="0" alt="download file after" src="http://lh3.ggpht.com/-je4cW9w02VY/Tg4I-Yg7f-I/AAAAAAAABuU/gujSbnsQxXo/download-file-after_thumb3.png?imgmax=800" width="308" height="283" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;h3&gt;HTML Coding&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;File Name:index.html&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;&amp;lt;!DOCTYPE html&amp;gt;      &lt;br /&gt;&amp;lt;html&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;head&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;meta charset=&amp;quot;utf-8&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;title&amp;gt;Generating files with Java Script&amp;lt;/title&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;!-- Our CSS stylesheet file --&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;link rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;assets/css/styles.css&amp;quot; /&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;!--[if lt IE 9]&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;script src=&amp;quot;&lt;a href="http://html5shiv.googlecode.com/svn/trunk/html5.js&amp;quot;"&gt;http://html5shiv.googlecode.com/svn/trunk/html5.js&amp;quot;&lt;/a&gt;&amp;gt;&amp;lt;/script&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;![endif]--&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/head&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;body&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;header&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;h1&amp;gt;Generating Files with JavaScript&amp;lt;/h1&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/header&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;form action=&amp;quot;./&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;textarea&amp;gt;&amp;lt;/textarea&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;blueButton&amp;quot; id=&amp;quot;download&amp;quot;&amp;gt;Download&amp;lt;/a&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/form&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;script src=&amp;quot;&lt;a href="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js&amp;quot;"&gt;http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js&amp;quot;&lt;/a&gt;&amp;gt;&amp;lt;/script&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;script src=&amp;quot;assets/js/jquery.generateFile.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;script src=&amp;quot;assets/js/script.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/p&gt;    &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/body&amp;gt;      &lt;br /&gt;&amp;lt;/html&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now we need to check whether the textbox is empty and we need to name the file while download. Using PHP we can implement this,&lt;/p&gt;  &lt;h3&gt;PHP Coding&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;strong&gt;File Name: download.php&lt;/strong&gt;&lt;/p&gt;    &lt;p&gt;if(empty($_POST['filename']) || empty($_POST['content'])){      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; exit;       &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;// Sanitizing the filename:      &lt;br /&gt;$filename = preg_replace('/[^a-z0-9\-\_\.]/i','',$_POST['filename']);&lt;/p&gt;    &lt;p&gt;// Outputting headers:      &lt;br /&gt;header(&amp;quot;Cache-Control: &amp;quot;);       &lt;br /&gt;header(&amp;quot;Content-type: text/plain&amp;quot;);       &lt;br /&gt;header('Content-Disposition: attachment; filename=&amp;quot;'.$filename.'&amp;quot;');&lt;/p&gt;    &lt;p&gt;echo $_POST['content'];&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;JQuery Coding&lt;/h3&gt;  &lt;blockquote&gt;   &lt;pre&gt;(function($){&lt;br /&gt;&lt;br /&gt;	// Creating a jQuery plugin:&lt;br /&gt;&lt;br /&gt;	$.generateFile = function(options){&lt;br /&gt;&lt;br /&gt;		options = options || {};&lt;br /&gt;&lt;br /&gt;		if(!options.script || !options.filename || !options.content){&lt;br /&gt;			throw new Error(&amp;quot;Please enter all the required config options!&amp;quot;);&lt;br /&gt;		}&lt;br /&gt;&lt;br /&gt;		// Creating a 1 by 1 px invisible iframe:&lt;br /&gt;&lt;br /&gt;		var iframe = $('&amp;lt;iframe&amp;gt;',{&lt;br /&gt;			width:1,&lt;br /&gt;			height:1,&lt;br /&gt;			frameborder:0,&lt;br /&gt;			css:{&lt;br /&gt;				display:'none'&lt;br /&gt;			}&lt;br /&gt;		}).appendTo('body');&lt;br /&gt;&lt;br /&gt;		var formHTML = '&amp;lt;form action=&amp;quot;&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;'+&lt;br /&gt;			'&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;filename&amp;quot; /&amp;gt;'+&lt;br /&gt;			'&amp;lt;input type=&amp;quot;hidden&amp;quot; name=&amp;quot;content&amp;quot; /&amp;gt;'+&lt;br /&gt;			'&amp;lt;/form&amp;gt;';&lt;br /&gt;&lt;br /&gt;		// Giving IE a chance to build the DOM in&lt;br /&gt;		// the iframe with a short timeout:&lt;br /&gt;&lt;br /&gt;		setTimeout(function(){&lt;br /&gt;&lt;br /&gt;			// The body element of the iframe document:&lt;br /&gt;&lt;br /&gt;			var body = (iframe.prop('contentDocument') !== undefined) ?&lt;br /&gt;							iframe.prop('contentDocument').body :&lt;br /&gt;							iframe.prop('document').body;	// IE&lt;br /&gt;&lt;br /&gt;			body = $(body);&lt;br /&gt;&lt;br /&gt;			// Adding the form to the body:&lt;br /&gt;			body.html(formHTML);&lt;br /&gt;&lt;br /&gt;			var form = body.find('form');&lt;br /&gt;&lt;br /&gt;			form.attr('action',options.script);&lt;br /&gt;			form.find('input[name=filename]').val(options.filename);&lt;br /&gt;			form.find('input[name=content]').val(options.content);&lt;br /&gt;&lt;br /&gt;			// Submitting the form to download.php. This will&lt;br /&gt;			// cause the file download dialog box to appear.&lt;br /&gt;&lt;br /&gt;			form.submit();&lt;br /&gt;		},50);&lt;br /&gt;	};&lt;br /&gt;&lt;br /&gt;})(jQuery);&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;assets/script.js&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;p&gt;$(document).ready(function(){&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; $('#download').click(function(e){&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $.generateFile({ &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; filename&amp;#160;&amp;#160;&amp;#160; : 'export.txt', &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; content&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : $('textarea').val(), &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; script&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 'download.php' &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.preventDefault(); &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; });&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160; $('#downloadPage').click(function(e){&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $.generateFile({ &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; filename&amp;#160;&amp;#160;&amp;#160; : 'page.html', &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; content&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : $('html').html(), &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; script&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; : 'download.php' &lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; e.preventDefault(); &lt;br /&gt;    &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; });&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;  &lt;p&gt;});&lt;/p&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;a title="Download the source file" href="http://www.mediafire.com/?51n3o1739lzxp1k" target="_blank"&gt;Download the source file&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6063915304408875503?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6063915304408875503/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6063915304408875503&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6063915304408875503'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6063915304408875503'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/07/generate-text-file-using-java-script.html' title='Generate text file using Java Script Coding'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh4.ggpht.com/-nAneXHPvbCE/Tg4I7KYhxDI/AAAAAAAABuM/BUiK_9j81Ak/s72-c/dowload-file_thumb3.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-517492146132648256</id><published>2011-07-01T03:06:00.000-07:00</published><updated>2011-07-01T03:07:17.581-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Attractive Menu using CSS3</title><content type='html'>&lt;p&gt;In this Tutorial we are going to see attractive menu using CSS3.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-dSSVy6g25fE/Tg2cUoZHX7I/AAAAAAAABuA/CQz0AFyZAJs/s1600-h/CSS3%252520menu%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="CSS3 menu" border="0" alt="CSS3 menu" src="http://lh5.ggpht.com/-78OzkPAceBo/Tg2cVBc7ByI/AAAAAAAABuE/031T4n1G-a0/CSS3%252520menu_thumb%25255B1%25255D.png?imgmax=800" width="708" height="54" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;First design the menu using HTML tags,&lt;/p&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;lt;nav&amp;gt;     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;ul&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;span&amp;gt;Home&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;span&amp;gt;Categories&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;span&amp;gt;About&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;span&amp;gt;Portfolio&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;&amp;lt;span&amp;gt;Contact&amp;lt;/span&amp;gt;&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/ul&amp;gt;      &lt;br /&gt;&amp;lt;/nav&amp;gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Using CSS we will arrange the code for &amp;lt;li&amp;gt; and &amp;lt;ul&amp;gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;nav ul     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; padding: 0;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; margin: 0;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; list-style: none;      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;nav li     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; float: left;      &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now we have to style the anchor text.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;nav a     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; float: left;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; color: #eee;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; margin: 0 5px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; padding: 3px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; text-decoration: none;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; border: 1px solid #831608;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; font: bold 14px Arial, Helvetica;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-color: #831608;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -moz-linear-gradient(#bb413b, #831608);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-gradient(linear, left top, left bottom, from(#bb413b), to(#831608));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-linear-gradient(#bb413b, #831608);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -o-linear-gradient(#bb413b, #831608);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -ms-linear-gradient(#bb413b, #831608);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: linear-gradient(#bb413b, #831608);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -moz-border-radius: 5px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -webkit-border-radius: 5px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; border-radius: 5px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; text-shadow: 0 -1px 0 rgba(0,0,0,.8);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -moz-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3), 0 3px 0 rgba(0, 0, 0, 0.7), 0 2px 2px rgba(0, 0, 0, 0.5), 0 1px 0 rgba(255, 255, 255, 0.5) inset;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -webkit-box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3), 0 3px 0 rgba(0, 0, 0, 0.7), 0 2px 2px rgba(0, 0, 0, 0.5), 0 1px 0 rgba(255, 255, 255, 0.5) inset;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; box-shadow: 0 1px 0 rgba(255, 255, 255, 0.3), 0 3px 0 rgba(0, 0, 0, 0.7), 0 2px 2px rgba(0, 0, 0, 0.5), 0 1px 0 rgba(255, 255, 255, 0.5) inset;      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;nav a:hover     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-color: #bb413b;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -moz-linear-gradient(#831608, #bb413b);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-gradient(linear, left top, left bottom, from(#831608), to(#bb413b));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-linear-gradient(#831608, #bb413b);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -o-linear-gradient(#831608, #bb413b);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -ms-linear-gradient(#831608, #bb413b);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: linear-gradient(#831608, #bb413b);      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;nav a:active     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background: #bb413b;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; position: relative;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; top: 2px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -moz-box-shadow: 0 0 3px rgba(0, 0, 0, 0.7) inset;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; -webkit-box-shadow: 0 0 3px rgba(0, 0, 0, 0.7) inset;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; box-shadow: 0 0 3px rgba(0, 0, 0, 0.7) inset;      &lt;br /&gt;}&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Now style the Span Element&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;nav span     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; border: 1px dashed #eba1a3;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; display: inline-block;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; padding: 4px 15px;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; cursor: pointer;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-color: #bb413b;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -moz-linear-gradient(#d4463c, #aa2618);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-gradient(linear, left top, left bottom, from(#d4463c), to(#aa2618));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-linear-gradient(#d4463c, #aa2618);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -o-linear-gradient(#d4463c, #aa2618);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -ms-linear-gradient(#d4463c, #aa2618);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: linear-gradient(#d4463c, #aa2618);      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;nav a:hover span     &lt;br /&gt;{      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-color: #bb413b;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -moz-linear-gradient(#aa2618, #d4463c);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-gradient(linear, left top, left bottom, from(#aa2618), to(#d4463c));      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -webkit-linear-gradient(#aa2618, #d4463c);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -o-linear-gradient(#aa2618, #d4463c);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: -ms-linear-gradient(#aa2618, #d4463c);      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; background-image: linear-gradient(#aa2618, #d4463c);      &lt;br /&gt;}&lt;/p&gt;    &lt;p&gt;&amp;#160;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;Style the font property,&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;font: bold 14px Arial, Helvetica;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;&lt;a title="CSS3 Menu" href="http://www.mediafire.com/?skbd5br3daz818t" target="_blank"&gt;Download CSS3 Menu&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-517492146132648256?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/517492146132648256/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=517492146132648256&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/517492146132648256'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/517492146132648256'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/07/attractive-menu-using-css3.html' title='Attractive Menu using CSS3'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh5.ggpht.com/-78OzkPAceBo/Tg2cVBc7ByI/AAAAAAAABuE/031T4n1G-a0/s72-c/CSS3%252520menu_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8854318837246797201</id><published>2011-07-01T02:51:00.000-07:00</published><updated>2011-07-01T02:52:38.465-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Script Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='AJAX'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Ajax Coding: Top Drop Down Menu</title><content type='html'>&lt;p&gt;In this Ajax Coding we are going to design a menu which can slide downwards and we can put some content in that dropdown area.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh6.ggpht.com/-sO58lDQnH5w/Tg2Y3J-0rgI/AAAAAAAABtw/4fefIBwuCGA/s1600-h/drop%252520drown%252520menu%252520using%252520Ajax%252520-%252520Second%252520Menu%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="drop drown menu using Ajax - Second Menu" border="0" alt="drop drown menu using Ajax - Second Menu" src="http://lh6.ggpht.com/-xOu4YjkFbmw/Tg2Y4VfXJRI/AAAAAAAABt0/CfSBAckFb9M/drop%252520drown%252520menu%252520using%252520Ajax%252520-%252520Second%252520Menu_thumb%25255B1%25255D.png?imgmax=800" width="519" height="214" /&gt;&lt;/a&gt;&lt;/p&gt; &lt;a href="http://lh5.ggpht.com/-N5l6hYz1Mjo/Tg2Y4-DAjRI/AAAAAAAABt4/OqF-O4flX9A/s1600-h/drop%252520drown%252520menu%252520using%252520Ajax%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="drop drown menu using Ajax" border="0" alt="drop drown menu using Ajax" src="http://lh4.ggpht.com/-d-ZOZpvjVpw/Tg2Y5Yr9BjI/AAAAAAAABt8/zqen9W8rlfY/drop%252520drown%252520menu%252520using%252520Ajax_thumb%25255B1%25255D.png?imgmax=800" width="527" height="174" /&gt;&lt;/a&gt;   &lt;a name='more'&gt;&lt;/a&gt;  &lt;p&gt;First we need to add the script in the head menu,&lt;/p&gt;  &lt;blockquote&gt;   &lt;pre&gt;&amp;lt;script type='text/javascript' src='js/prototype.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script type='text/javascript' src='/js/scriptaculous.js?load=effects'&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script type='text/javascript' src='js/e24tabmenu.js'&amp;gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;After Adding Script, now we need to design the menu using HTML tags,&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;pre&gt;&amp;lt;div id=&amp;quot;menu&amp;quot; &amp;gt;&amp;lt;!---menu container--&amp;gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;item_3d&amp;quot; class=&amp;quot;menutarget&amp;quot;&amp;gt;&lt;br /&gt;	Contenido&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;item_gall&amp;quot; class=&amp;quot;menutarget&amp;quot;&amp;gt;&lt;br /&gt;	Contenido&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;item_menu&amp;quot; class=&amp;quot;menutarget&amp;quot;&amp;gt;&lt;br /&gt;	Contenido&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;item_efec&amp;quot; class=&amp;quot;menutarget&amp;quot;&amp;gt;&lt;br /&gt;	Contenido&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;!---tabs del menu--&amp;gt;&lt;br /&gt;  &amp;lt;a id=&amp;quot;3d&amp;quot; href=&amp;quot;#3d&amp;quot; rel=&amp;quot;e24menuitem[item_3d]&amp;quot;&amp;gt;&amp;lt;img src=&amp;quot;img/3d.gif&amp;quot; alt=&amp;quot;Efectos 3D&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;  &amp;lt;a id=&amp;quot;gall&amp;quot; href=&amp;quot;#galerias&amp;quot; rel=&amp;quot;e24menuitem[item_gall]&amp;quot;&amp;gt;&amp;lt;img src=&amp;quot;img/galerias.gif&amp;quot; alt=&amp;quot;Galeria de fotos AJAX&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;  &amp;lt;a id=&amp;quot;menus&amp;quot; href=&amp;quot;#menus&amp;quot; rel=&amp;quot;e24menuitem[item_menu]&amp;quot; &amp;gt;&amp;lt;img src=&amp;quot;img/menus.gif&amp;quot; alt=&amp;quot;Efectos de Menus&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;  &amp;lt;a id=&amp;quot;efec&amp;quot; href=&amp;quot;#efectos&amp;quot; rel=&amp;quot;e24menuitem[item_efec]&amp;quot;&amp;gt;&amp;lt;img src=&amp;quot;img/efectos.gif&amp;quot; alt=&amp;quot;Otros efectos ajax&amp;quot; /&amp;gt;&amp;lt;/a&amp;gt;&lt;br /&gt;  &amp;lt;!---tabs del menu--&amp;gt;&lt;br /&gt;&amp;lt;div id=&amp;quot;maincontent&amp;quot;&amp;gt;&lt;br /&gt;	Contenido principal&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;!--texto--&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;&amp;lt;!--menu container--&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Now we have to add the java script in menu,&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;pre&gt;oe24TabMenu = new e24TabMenu( 'menu', { duration: 1.0, transition: Effect.Transitions.sinoidal } );&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;a title="Top Menu sliding using Ajax Coding" href="http://www.mediafire.com/?sk7haxwhsju7dwq" target="_blank"&gt;Download the Top Menu sliding using Ajax Coding&lt;/a&gt;&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8854318837246797201?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8854318837246797201/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8854318837246797201&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8854318837246797201'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8854318837246797201'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/07/ajax-coding-top-drop-down-menu.html' title='Ajax Coding: Top Drop Down Menu'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-xOu4YjkFbmw/Tg2Y4VfXJRI/AAAAAAAABt0/CfSBAckFb9M/s72-c/drop%252520drown%252520menu%252520using%252520Ajax%252520-%252520Second%252520Menu_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-1538497203819543437</id><published>2011-06-28T21:34:00.000-07:00</published><updated>2011-06-28T21:35:11.808-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Script Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Sliding Read More in JQuery</title><content type='html'>&lt;p&gt;&lt;a href="http://lh5.ggpht.com/-cuoSe_3Pr5M/TgqrV5UfCWI/AAAAAAAABtU/6yA9O_mDKaE/s1600-h/Sliding%252520Readmore%252520in%252520JQuery%252520before%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Sliding Readmore in JQuery before" border="0" alt="Sliding Readmore in JQuery before" src="http://lh6.ggpht.com/-Q1sllxmt1qc/Tgqrac2SUQI/AAAAAAAABtY/wMbKiLywCms/Sliding%252520Readmore%252520in%252520JQuery%252520before_thumb%25255B1%25255D.png?imgmax=800" width="372" height="229" /&gt;&lt;/a&gt;&lt;a href="http://lh3.ggpht.com/-Uq1L6EM1kJ0/TgqrbpbmGPI/AAAAAAAABtc/b9welgCARrk/s1600-h/Sliding%252520Readmore%252520in%252520JQuery%252520After%25255B3%25255D.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="Sliding Readmore in JQuery After" border="0" alt="Sliding Readmore in JQuery After" src="http://lh5.ggpht.com/-iwqAzZsFe6s/TgqrfT3Z8aI/AAAAAAAABtk/nnia5jQBzl8/Sliding%252520Readmore%252520in%252520JQuery%252520After_thumb%25255B1%25255D.png?imgmax=800" width="379" height="235" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;In this tutorial we are going to see how read more option can be effectively and attractively used using JQuery. &lt;/p&gt;  &lt;h3&gt;XHTML Coding&lt;/h3&gt;  &lt;p&gt;Here we are creating the layout of the page using &amp;lt;div&amp;gt; tag, then using that &amp;lt;div&amp;gt; tag we are going to apply the JQuery.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&amp;lt;div id=&amp;quot;container&amp;quot;&amp;gt;      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;h1&amp;gt;jQuery slide with minimum height       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;h2&amp;gt;About Billabong&amp;lt;/h2&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div id=&amp;quot;wrap&amp;quot;&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;Gordon Merchant founded Billabong in Burleigh Heads on the Gold Coast in 1973. Combining his passion for surfing with hard work, Gordon designed boardshorts, manufacturing them on the kitchen table and selling through local surf shops and markets.&amp;lt;/p&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;Gordon developed his own stitching technique, which made the garments more durable, cost effective and less labor intensive. He employed machinists, moved the operation into a factory, set up a distribution network and sponsored a team of renowned Australian surfers. The business thrived.&amp;lt;/p&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;Since those beginnings, Billabong has expanded its product range to include boardsport products such as wetsuits, watches, surfboards, snowboard outerwear and skateboarding apparel.&amp;lt;/p&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;p&amp;gt;Information courtesy of &amp;lt;a title=&amp;quot;Billabong&amp;quot; href=&amp;quot;&lt;a href="http://www.billabong.com/us/&amp;quot;"&gt;http://www.billabong.com/us/&amp;quot;&lt;/a&gt;&amp;gt;Billabong&amp;lt;/a&amp;gt;.&amp;lt;/p&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div id=&amp;quot;gradient&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;/div&amp;gt;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; &amp;lt;div id=&amp;quot;read-more&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;       &lt;br /&gt;&amp;lt;/div&amp;gt;       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;&amp;#160;&lt;/h3&gt;  &lt;h3&gt;JQuery Coding&lt;/h3&gt;  &lt;p&gt;Using JQuery Coding we are going to apply the sliding animation.&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;$(function(){      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var slideHeight = 75; // px       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; var defHeight = $('#wrap').height();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if(defHeight &amp;gt;= slideHeight){       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#wrap').css('height' , slideHeight + 'px');       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#read-more').append('&amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Click to Read More&amp;lt;/a&amp;gt;');       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#read-more a').click(function(){       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; var curHeight = $('#wrap').height();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; if(curHeight == slideHeight){       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#wrap').animate({       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; height: defHeight       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }, &amp;quot;normal&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#read-more a').html('Close');       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#gradient').fadeOut();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }else{       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#wrap').animate({       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; height: slideHeight       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }, &amp;quot;normal&amp;quot;);       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#read-more a').html('Click to Read More');       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('#gradient').fadeIn();       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; return false;       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; });       &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }       &lt;br /&gt;});&lt;/p&gt; &lt;/blockquote&gt;  &lt;h3&gt;CSS Coding&lt;/h3&gt;  &lt;blockquote&gt;   &lt;p&gt;/* simple reset */      &lt;br /&gt;html,body,div,h2,p {margin:0;padding:0;}&lt;/p&gt;    &lt;p&gt;html {      &lt;br /&gt;font:1em Arial, Helvetica, sans-serif;       &lt;br /&gt;color:#444;       &lt;br /&gt;}       &lt;br /&gt;a {color:#0087f1;}       &lt;br /&gt;p {margin-bottom:5px;}       &lt;br /&gt;#container {       &lt;br /&gt;margin:0 auto;       &lt;br /&gt;width:600px;       &lt;br /&gt;}       &lt;br /&gt;#container h2 {       &lt;br /&gt;font-size:20px;       &lt;br /&gt;color:#0087f1;       &lt;br /&gt;}       &lt;br /&gt;#wrap {       &lt;br /&gt;position: relative;       &lt;br /&gt;padding: 10px;       &lt;br /&gt;overflow: hidden;       &lt;br /&gt;}       &lt;br /&gt;#gradient {       &lt;br /&gt;width:100%;       &lt;br /&gt;height:35px;       &lt;br /&gt;background:url(images/bg-gradient.png) repeat-x;       &lt;br /&gt;position:absolute;       &lt;br /&gt;bottom:0;       &lt;br /&gt;left:0;       &lt;br /&gt;}       &lt;br /&gt;#read-more {       &lt;br /&gt;padding:5px;       &lt;br /&gt;border-top:4px double #ddd;       &lt;br /&gt;background:#fff;       &lt;br /&gt;color:#333;       &lt;br /&gt;}       &lt;br /&gt;#read-more a {       &lt;br /&gt;padding-right:22px;       &lt;br /&gt;background:url(images/icon-arrow.gif) no-repeat 100% 50%;       &lt;br /&gt;font-weight:bold;       &lt;br /&gt;text-decoration:none;       &lt;br /&gt;}       &lt;br /&gt;#read-more a:hover {color:#000;}       &lt;br /&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;a name='more'&gt;&lt;/a&gt;  &lt;p&gt;&lt;a href="http://htmldrive.net/items/show/892/text-slide-effect-with-jQuery" rel="nofollow" target="_blank"&gt;Source&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-1538497203819543437?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/1538497203819543437/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=1538497203819543437&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1538497203819543437'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1538497203819543437'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/06/sliding-read-more-in-jquery.html' title='Sliding Read More in JQuery'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://lh6.ggpht.com/-Q1sllxmt1qc/Tgqrac2SUQI/AAAAAAAABtY/wMbKiLywCms/s72-c/Sliding%252520Readmore%252520in%252520JQuery%252520before_thumb%25255B1%25255D.png?imgmax=800' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2465485446333168016</id><published>2011-06-27T11:15:00.001-07:00</published><updated>2011-06-27T11:15:36.640-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Java Script Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='HTML Coding'/><category scheme='http://www.blogger.com/atom/ns#' term='CSS'/><title type='text'>Multi-color for Tool Tips–”CSS3”</title><content type='html'>&lt;p&gt;In this tutorial we are going learn how to make multi color tool tips for the text.&lt;/p&gt;  &lt;p&gt;&lt;a title="Multi Color for Tool Tips Source Code" href="http://www.mediafire.com/?sjxapzedqcsltws" rel="nofollow" target="_blank"&gt;Download the Multi Color for Tool Tips Source Code&lt;/a&gt;&lt;/p&gt;  &lt;blockquote&gt;   &lt;h3&gt;HTML&lt;/h3&gt;    &lt;pre&gt;&amp;lt;a href=&amp;quot;#&amp;quot; class=&amp;quot;tooltip&amp;quot;&amp;gt;&lt;br /&gt;&amp;#160; your text&lt;br /&gt;&amp;#160; &amp;lt;span&amp;gt;Your tooltip description&amp;lt;/span&amp;gt;&lt;br /&gt;&amp;lt;/a&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;h3&gt;Java Script&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot; src=&amp;quot;http://code.jquery.com/jquery-latest.min.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br /&gt;&amp;lt;script type=&amp;quot;text/javascript&amp;quot;&amp;gt;&lt;br /&gt;&amp;#160; $(function() {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; if ($.browser.msie &amp;amp;&amp;amp; $.browser.version.substr(0,1)&amp;lt;7)&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $('.tooltip').mouseover(function(){&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $(this).children('span').show();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; }).mouseout(function(){&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; $(this).children('span').hide();&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; })&lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;br /&gt;&amp;#160; });&lt;br /&gt;&amp;lt;/script&amp;gt;&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;blockquote&gt;&lt;br /&gt;  &lt;h3&gt;CSS&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;  &lt;pre&gt;.tooltip&lt;br /&gt;{&lt;br /&gt;&amp;#160; position: relative;&lt;br /&gt;&amp;#160; background: #eaeaea;&lt;br /&gt;&amp;#160; cursor: help;&lt;br /&gt;&amp;#160; display: inline-block;&lt;br /&gt;&amp;#160; text-decoration: none;&lt;br /&gt;&amp;#160; color: #222;&lt;br /&gt;&amp;#160; outline: none;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tooltip span&lt;br /&gt;{&lt;br /&gt;&amp;#160; visibility: hidden;&lt;br /&gt;&amp;#160; position: absolute;&lt;br /&gt;&amp;#160; bottom: 30px;&lt;br /&gt;&amp;#160; left: 50%;&lt;br /&gt;&amp;#160; z-index: 999;&lt;br /&gt;&amp;#160; width: 230px;&lt;br /&gt;&amp;#160; margin-left: -127px;&lt;br /&gt;&amp;#160; padding: 10px;&lt;br /&gt;&amp;#160; border: 2px solid #ccc;&lt;br /&gt;&amp;#160; opacity: .9;&lt;br /&gt;&amp;#160; background-color: #ddd;&lt;br /&gt;&amp;#160; background-image: -webkit-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));&lt;br /&gt;&amp;#160; background-image: -moz-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));&lt;br /&gt;&amp;#160; background-image: -ms-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));&lt;br /&gt;&amp;#160; background-image: -o-linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));&lt;br /&gt;&amp;#160; background-image: linear-gradient(rgba(255,255,255,.5), rgba(255,255,255,0));&lt;br /&gt;&amp;#160; -moz-border-radius: 4px;&lt;br /&gt;&amp;#160; border-radius: 4px;&lt;br /&gt;&amp;#160; -moz-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;&lt;br /&gt;&amp;#160; -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;&lt;br /&gt;&amp;#160; box-shadow: 0 1px 2px rgba(0,0,0,.4), 0 1px 0 rgba(255,255,255,.5) inset;&lt;br /&gt;&amp;#160; text-shadow: 0 1px 0 rgba(255,255,255,.4);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tooltip:hover&lt;br /&gt;{&lt;br /&gt;&amp;#160; border: 0; /* IE6 fix */&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tooltip:hover span&lt;br /&gt;{&lt;br /&gt;&amp;#160; visibility: visible;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tooltip span:before,&lt;br /&gt;.tooltip span:after&lt;br /&gt;{&lt;br /&gt;&amp;#160; content: &amp;quot;&amp;quot;;&lt;br /&gt;&amp;#160; position: absolute;&lt;br /&gt;&amp;#160; z-index: 1000;&lt;br /&gt;&amp;#160; bottom: -7px;&lt;br /&gt;&amp;#160; left: 50%;&lt;br /&gt;&amp;#160; margin-left: -8px;&lt;br /&gt;&amp;#160; border-top: 8px solid #ddd;&lt;br /&gt;&amp;#160; border-left: 8px solid transparent;&lt;br /&gt;&amp;#160; border-right: 8px solid transparent;&lt;br /&gt;&amp;#160; border-bottom: 0;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;.tooltip span:before&lt;br /&gt;{&lt;br /&gt;&amp;#160; border-top-color: #ccc;&lt;br /&gt;&amp;#160; bottom: -8px;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;/blockquote&gt;&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;a title="Multi Color for Tool Tips Source Code" href="http://www.mediafire.com/?sjxapzedqcsltws" rel="nofollow" target="_blank"&gt;Download the Multi Color for Tool Tips Source Code&lt;/a&gt;&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2465485446333168016?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2465485446333168016/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2465485446333168016&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2465485446333168016'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2465485446333168016'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2011/06/multi-color-for-tool-tipscss3.html' title='Multi-color for Tool Tips–”CSS3”'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-7427654859594013322</id><published>2008-06-30T00:44:00.000-07:00</published><updated>2008-06-30T00:46:23.812-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Finding the Min/Max Value in a Column : SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to find the highest and lowest &lt;a name="idx-CHP-7-0302"&gt;&lt;/a&gt;values in a given column. For example, you want to find  the highest and lowest salaries for all employees, as well as the highest and  lowest salaries for each department.&lt;/p&gt;&lt;a name="sqlckbk-CHP-7-SECT-2.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;When searching for the lowest and highest salaries for all  employees, simply use the functions MIN and MAX, respectively:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select min(sal) as min_sal, max(sal) as max_sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    MIN_SAL     MAX_SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        800        5000&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;When searching for the lowest and highest salaries for each  department, use the functions MIN and MAX with the GROUP BY clause:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;1 select deptno, min(sal) as min_sal, max(sal) as max_sal&lt;br /&gt; 2   from emp&lt;br /&gt; 3  group by deptno&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO     MIN_SAL     MAX_SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------  ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10        1300        5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20         800        3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30         950        2850&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-7-SECT-2.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When searching for the highest or lowest values, and in cases  where the whole table is the group or window, simply apply the MIN or MAX  function to the column you are interested in without using the &lt;a name="idx-CHP-7-0303"&gt;&lt;/a&gt;GROUP BY clause.&lt;/p&gt; &lt;p class="docText"&gt;Remember that the MIN and MAX &lt;a name="idx-CHP-7-0304"&gt;&lt;/a&gt;functions ignore NULLs, and that you can have NULL  groups as well as NULL values for columns in a group. The following are examples  that ultimately lead to a query using GROUP BY that returns NULL values for two  groups (DEPTNO 10 and 20):&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select deptno, comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno in (10,30)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  order by 1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      DEPTNO       COMM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30        300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30        500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30          0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30       1300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select min(comm), max(comm)&lt;br /&gt;   from emp&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MIN(COMM)   MAX(COMM)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          0        1300&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno, min(comm), max(comm)&lt;br /&gt;   from emp&lt;br /&gt;  group by deptno&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      DEPTNO  MIN(COMM)   MAX(COMM)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ---------- ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          30          0        1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Remember, Even if  nothing other than aggregate functions are listed in the &lt;a name="idx-CHP-7-0305"&gt;&lt;/a&gt;SELECT clause, you can still group by other columns in  the table; for example:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select min(comm), max(comm)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by deptno&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MIN(COMM)   MAX(COMM)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          0        1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Here you are still grouping by DEPTNO even though it is not in  the SELECT clause. Including the column you are grouping by in the SELECT clause  often improves readability, but is not mandatory. It is mandatory, however, that  any column in the SELECT list of a GROUP BY query also be listed in the GROUP BY  clause.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-7427654859594013322?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/7427654859594013322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=7427654859594013322&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7427654859594013322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7427654859594013322'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/finding-minmax-value-in-column-sql.html' title='Finding the Min/Max Value in a Column : SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8629154929322902788</id><published>2008-06-25T01:59:00.000-07:00</published><updated>2008-06-25T02:00:46.722-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Computing an Average : SQL Tutorial</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to compute the average value in a column, either for  all rows in a table or for some subset of rows. For example, you might want to  find the average salary for all employees as well as the average salary for each  department.&lt;/p&gt;&lt;a name="sqlckbk-CHP-7-SECT-1.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;When computing the average of all employee salaries, simply  apply the &lt;a name="idx-CHP-7-0297"&gt;&lt;/a&gt;AVG function to the column containing those  salaries. By excluding a WHERE clause, the average is computed against all  non-NULL values:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select avg(sal) as avg_sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    AVG_SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2073.21429&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;To compute the average salary for each department, use the  GROUP BY clause to create a group corresponding to each department:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select deptno, avg(sal) as avg_sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  group by deptno&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO     AVG_SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------  ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10  2916.66667&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20        2175&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30  1566.66667&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-7-SECT-1.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When finding an average where the whole table is the group or  window, simply apply the &lt;a name="idx-CHP-7-0298"&gt;&lt;/a&gt;AVG function to the column  you are interested in without using the GROUP BY clause. It is important to  realize that the function AVG ignores NULLs. The effect of NULL values being  ignored can be seen here:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create table t2(sal integer)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into t2 values (10)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into t2 values (20)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into t2 values (null)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select avg(sal)    select distinct 30/2&lt;br /&gt;   from t2            from t2&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   AVG(SAL)               30/2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------         ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         15                 15&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select avg(coalesce(sal,0))    select distinct 30/3&lt;br /&gt;   from t2                        from t2&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; AVG(COALESCE(SAL,0))                 30/3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------------------           ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   10                   10&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;a name="idx-CHP-7-0299"&gt;&lt;/a&gt;COALESCE function will return  the first non-NULL value found in the list of values that you pass. When NULL  SAL values are converted to zero, the average changes. When invoking aggregate  functions, always give thought to how you want NULLs handled.&lt;/p&gt; &lt;p class="docText"&gt;The second part of the solution uses GROUP BY (line 3) to  divide employee records into groups based on department affiliation. GROUP BY  automatically causes aggregate functions such as AVG to execute and return a  result for each group. In this example, AVG would execute once for each  department-based group of employee records.&lt;/p&gt; &lt;p class="docText"&gt;It is not necessary, by the way, to include GROUP BY columns in  your select list. For example:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select avg(sal)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by deptno&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   AVG(SAL)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2916.66667&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       2175&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1566.66667&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You are still grouping by DEPTNO even though it is not in the  SELECT clause. Including the column you are grouping by in the SELECT clause  often improves readability, but is not mandatory. It is mandatory, however, to  avoid placing columns in your SELECT list that are not also in your GROUP BY  clause.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8629154929322902788?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8629154929322902788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8629154929322902788&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8629154929322902788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8629154929322902788'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/computing-average-sql-tutorial.html' title='Computing an Average : SQL Tutorial'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-1673752405071302859</id><published>2008-06-23T22:20:00.001-07:00</published><updated>2008-06-23T22:22:34.298-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Parsing an IP Address in SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to parse an IP address's fields into columns. Consider  the following IP address:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;111.22.3.4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You would like the result of your query to be:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; A     B    C      D&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----- ----- ----- ---&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 111   22    3     4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The solution depends on the built-in functions provided by your  DBMS. Regardless of your DBMS, being able to locate periods and the numbers  immediately surrounding them are the keys to the solution.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the recursive WITH clause to simulate an iteration through  the &lt;a name="idx-CHP-6-0295"&gt;&lt;/a&gt;IP address while using SUBSTR to easily parse it.  A leading period is added to the IP address so that every set of numbers has a  period in front of it and can be treated the same way.&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt; 1 with x (pos,ip) as (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   values (1,'.92.111.0.222')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3   union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4  select pos+1,ip from x where pos+1 &lt;= 20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5 )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6 select max(case when rn=1 then e end) a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        max(case when rn=2 then e end) b,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        max(case when rn=3 then e end) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        max(case when rn=4 then e end) d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11 select pos,c,d,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        case when posstr(d,'.') &gt; 0 then substr(d,1,posstr(d,'.')-1)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13             else d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14        end as e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15        row_number() over( order by pos desc) rn&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17 select pos, ip,right(ip,pos) as c, substr(right(ip,pos),2) as d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18   from x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19  where pos &lt;= length(ip)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20   and substr(right(ip,pos),1,1) = '.'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21      ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22      ) y&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The function SUBSTR_INDEX makes parsing an IP address an easy  operation:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select substring_index(substring_index(y.ip,'.',1),'.',-1) a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2        substring_index(substring_index(y.ip,'.',2),'.',-1) b,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        substring_index(substring_index(y.ip,'.',3),'.',-1) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4        substring_index(substring_index(y.ip,'.',4),'.',-1) d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5   from (select '92.111.0.2' as ip from t1) y&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in function SUBSTR and INSTR to parse and  navigate through the &lt;a name="idx-CHP-6-0296"&gt;&lt;/a&gt;IP address:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select ip,&lt;br /&gt; 2        substr(ip, 1, instr(ip,'.')-1 ) a,&lt;br /&gt; 3        substr(ip, instr(ip,'.')+1,&lt;br /&gt; 4                    instr(ip,'.',1,2)-instr(ip,'.')-1 ) b,&lt;br /&gt; 5        substr(ip, instr(ip,'.',1,2)+1,&lt;br /&gt; 6                    instr(ip,'.',1,3)-instr(ip,'.',1,2)-1 ) c,&lt;br /&gt; 7        substr(ip, instr(ip,'.',1,3)+1 ) d&lt;br /&gt; 8   from (select '92.111.0.2' as ip from t1)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in function SPLIT_PART to parse an IP  address:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select split_part(y.ip,'.',1) as a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2        split_part(y.ip,'.',2) as b,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        split_part(y.ip,'.',3) as c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4        split_part(y.ip,'.',4) as d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5   from (select cast('92.111.0.2' as text) as ip from t1) as y&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Use the recursive WITH clause to simulate an iteration through  the IP address while using SUBSTR to easily parse it. A leading period is added  to the IP address so that every set of numbers has a period in front of it and  can be treated the same way:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  with x (pos,ip) as (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2    select 1 as pos,'.92.111.0.222' as ip from t1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3    union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4   select pos+1,ip from x where pos+1 &lt;= 20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5  )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6  select max(case when rn=1 then e end) a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7         max(case when rn=2 then e end) b,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         max(case when rn=3 then e end) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9         max(case when rn=4 then e end) d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    from  (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  select pos,c,d,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12         case when charindex('.',d) &gt; 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13            then substring(d,1,charindex('.',d)-1)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14            else d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15        end as e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16        row_number() over(order by pos desc) rn&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18 select pos, ip,right(ip,pos) as c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19        substring(right(ip,pos),2,len(ip)) as d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20   from x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21  where pos &lt;= len(ip)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22    and substring(right(ip,pos),1,1) = '.'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 23       ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 24       ) y&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-15.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;By using the built-in functions for your database, you can  easily walk through parts of a string. The key is being able to locate each of  the periods in the address. Then you can parse the numbers between each.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-1673752405071302859?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/1673752405071302859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=1673752405071302859&amp;isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1673752405071302859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1673752405071302859'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/parsing-ip-address-in-sql-server.html' title='Parsing an IP Address in SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-369882636743739548</id><published>2008-06-22T23:10:00.000-07:00</published><updated>2008-06-22T23:13:43.032-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Extracting the nth Delimited Substring : SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to extract a specified, delimited substring from a  string. Consider the following view V, which generates source data for this  problem:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; create view V as&lt;br /&gt; select 'mo,larry,curly' as name&lt;br /&gt;   from t1&lt;br /&gt;  union all&lt;br /&gt; select 'tina,gina,jaunita,regina,leena' as name&lt;br /&gt;   from t1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Output from the view is as follows:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select * from v&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; NAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; mo,larry,curly&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; tina,gina,jaunita,regina,leena&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You would like to extract the second name in each row, so the  final result set would be:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;   SUB&lt;br /&gt; -----&lt;br /&gt; larry&lt;br /&gt; gina&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The key to solving this problem is to return each name as an  individual row while preserving the order in which the name exists in the list.  Exactly how you do these things depends on which DBMS you are using.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;After walking the NAMEs returned by view V, use the function  ROW_NUMBER to keep only the second name from each string:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select substr(c,2,locate(',',c,2)-2)&lt;br /&gt;  2   from (&lt;br /&gt;  3 select pos, name, substr(name, pos) c,&lt;br /&gt;  4        row_number() over( partition by name&lt;br /&gt;  5                       order by length(substr(name,pos)) desc) rn&lt;br /&gt;  6  from (&lt;br /&gt;  7 select ',' ||csv.name|| ',' as name,&lt;br /&gt;  8         cast(iter.pos as integer) as pos&lt;br /&gt;  9   from V csv,&lt;br /&gt; 10        (select row_number( ) over( ) pos from t100 ) iter&lt;br /&gt; 11  where iter.pos &lt;= length(csv.name)+2&lt;br /&gt; 12        ) x&lt;br /&gt; 13  where length(substr(name,pos)) &gt; 1&lt;br /&gt; 14   and substr(substr(name,pos),1,1) = ','&lt;br /&gt; 15        ) y&lt;br /&gt; 16  where rn = 2&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;After walking the NAMEs returned by view V, use the position of  the commas to return only the second name in each string:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select iter.pos,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        substring_index(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        substring_index(src.name,',',iter.pos),',',-1) name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6   from V src,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        (select id pos from t10) iter,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8  where iter.pos &lt;=&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        length(src.name)-length(replace(src.name,',',''))&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  where pos = 2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;After walking the NAMEs returned by view V, retrieve the second  name in each list by using SUBSTR and INSTR:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select sub&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select iter.pos,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        src.name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        substr( src.name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         instr( src.name,',',1,iter.pos )+1,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7         instr( src.name,',',1,iter.pos+1 ) -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         instr( src.name,',',1,iter.pos )-1) sub&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9   from (select ','||name||',' as name from V) src,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        (select rownum pos from emp) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  where iter.pos &lt;&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13  where pos = 2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the function SPLIT_PART to help return each individual name  as a row:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select iter.pos, split_part(src.name,',',iter.pos) as name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4   from (select id as pos from t10) iter,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        (select cast(name as text) as name from v) src&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7  where iter.pos &lt;=&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         length(src.name)-length(replace(src.name,',',''))+1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10  where pos = 2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;After walking the NAMEs returned by view V, use the function  ROW_NUMBER to keep only the second name from each string:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select substring(c,2,charindex(',',c,2)-2)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select pos, name, substring(name, pos, len(name)) as c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        row_number() over(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5         partition by name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         order by len(substring(name,pos,len(name))) desc) rn&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8 select ',' + csv.name + ',' as name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        iter.pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10   from V csv,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        (select id as pos from t100 ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12  where iter.pos &lt;= len(csv.name)+2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14  where len(substring(name,pos,len(name))) &gt; 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15    and substring(substring(name,pos,len(name)),1,1) = ','&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16        ) y&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  where rn = 2&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The syntax is slightly different between these two DBMSs, but  the technique is the same. I will use the solution for DB2 for the discussion.  The strings are walked and the results are represented by inline view X:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select ','||csv.name|| ',' as name,&lt;br /&gt;         iter.pos&lt;br /&gt;   from v csv,&lt;br /&gt;         (select row_number() over( ) pos from t100 ) iter&lt;br /&gt;  where iter.pos &lt;= length(csv.name)+2&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; EMPS                             POS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------- ----&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,tina,gina,jaunita,regina,leena,    1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,tina,gina,jaunita,regina,leena,    2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,tina,gina,jaunita,regina,leena,    3&lt;/span&gt;&lt;br /&gt; …&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The next step is to then step through each character in each  string:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select pos, name, substr(name, pos) c,&lt;br /&gt;         row_number() over(partition by name&lt;br /&gt;                         order by length(substr(name, pos)) desc) rn&lt;br /&gt;   from (&lt;br /&gt; select ','||csv.name||',' as name,&lt;br /&gt;        cast(iter.pos as integer) as pos&lt;br /&gt;   from v csv,&lt;br /&gt;       (select row_number() over() pos from t100 ) iter&lt;br /&gt;  where iter.pos &lt;= length(csv.name)+2&lt;br /&gt;        ) x&lt;br /&gt;  where length(substr(name,pos)) &gt; 1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; POS EMPS            C                RN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --- --------------- ---------------- --&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   1 ,mo,larry,curly, ,mo,larry,curly, 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   2 ,mo,larry,curly, mo,larry,curly,  2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   3 ,mo,larry,curly, o,larry,curly,   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   4 ,mo,larry,curly, ,larry,curly,    4&lt;/span&gt;&lt;br /&gt;   …&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Now that different portions of the string are available to you,  simply identify which rows to keep. The rows you are interested in are the ones  that begin with a comma; the rest can be discarded:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select pos, name, substr(name,pos) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        row_number() over(partition by name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                        order by length(substr(name, pos)) desc) rn&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ','||csv.name||',' as name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        cast(iter.pos as integer) as pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from v csv,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (select row_number() over( ) pos from t100 ) iter &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where iter.pos &lt;= length(csv.name)+2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        ) x &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where length(substr(name,pos)) &gt; 1 &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     and substr(substr(name,pos),1,1) = ','&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; POS  EMPS                               C                                    RN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ---  --------------                     ----------------                      --&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    1  ,mo,larry,curly,                   ,mo,larry,curly,                       1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    4  ,mo,larry,curly,                   ,larry,curly,                          2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   10  ,mo,larry,curly,                   ,curly,                                3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    1  ,tina,gina,jaunita,regina,leena,   ,tina,gina,jaunita,regina,leena,       1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    6  ,tina,gina,jaunita,regina,leena,   ,gina,jaunita,regina,leena,            2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   11  ,tina,gina,jaunita,regina,leena,   ,jaunita,regina,leena,                 3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   19  ,tina,gina,jaunita,regina,leena,   ,regina,leena,                         4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   26  ,tina,gina,jaunita,regina,leena,   ,leena,       &lt;/span&gt;                         5&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This is an important step as it sets up how you will get the  &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th substring. Notice that many rows have been  eliminated from this query because of the following condition in the WHERE  clause:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;substr(substr(name,pos),1,1) = ','&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You'll notice that &lt;tt&gt;,larry,curly&lt;/tt&gt;, was ranked 4, but now  is ranked 2. Remember, the WHERE clause is evaluated before the SELECT, so the  rows with leading commas are kept, &lt;span class="docEmphasis"&gt;then&lt;/span&gt;  ROW_NUMBER performs its ranking. At this point it's easy to see that, to get the  &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th substring you want rows where RN equals &lt;span class="docEmphasis"&gt;n&lt;/span&gt;. The last step is to keep only the rows you are  interested in (in this case where RN equals 2) and use SUBSTR to extract the  name from that row. The name to keep is the first name in the row:  &lt;tt&gt;larry&lt;/tt&gt; from &lt;tt&gt;,larry,curly&lt;/tt&gt;, and &lt;tt&gt;gina&lt;/tt&gt; from  &lt;tt&gt;,gina,jaunita,regina,leena&lt;/tt&gt;,.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view X walks each string. You can determine how many  values are in each string by counting the delimiters in the string:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select iter.pos, src.name&lt;br /&gt;   from (select id pos from t10) iter,&lt;br /&gt;        V src&lt;br /&gt;  where iter.pos &lt;=&lt;br /&gt;        length(src.name)-length(replace(src.name,',',''))&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +------+--------------------------------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | pos  | name                           |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +------+--------------------------------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    1 | mo,larry,curly                 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    2 | mo,larry,curly                 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    1 | tina,gina,jaunita,regina,leena |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    2 | tina,gina,jaunita,regina,leena |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    3 | tina,gina,jaunita,regina,leena |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    4 | tina,gina,jaunita,regina,leena |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +------+--------------------------------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;In this case, there is one fewer row than values in each string  because that's all that is needed. The function SUBSTRING_INDEX takes care of  parsing the needed values:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select iter.pos,src.name name1,&lt;br /&gt;         substring_index(src.name,',',iter.pos) name2,&lt;br /&gt;         substring_index(&lt;br /&gt;          substring_index(src.name,',',iter.pos),',',-1) name3&lt;br /&gt;    from (select id pos from t10) iter,&lt;br /&gt;         V src&lt;br /&gt;   where  iter.pos &lt;=&lt;br /&gt;         length(src.name)-length(replace(src.name,',',''))&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;+------+--------------------------------+--------------------------+---------+ &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;| pos  | name1                        | name2                      | name3   | &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;+------+--------------------------------+--------------------------+---------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    1 | mo,larry,curly                 | mo                       | mo      |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    2 | mo,larry,curly                 | mo,larry                 | larry   |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    1 | tina,gina,jaunita,regina,leena | tina                     | tina    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    2 | tina,gina,jaunita,regina,leena | tina,gina                | gina    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    3 | tina,gina,jaunita,regina,leena | tina,gina,jaunita        | jaunita |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;|    4 | tina,gina,jaunita,regina,leena | tina,gina,jaunita,regina | regina  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;+------+--------------------------------+--------------------------+---------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;I've shown three name fields, so you can see how the nested  SUBSTRING_INDEX calls work. The inner call returns all characters to the left of  the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of a comma. The outer call  returns everything to the right of the first comma it finds (starting from the  end of the string). The final step is to keep the value for NAME3 where POS  equals &lt;span class="docEmphasis"&gt;n&lt;/span&gt;, in this case 2.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The inline view walks each string. The number of times each  string is returned is determined by how many values are in each string. The  solution finds the number of values in each string by counting the number of  delimiters in it. Because each string is enclosed in commas, the number of  values in a string is the number of commas minus one. The strings are then  UNIONed and joined to a table with a cardinality that is at least the number of  values in the largest string. The functions SUBSTR and INSTR use the value of  POS to parse each string:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select iter.pos, src.name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        substr( src.name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         instr( src.name,',',1,iter.pos )+1,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         instr( src.name,',',1,iter.pos+1 )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         instr( src.name,',',1,iter.pos )-1) sub&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select ','||name||',' as name from v) src,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (select rownum pos from emp) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where iter.pos &lt;&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; POS NAME                              SUB&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --- --------------------------------- -------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   1 ,mo,larry,curly,                  mo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   1 , tina,gina,jaunita,regina,leena, tina&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   2 ,mo,larry,curly,                  larry&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   2 , tina,gina,jaunita,regina,leena, gina&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   3 ,mo,larry,curly,                  curly&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   3 , tina,gina,jaunita,regina,leena, jaunita&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   4 , tina,gina,jaunita,regina,leena, regina &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   5 , tina,gina,jaunita,regina,leena, leena&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The first call to INSTR within SUBSTR determines the start  position of the substring to extract. The next call to INSTR within SUBSTR finds  the position of the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th comma (same as the start  position) as well the position of the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th + 1  comma. Subtracting the two values returns the length of the substring to  extract. Because every value is parsed into its own row, simply specify WHERE  POS = &lt;span class="docEmphasis"&gt;n&lt;/span&gt; to keep the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th substring (in this case, where POS = 2, so, the  second substring in the list).&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-14.3.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view X walks each string. The number of rows  returned is determined by how many values are in each string. To find the number  of values in each string, find the number of delimiters in each string and add  one. The function SPLIT_PART uses the values in POS to find the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of the delimiter and parse the string  into values:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select iter.pos, src.name as name1,&lt;br /&gt;         split_part(src.name,',',iter.pos) as name2&lt;br /&gt;    from (select id as pos from t10) iter,&lt;br /&gt;         (select cast(name as text) as name from v) src&lt;br /&gt;   where iter.pos &lt;=&lt;br /&gt;         length(src.name)-length(replace(src.name,',',''))+1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  pos |             name1              |  name2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -----+--------------------------------+---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    1 | mo,larry,curly                 | mo&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    2 | mo,larry,curly                 | larry&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    3 | mo,larry,curly                 | curly&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    1 | tina,gina,jaunita,regina,leena | tina&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    2 | tina,gina,jaunita,regina,leena | gina&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    3 | tina,gina,jaunita,regina,leena | jaunita&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    4 | tina,gina,jaunita,regina,leena | regina&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    5 | tina,gina,jaunita,regina,leena | leena&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;I've shown NAME twice so you can see how SPLIT_PART parses each  string using POS. Once each string is parsed, the final step is the keep the  rows where POS equals the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th substring you are  interested in, in this case, 2.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-369882636743739548?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/369882636743739548/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=369882636743739548&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/369882636743739548'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/369882636743739548'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/extracting-nth-delimited-substring-sql.html' title='Extracting the nth Delimited Substring : SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8816986617666819935</id><published>2008-06-22T00:51:00.000-07:00</published><updated>2008-06-22T00:54:26.665-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Identifying Strings That Can Be Treated as Numbers : SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You have a column that is defined to hold character data.  Unfortunately, the rows contain mixed &lt;a name="idx-CHP-6-0287"&gt;&lt;/a&gt;numeric and  character data. Consider view V:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; create view V as&lt;br /&gt; select replace(mixed,' ','') as mixed&lt;br /&gt;   from (&lt;br /&gt; select substr(ename,1,2)||&lt;br /&gt;        cast(deptno as char(4))||&lt;br /&gt;        substr(ename,3,2) as mixed&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 10&lt;br /&gt;  union all&lt;br /&gt; select cast(empno as char(4)) as mixed&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 20&lt;br /&gt;  union all&lt;br /&gt; select ename as mixed&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 30&lt;br /&gt;        ) x&lt;br /&gt; select * from v&lt;br /&gt;&lt;br /&gt;  MIXED&lt;br /&gt;  --------------&lt;br /&gt;  CL10AR&lt;br /&gt;  KI10NG&lt;br /&gt;  MI10LL&lt;br /&gt;  7369&lt;br /&gt;  7566&lt;br /&gt;  7788&lt;br /&gt;  7876&lt;br /&gt;  7902&lt;br /&gt;  ALLEN&lt;br /&gt;  WARD&lt;br /&gt;  MARTIN&lt;br /&gt;  BLAKE&lt;br /&gt;  TURNER&lt;br /&gt;  JAMES&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to return rows that are numbers only, or that contain  at least one number. If the numbers are mixed with character data, you want to  remove the characters and return only the numbers. For the sample data above you  want the following result set:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;    MIXED&lt;br /&gt; --------&lt;br /&gt;       10&lt;br /&gt;       10&lt;br /&gt;       10&lt;br /&gt;     7369&lt;br /&gt;     7566&lt;br /&gt;     7788&lt;br /&gt;     7876&lt;br /&gt;     7902&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The functions &lt;a name="idx-CHP-6-0288"&gt;&lt;/a&gt;REPLACE and TRANSLATE  are extremely useful for manipulating strings and individual characters. The key  is to convert all numbers to a single character, which then makes it easy to  isolate and identify any number by referring to a single character.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use functions TRANSLATE, REPLACE, and POSSTR to isolate the &lt;a name="idx-CHP-6-0289"&gt;&lt;/a&gt;numeric characters in each row. The calls to CAST are  necessary in view V; otherwise, the view will fail to be created due to type  conversion errors. You'll need the function REPLACE to remove extraneous white  space due to casting to the fixed length CHAR:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select mixed old,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3          case&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4          when&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5            replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6           translate(mixed,'9999999999','0123456789'),'9','') = ''&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7          then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8             mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9          else replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10            translate(mixed,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11               repeat('#',length(mixed)),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12             replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13              translate(mixed,'9999999999','0123456789'),'9','')),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14                      '#','')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15          end as integer ) mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  where posstr(translate(mixed,'9999999999','0123456789'),'9') &gt; 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The syntax for MySQL is slightly different and will define view  V as:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create view V as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select concat(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          substr(ename,1,2),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          replace(cast(deptno as char(4)),' ',''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          substr(ename,3,2)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        ) as mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno = 10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select replace(cast(empno as char(4)), ' ', '')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp where deptno = 20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename from emp where deptno = 30&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Because MySQL does not support the TRANSLATE function, you must  walk each row and evaluate it on a character-by-character basis.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select cast(group_concat(c order by pos separator '') as unsigned)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        as MIXED1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4 select v.mixed, iter.pos, substr(v.mixed,iter.pos,1) as c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5   from V,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        ( select id pos from t10 ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7  where iter.pos &lt;= length(v.mixed)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8    and ascii(substr(v.mixed,iter.pos,1)) between 48 and 57&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        ) y&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10  group by mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  order by 1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use functions TRANSLATE, REPLACE, and INSTR to isolate the &lt;a name="idx-CHP-6-0290"&gt;&lt;/a&gt;numeric characters in each row. The calls to CAST are  not necessary in view V. Use the function REPLACE to remove extraneous white  space due to casting to the fixed length CHAR. If you decide you would like to  keep the explicit type conversion calls in the view definition, it is suggested  you cast to VARCHAR2:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select to_number (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         case&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         when&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4            replace(translate(mixed,'0123456789','9999999999'),'9')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5           is not null&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7              replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8            translate(mixed,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9              replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10            translate(mixed,'0123456789','9999999999'),'9'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11                     rpad('#',length(mixed),'#')),'#')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12         else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13              mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14         end&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15         ) mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  where instr(translate(mixed,'0123456789','9999999999'),'9') &gt; 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use functions TRANSLATE, REPLACE, and STRPOS to isolate the &lt;a name="idx-CHP-6-0291"&gt;&lt;/a&gt;numeric characters in each row. The calls to CAST are  not necessary in view V. Use the function REPLACE ito remove extraneous white  space due to casting to the fixed length CHAR. If you decide you would like to  keep the explicit type conversion calls in the view definition, it is suggested  you cast to VARCHAR:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        case&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3        when&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         replace(translate(mixed,'0123456789','9999999999'),'9','')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5         is not null&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        translate(mixed,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        translate(mixed,'0123456789','9999999999'),'9',''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11                 rpad('#',length(mixed),'#')),'#','')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13          mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14        end as integer ) as mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15    from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16  where strpos(translate(mixed,'0123456789','9999999999'),'9') &gt; 0   &lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The built-in function ISNUMERIC along with a wildcard search  allows you to easily identify strings that contains numbers, but getting numeric  characters out of a string is not particularly efficient because the TRANSLATE  function is not supported.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The TRANSLATE function is very useful here as it allows you to  easily isolate and identify numbers and characters. The trick is to convert all  numbers to a single character; this way, rather than searching for different  numbers you only search for one character.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2, Oracle, and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;The syntax differs slightly among these DBMSs, but the  technique is the same. I'll use the solution for PostgreSQL for the  discussion.&lt;/p&gt; &lt;p class="docText"&gt;The real work is done by functions TRANSLATE and REPLACE. To  get the final result set requires several function calls, each listed below in  one query:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select mixed as orig,&lt;br /&gt; translate(mixed,'0123456789','9999999999') as mixed1,&lt;br /&gt; replace(translate(mixed,'0123456789','9999999999'),'9','') as mixed2,&lt;br /&gt;  translate(mixed,&lt;br /&gt;  replace(&lt;br /&gt;  translate(mixed,'0123456789','9999999999'),'9',''),&lt;br /&gt;           rpad('#',length(mixed),'#')) as mixed3,&lt;br /&gt;  replace(&lt;br /&gt;  translate(mixed,&lt;br /&gt;  replace(&lt;br /&gt; translate(mixed,'0123456789','9999999999'),'9',''),&lt;br /&gt;           rpad('#',length(mixed),'#')),'#','') as mixed4&lt;br /&gt;   from V&lt;br /&gt;  where strpos(translate(mixed,'0123456789','9999999999'),'9') &gt; 0&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   ORIG  | MIXED1 | MIXED2 | MIXED3 | MIXED4 | MIXED5 &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------+--------+--------+--------+--------+--------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CL10AR | CL99AR | CLAR   | ##10## | 10     |     10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KI10NG | KI99NG | KING   | ##10## | 10     |     10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MI10LL | MI99LL | MILL   | ##10## | 10     |     10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7369   | 9999   |        | 7369   | 7369   |   7369&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7566   | 9999   |        | 7566   | 7566   |   7566&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7788   | 9999   |        | 7788   | 7788   |   7788&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7876   | 9999   |        | 7876   | 7876   |   7876&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7902   | 9999   |        | 7902   | 7902   |   7902&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;First, notice that any rows without at least one number are  removed. How this is accomplished will become clear as you examine each of the  columns in the above result set. The rows that are kept are the values in the  ORIG column and are the rows that will eventually make up the result set. The  first step to extracting the numbers is to use the function TRANSLATE to convert  any number to a 9 (you can use any digit; 9 is arbitrary), this is represented  by the values in MIXED1. Now that all numbers are 9's, they can be treating as a  single unit. The next step is to remove all of the numbers by using the function  REPLACE. Because all digits are now 9, REPLACE simply looks for any 9's and  removes them. This is represented by the values in MIXED2. The next step,  MIXED3, uses values that are returned by MIXED2. These values are then compared  to the values in ORIG. If any characters from MIXED2 are found in ORIG, they are  converted to the # character by TRANSLATE. The result set from MIXED3 shows that  the letters, not the numbers, have now been singled out and converted to a  single character. Now that all non-&lt;a name="idx-CHP-6-0292"&gt;&lt;/a&gt;numeric characters  are represented by #'s, they can be treated as a single unit. The next step,  MIXED4, uses REPLACE to find and remove any # characters in each row; what's  left are numbers only. The final step is to cast the numeric characters as  numbers. Now that you've gone through the steps, you can see how the WHERE  clause works. The results from MIXED1 are passed to STRPOS, and if a 9 is found  (the position in the string where the first 9 is located) the result must be  greater than 0. For rows that return a value greater than zero, it means there's  at least one number in that row and it should be kept.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-13.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The first step is to walk each string and evaluate each  character and determine whether or not it's a number:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select v.mixed, iter.pos, substr(v.mixed,iter.pos,1) as c&lt;br /&gt;   from V,&lt;br /&gt;       ( select id pos from t10 ) iter&lt;br /&gt;  where iter.pos &lt;= length(v.mixed)&lt;br /&gt;  order by 1,2&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | mixed  | pos  | c    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    1 | 7    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    2 | 3    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    3 | 6    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    4 | 9    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ALLEN  |    1 | A    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ALLEN  |    2 | L    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ALLEN  |    3 | L    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ALLEN  |    4 | E    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ALLEN  |    5 | N    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    1 | C    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    2 | L    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    3 | 1    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    4 | 0    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    5 | A    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    6 | R    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Now that each character in each string can be evaluated  individually, the next step is to keep only the rows that have a number in the C  column:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select v.mixed, iter.pos, substr(v.mixed,iter.pos,1) as c&lt;br /&gt;    from V,&lt;br /&gt;        ( select id pos from t10 ) iter&lt;br /&gt;  where iter.pos &lt;= length(v.mixed)&lt;br /&gt;   and ascii(substr(v.mixed,iter.pos,1)) between 48 and 57&lt;br /&gt;  order by 1,2&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | mixed  | pos  | c    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    1 | 7    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    2 | 3    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    3 | 6    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7369   |    4 | 9    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    3 | 1    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | CL10AR |    4 | 0    |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;At this point, all the rows in column C are numbers. The next  step is to use GROUP_CONCAT to concatenate the numbers to form their respective  whole number in MIXED. The final result is then cast as a number:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select cast(group_concat(c order by pos separator '') as unsigned)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          as MIXED1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from ( &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select v.mixed, iter.pos, substr(v.mixed,iter.pos,1) as c &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from V, &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       ( select id pos from t10 ) iter &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where iter.pos &lt;= length(v.mixed) &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   and ascii(substr(x.mixed,iter.pos,1)) between 48 and 57&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        ) y&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   group by mixed&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   order by 1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | MIXED1 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    10  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    10  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |    10  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |  7369  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |  7566  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |  7788  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |  7876  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; |  7902  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +--------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;As a final note, keep in mind that any digits in each string  will be concatenated to form one &lt;a name="idx-CHP-6-0293"&gt;&lt;/a&gt;numeric value. For  example, an input value of, say, '99Gennick87' will result in the value 9987  being returned. This is something to keep in mind, particularly when working  with serialized data.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8816986617666819935?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8816986617666819935/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8816986617666819935&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8816986617666819935'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8816986617666819935'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/identifying-strings-that-can-be-treated.html' title='Identifying Strings That Can Be Treated as Numbers : SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2029751580564870745</id><published>2008-06-20T02:47:00.000-07:00</published><updated>2008-06-20T02:50:16.531-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Alphabetizing a String : SQL SERVER</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want alphabetize the individual characters within strings  in your tables. Consider the following result set:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;You would like the result to be:&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; OLD_NAME   NEW_NAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- --------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      AADMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN      AELLN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE      ABEKL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK      ACKLR&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD       DFOR&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES      AEJMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES      EJNOS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING       GIKN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN     AIMNRT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER     EILLMR&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT      COSTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH      HIMST&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER     ENRRTU&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD       ADRW&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;This problem is a perfect example of why it is crucial to  understand your DBMS and what functionality is available to you. In situations  where your DBMS does not provide built-in functions to facilitate this solution,  you need to come up with something creative. Compare the MySQL solution with the  rest.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;To alphabetize rows of strings it is necessary to walk each  string then order its characters:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        max(case when pos=1 then c else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3        max(case when pos=2 then c else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        max(case when pos=3 then c else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        max(case when pos=4 then c else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        max(case when pos=5 then c else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        max(case when pos=6 then c else '' end)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9 select e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        cast(substr(e.ename,iter.pos,1) as varchar(100)) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        cast(row_number( )over(partition by e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12                               order by substr(e.ename,iter.pos,1))&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13            as integer) pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14   from emp e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15        (select cast(row_number( )over( ) as integer) pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16           from emp) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  where iter.pos &lt;= length(e.ename)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19  group by ename&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The key here is the GROUP_CONCAT function, which allows you to  not only concatenate the characters that make up each name but also order  them:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select ename, group_concat(c order by c separator '')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3 select ename, substr(a.ename,iter.pos,1) c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   from emp a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5        ( select id pos from t10 ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6  where iter.pos &lt;= length(a.ename)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8  group by ename&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The function SYS_CONNECT_BY_PATH allows you to iteratively  build a list:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select old_name, new_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select old_name, replace(sys_connect_by_path(c,' '),' ') new_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5 select e.ename old_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        row_number() over(partition by e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7                         order by substr(e.ename,iter.pos,1)) rn,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        substr(e.ename,iter.pos,1) c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9   from emp e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        ( select rownum pos from emp ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  where iter.pos &lt;= length(e.ename)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12  order by 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13         ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14  start with rn = 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15 connect by prior rn = rn-1 and prior old_name = old_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16         )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  where length(old_name) = length(new_name)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;PostgreSQL does not offer any built-in functions to easily sort  characters in a string, so it is necessary not only to walk through each string  but also to know in advance the largest length of any one name. View V is used  in this solution for readability:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  create or replace view V as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  select x.*&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  select a.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         substr(a.ename,iter.pos,1) as c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    from emp a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         (select id as pos from t10) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   where iter.pos &lt;= length(a.ename)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   order by 1,2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         ) x&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;The following select statement leverages the view:&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select ename,&lt;br /&gt;  2        max(case when pos=1 then&lt;br /&gt;  3                 case when cnt=1 then c&lt;br /&gt;  4                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt;  5                 end&lt;br /&gt;  6                 else ''&lt;br /&gt;  7             end)||&lt;br /&gt;  8        max(case when pos=2 then&lt;br /&gt;  9                 case when cnt=1 then c&lt;br /&gt; 10                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt; 11                 end&lt;br /&gt; 12                 else ''&lt;br /&gt; 13             end)||&lt;br /&gt; 14        max(case when pos=3 then&lt;br /&gt; 15                 case when cnt=1 then c&lt;br /&gt; 16                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt; 17                 end&lt;br /&gt; 18                 else ''&lt;br /&gt; 19             end)||&lt;br /&gt; 20        max(case when pos=4 then&lt;br /&gt; 21                 case when cnt=1 then c&lt;br /&gt; 22                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt; 23                 end&lt;br /&gt; 24                 else ''&lt;br /&gt; 25             end)||&lt;br /&gt; 26        max(case when pos=5 then&lt;br /&gt; 27                 case when cnt=1 then c&lt;br /&gt; 28                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt; 29                 end&lt;br /&gt; 30                 else ''&lt;br /&gt; 31             end)||&lt;br /&gt; 32        max(case when pos=6 then&lt;br /&gt; 33                 case when cnt=1 then c&lt;br /&gt; 34                      else rpad(c,cast(cnt as integer),c)&lt;br /&gt; 35                 end&lt;br /&gt; 36                 else ''&lt;br /&gt; 37             end)&lt;br /&gt; 38   from (&lt;br /&gt; 39 select a.ename, a.c,&lt;br /&gt; 40        (select count(*)&lt;br /&gt; 41           from v b&lt;br /&gt; 42          where a.ename=b.ename and a.c=b.c ) as cnt,&lt;br /&gt; 43        (select count(*)+1&lt;br /&gt; 44           from v b&lt;br /&gt; 45          where a.ename=b.ename and b.c&lt;a.c) as="" pos="" 46="" from="" v="" a="" 47="" x="" 48="" group="" by="" ename=""&gt;&lt;/a.c)&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;To alphabetize rows of strings it is necessary to walk each  string, and then order their characters:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select ename,&lt;br /&gt;  2           max(case when pos=1 then c else '' end)+&lt;br /&gt;  3           max(case when pos=2 then c else '' end)+&lt;br /&gt;  4           max(case when pos=3 then c else '' end)+&lt;br /&gt;  5           max(case when pos=4 then c else '' end)+&lt;br /&gt;  6           max(case when pos=5 then c else '' end)+&lt;br /&gt;  7           max(case when pos=6 then c else '' end)&lt;br /&gt;  8      from (&lt;br /&gt;  9    select e.ename,&lt;br /&gt; 10         substring(e.ename,iter.pos,1) as c,&lt;br /&gt; 11         row_number() over (&lt;br /&gt; 12          partition by e.ename&lt;br /&gt; 13              order by substring(e.ename,iter.pos,1)) as pos&lt;br /&gt; 14    from emp e,&lt;br /&gt; 15         (select row_number()over(order by ename) as pos&lt;br /&gt; 16            from emp) iter&lt;br /&gt; 17   where iter.pos &lt;= len(e.ename)  18          ) x  19   group by ename &lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The inline view X returns each character in each name as a row.  The function SUBSTR or SUBSTRING extracts each character from each name, and the  function ROW_NUMBER ranks each character alphabetically:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME  C  POS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -----  -  ---&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  A  1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  A  2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  D  3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  M  4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  S  5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;To return each letter of a string as a row, you must walk the  string. This is accomplished with inline view ITER.&lt;/p&gt; &lt;p class="docText"&gt;Now that the letters in each name have been alphabetized, the  last step is to put those letters back together, into a string, in the order  they are ranked. Each letter's position is evaluated by the CASE statements  (lines 27). If a character is found at a particular position it is then  concatenated to the result of the next evaluation (the following CASE  statement). Because the aggregate function MAX is used as well, only one  character per position POS is returned, so that only one row per name is  returned. The CASE evaluation goes up to the number 6, which is the maximum  number of characters in any name in table EMP.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-12.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view X (lines 36) returns each character in each  name as a row. The function SUBSTR extracts each character from each name:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME  C&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -----  -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  D&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  M&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS  S&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;Inline view ITER is used to walk the string. From there, the  rest of the work is done by the GROUP_CONCAT function. By specifying an order,  the function not only concatenates each letter, it does so alphabetically.&lt;a name="sqlckbk-CHP-6-SECT-12.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The real work is done by inline view X (lines 511), where the  characters in each name are extracted and put into alphabetical order. This is  accomplished by walking the string, then imposing order on those characters. The  rest of the query merely glues the names back together.&lt;/p&gt; &lt;p class="docText"&gt;The tearing apart of names can be seen by executing only inline  view X:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; OLD_NAME          RN C&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- --------- -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS              1 A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS              2 A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS              3 D&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS              4 M&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS              5 S&lt;/span&gt;&lt;br /&gt; …&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The next step is to take the alphabetized characters and  rebuild each name. This is done with the function SYS_CONNECT_BY_PATH by  appending each character to the ones before it:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; OLD_NAME   NEW_NAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      AA&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      AAD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      AADM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      AADMS&lt;/span&gt;&lt;br /&gt; …&lt;br /&gt;&lt;/pre&gt;The final step is to keep only the strings that have the same  length as the names they were built from.&lt;a name="sqlckbk-CHP-6-SECT-12.3.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;For readability, view V is used in this solution to walk the  string. The function SUBSTR, in the view definition, extracts each character  from each name so that the view returns:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME C&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----- -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS A&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS D&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS M&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS S&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;The view also orders the results by ENAME and by each letter in  each name. The inline view X (lines 1518) returns the names and characters from  view V, the number of times each character occurs in each name, and its position  (alphabetically):&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ename | c | cnt | pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------+---+-----+-----&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS | A |   2 |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS | A |   2 |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS | D |   1 |   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS | M |   1 |   4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS | S |   1 |   5&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;The extra columns CNT and POS, returned by the inline view X,  are crucial to the solution. POS is used to rank each character and CNT is used  to determine the number of times the character exists in each name. The final  step is to evaluate the position of each character and rebuild the name. You'll  notice that each case statement is actually two case statements. This is to  determine whether or not a character occursmore than once in a name; if it does,  then rather than return that character, what is returned is that character  appended to itself CNT times. The aggregate function, MAX, is used to ensure  there is only one row per name.** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2029751580564870745?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2029751580564870745/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2029751580564870745&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2029751580564870745'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2029751580564870745'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/alphabetizing-string-sql-server.html' title='Alphabetizing a String : SQL SERVER'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-5824518952739750458</id><published>2008-06-20T02:44:00.000-07:00</published><updated>2008-06-20T02:47:40.191-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Converting Delimited Data into a Multi-Valued IN-List : SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You have &lt;a name="idx-CHP-6-0283"&gt;&lt;/a&gt;delimited data that you  want to pass to the IN-list iterator of a WHERE clause. Consider the following  string:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;7654,7698,7782,7788&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You would like to use the string in a WHERE clause but the  following SQL fails because EMPNO is a numeric column:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename,sal,deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where empno in ( '7654,7698,7782,7788' )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This SQL fails because, while EMPNO is a numeric column, the IN  list is composed of a single string value. You want that string to be treated as  a comma-delimited list of numeric values.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;On the surface it may seem that SQL should do the work of  treating a delimited string as a list of delimited values for you, but that is  not the case. When a comma embedded within quotes is encountered, SQL can't  possibly know that signals a multi-valued list. SQL must treat everything  between the quotes as a single entity, as one string value. You must break the  string up into individual EMPNOs. The key to this solution is to walk the  string, but not into individual characters. You want to walk the string into  valid EMPNO values.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;By walking the string passed to the IN-list, you can you can  easily convert it to rows. The functions ROW_NUMBER, LOCATE, and SUBSTR are  particularly useful here:&lt;/p&gt;&lt;pre style="color: rgb(0, 0, 0);"&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select empno,ename,sal,deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3  where empno in (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4 select cast(substr(c,2,locate(',',c,2)-2) as integer) empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6 select substr(csv.emps,cast(iter.pos as integer)) as c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7   from (select ','||'7654,7698,7782,7788'||',' emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8          from t1) csv,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        (select id as pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10           from t100 ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  where iter.pos &lt;= length(csv.emps)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13  where length(c) &gt; 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14    and substr(c,1,1) = ','&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15        ) y&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 0, 0);" name="sqlckbk-CHP-6-SECT-11.2.2"&gt;&lt;/a&gt; &lt;h5 style="color: rgb(0, 0, 0);" class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p style="color: rgb(0, 0, 0);" class="docText"&gt;By walking the string passed to the IN-list, you can easily  convert it to rows:&lt;/p&gt;&lt;pre style="color: rgb(0, 0, 0);"&gt; &lt;span style="color: rgb(204, 0, 0);"&gt; 1 select empno, ename, sal, deptno &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3  where empno in&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5 select substring_index(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        substring_index(list.vals,',',iter.pos),',',-1) empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6   from (select id pos from t10) as iter,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        (select '7654,7698,7782,7788' as vals&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8           from t1) list&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9  where iter.pos &lt;=&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        (length(list.vals)-length(replace(list.vals,',','')))+1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        ) x&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a style="color: rgb(0, 0, 0);" name="sqlckbk-CHP-6-SECT-11.2.3"&gt;&lt;/a&gt; &lt;h5 style="color: rgb(0, 0, 0);" class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p style="color: rgb(0, 0, 0);" class="docText"&gt;By walking the string passed to the IN-list, you can you can  easily convert it to rows. The functions ROWNUM, SUBSTR, and INSTR are  particularly useful here:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select empno,ename,sal,deptno&lt;br /&gt;  2   from emp&lt;br /&gt;  3  where empno in (&lt;br /&gt;  4        select to_number(&lt;br /&gt;  5                   rtrim(&lt;br /&gt;  6                 substr(emps,&lt;br /&gt;  7                  instr(emps,',',1,iter.pos)+1,&lt;br /&gt;  8                  instr(emps,',',1,iter.pos+1)&lt;br /&gt;  9                  instr(emps,',',1,iter.pos)),',')) emps&lt;br /&gt; 10          from (select ','||'7654,7698,7782,7788'||',' emps from t1) csv,&lt;br /&gt; 11               (select rownum pos from emp) iter&lt;br /&gt; 12         where iter.pos &lt;= ((length(csv.emps)-  13                   length(replace(csv.emps,',')))/length(','))-1  14 ) &lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Postgres&lt;/h5&gt; &lt;p class="docText"&gt;By walking the string passed to the IN-list, you can easily  convert it to rows. The function SPLIT_PART makes it easy to parse the string  into individual numbers:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select ename,sal,deptno&lt;br /&gt;  2   from emp&lt;br /&gt;  3  where empno in (&lt;br /&gt;  4 select cast(empno as integer) as empno&lt;br /&gt;  5   from (&lt;br /&gt;  6 select split_part(list.vals,',',iter.pos) as empno&lt;br /&gt;  7   from (select id as pos from t10) iter,&lt;br /&gt;  8        (select ','||'7654,7698,7782,7788'||',' as vals&lt;br /&gt;  9           from t1) list&lt;br /&gt; 10  where iter.pos &lt;=  11        length(list.vals)-length(replace(list.vals,',',''))  12        ) z  13  where length(empno) &gt; 0&lt;br /&gt; 14        ) x&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;By walking the string passed to the IN-list, you can you can  easily convert it to rows. The functions ROW_NUMBER, CHARINDEX, and SUBSTRING  are particularly useful here:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select empno,ename,sal,deptno&lt;br /&gt;  2   from emp&lt;br /&gt;  3  where empno in (select substring(c,2,charindex(',',c,2)-2) as empno&lt;br /&gt;  4   from (&lt;br /&gt;  5 select substring(csv.emps,iter.pos,len(csv.emps)) as c&lt;br /&gt;  6   from (select ','+'7654,7698,7782,7788'+',' as emps&lt;br /&gt;  7           from t1) csv,&lt;br /&gt;  8        (select id as pos&lt;br /&gt;  9          from t100) iter&lt;br /&gt; 10  where iter.pos &lt;= len(csv.emps)  11       ) x  12  where len(c) &gt; 1&lt;br /&gt; 13    and substring(c,1,1) = ','&lt;br /&gt; 14       ) y&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The first and most important step in this solution is to walk  the string. Once you've accomplished that, all that's left is to parse the  string into individual, numeric values using your DBMS's provided  functions.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The inline view X (lines 611) walks the string. The idea in  this solution is to "walk through" the string, so that each row has one less  character than the one before it:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788,&lt;br /&gt; 7654,7698,7782,7788,&lt;br /&gt; 654,7698,7782,7788,&lt;br /&gt; 54,7698,7782,7788,&lt;br /&gt; 4,7698,7782,7788,&lt;br /&gt; ,7698,7782,7788,&lt;br /&gt; 7698,7782,7788,&lt;br /&gt; 698,7782,7788,&lt;br /&gt; 98,7782,7788,&lt;br /&gt; 8,7782,7788,&lt;br /&gt; ,7782,7788,&lt;br /&gt; 7782,7788,&lt;br /&gt; 782,7788,&lt;br /&gt; 82,7788,&lt;br /&gt; 2,7788,&lt;br /&gt; ,7788,&lt;br /&gt; 7788,&lt;br /&gt; 788,&lt;br /&gt; 88,&lt;br /&gt; 8,&lt;br /&gt; ,&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Notice that by enclosing the string in commas (the delimiter),  there's no need to make special checks as to where the beginning or end of the  string is.&lt;/p&gt; &lt;p class="docText"&gt;The next step is to keep only the values you want to use in the  IN-list. The values to keep are the ones with leading commas, with the exception  of the last row with its lone comma. Use &lt;a name="idx-CHP-6-0284"&gt;&lt;/a&gt;SUBSTR or  SUBSTRING to identify which rows have a leading comma, then keep all characters  found before the next comma in that row. Once that's done, cast the string to a  number so it can be properly evaluated against the numeric column EMPNO (lines  414):&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt; EMPNO&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   7654&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   7698&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   7782&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   7788&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to use the results in a subquery to return  the desired rows.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view (lines 59) walks the string. The expression on  line 10 determines how many values are in the string by finding the number of  commas (the delimiter) and adding one. The function SUBSTRING_INDEX (line 6)  returns all characters in the string before (to the left of ) the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of a comma (the delimiter):&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;+---------------------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | empno               |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +---------------------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7654                |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7654,7698           |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7654,7698,7782      |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7654,7698,7782,7788 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +---------------------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Those rows are then passed to another call to SUBSTRING_INDEX  (line 5); this time the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of the &lt;a name="idx-CHP-6-0285"&gt;&lt;/a&gt;delimited is 1, which causes all values to the right of  the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of the delimiter to be  kept:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +-------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | empno |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +-------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7654  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7698  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7782  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | 7788  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +-------+&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to plug the results into a subquery.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The first step is to walk the string:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select emps,pos&lt;br /&gt;   from (select ','||'7654,7698,7782,7788'||',' emps&lt;br /&gt;           from t1) csv,&lt;br /&gt;        (select rownum pos from emp) iter&lt;br /&gt;  where iter.pos &lt;=   ((length(csv.emps)-length(replace(csv.emps,',')))/length(','))-1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; EMPS                         POS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788,          1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788,          2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788,          3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788,          4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The number of rows returned represents the number of values in  your list. The values for POS are crucial to the query as they are needed to  parse the string into individual values. The strings are parsed using SUBSTR and  INSTR. POS is used to locate the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence  of the delimiter in each string. By enclosing the strings in commas, no special  checks are necessary to determine the beginning or end of a string. The values  passed to SUBSTR, INSTR (lines 79) locate the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th  and &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th+1 occurrence of the delimiter. By  subtracting the value returned for the current comma (the location in the string  where the current comma is) from the value returned bythe next comma (the  location in the string where the next comma is) you can extract each value from  the string:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;select substr(emps,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        instr(emps,',',1,iter.pos)+1,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        instr(emps,',',1,iter.pos+1)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        instr(emps,',',1,iter.pos)) emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select ','||'7654,7698,7782,7788'||',' emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           from t1) csv,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (select rownum pos from emp) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where iter.pos &lt;=&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   ((length(csv.emps)-length(replace(csv.emps,',')))/length(','))-1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  EMPS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7654,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7698,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7782,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7788,&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to remove the trailing comma from each value,  cast it to a number, and plug it into a subquery.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-11.3.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view Z (lines 69) walks the string. The number of  rows returned is determined by how many values are in the string. To find the  number of values in the string, subtract the size of the string without the  delimiter from the size of the string with the delimiter (line 9). The function  SPLIT_PART does the work of parsing the string. It looks for the value that  comes before the &lt;span class="docEmphasis"&gt;n&lt;/span&gt;th occurrence of the  delimiter:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select list.vals,&lt;br /&gt;        split_part(list.vals,',',iter.pos) as empno,&lt;br /&gt;        iter.pos&lt;br /&gt;   from (select id as pos from t10) iter,&lt;br /&gt;        (select ','||'7654,7698,7782,7788'||',' as vals&lt;br /&gt;           from t1) list&lt;br /&gt;  where iter.pos &lt;=         length(list.vals)-length(replace(list.vals,',',''))&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      vals             | empno | pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------------------+-------+-----&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788, |       |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788, | 7654  |   2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788, | 7698  |   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788, | 7782  |   4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ,7654,7698,7782,7788, | 7788  |   5&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to cast the values (EMPNO) to a number and  plug it into a subquery.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-5824518952739750458?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/5824518952739750458/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=5824518952739750458&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5824518952739750458'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5824518952739750458'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/converting-delimited-data-into-multi.html' title='Converting Delimited Data into a Multi-Valued IN-List : SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2419546698266692521</id><published>2008-06-17T22:04:00.000-07:00</published><updated>2008-06-17T22:06:39.285-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server : Creating a Delimited List from Table Rows</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to return table rows as values in a delimited list,  perhaps delimited &lt;a name="idx-CHP-6-0275"&gt;&lt;/a&gt;by commas, rather than in vertical  columns as they normally appear. You want to convert a result set from this:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO EMPS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 WARD&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;to this:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DEPTNO EMPS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------- ------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      10 CLARK,KING,MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 SMITH,JONES,SCOTT,ADAMS,FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 ALLEN,WARD,MARTIN,BLAKE,TURNER,JAMES&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Each DBMS requires a different approach to this problem. The  key is to take advantage of the built-in functions provided by your DBMS.  Understanding what is available to you will allow you to exploit your DBMS's  functionality and come up with creative solutions for a problem that is  typically not solved in SQL.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use recursive WITH to build the &lt;a name="idx-CHP-6-0276"&gt;&lt;/a&gt;delimited list:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1   with x (deptno, cnt, list, empno, len)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2     as (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select deptno, count(*) over (partition by deptno),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        cast(ename as varchar(100)), empno, 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7  select x.deptno, x.cnt, x.list ||','|| e.ename, e.empno, x.len+1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8   from emp e, x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9  where e.deptno = x.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    and e.empno &gt; x. empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12 select deptno,list&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13   from x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14  where len = cnt&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in function GROUP_CONCAT to build the delimited  list:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select deptno,&lt;br /&gt; 2        group_concat(ename order by empno separator, ',') as emps&lt;br /&gt; 3   from emp&lt;br /&gt; 4  group by deptno&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in function SYS_CONNECT_BY_PATH to build the  delimited list:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        ltrim(sys_connect_by_path(ename,','),',') emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4 select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        row_number() over&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7                 (partition by deptno order by empno) rn,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        count(*) over&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9                 (partition by deptno) cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12  where level = cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13  start with rn = 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14 connect by prior deptno = deptno and prior rn = rn-1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;PostgreSQL does not offer a standard built-in function for  creating a &lt;a name="idx-CHP-6-0277"&gt;&lt;/a&gt;delimited list, so it is necessary to know  how many values will be in the list in advance. Once you know the size of the  largest list, you can determine the number of values to append to create your  list by using standard transposition and concatenation:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        rtrim(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3            max(case when pos=1 then emps else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4            max(case when pos=2 then emps else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5            max(case when pos=3 then emps else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6            max(case when pos=4 then emps else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7            max(case when pos=5 then emps else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8            max(case when pos=6 then emps else '' end),','&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        ) as emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11 select a.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        a.ename||',' as emps,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13        d.cnt,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14        (select count(*) from emp b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15           where a.deptno=b.deptno and b.empno &lt;= a.empno) as pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16   from emp a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17        (select deptno, count(ename) as cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18           from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19           group by deptno) d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20  where d.deptno=a.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22  group by deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 23  order by 1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Use recursive WITH to build the delimited list:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  with x (deptno, cnt, list, empno, len)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2     as (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select deptno, count(*) over (partition by deptno),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        cast(ename as varchar(100)),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9 select x.deptno, x.cnt,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10        cast(x.list + ',' + e.ename as varchar(100)),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        e.empno, x.len+1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12   from emp e, x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13 where e.deptno = x.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14   and e.empno &gt; x. empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15            )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16 select deptno,list&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17   from x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18  where len = cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19  order by 1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Being able to create &lt;a name="idx-CHP-6-0278"&gt;&lt;/a&gt;delimited lists  in SQL is useful because it is a common requirement. Yet each DBMS offers a  unique method for building such a list in SQL. There's very little commonality  between the vendor-specific solutions; the techniques vary from using recursion,  to hierarchal functions, to classic transposition, to aggregation.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The solution for these two databases differ slightly in syntax  (the concatenation operators are "||" for DB2 and "+" for SQL Server), but the  technique is the same. The first query in the WITH clause (upper portion of the  UNION ALL) returns the following information about each employee: the  department, the number of employees in that department, the name, the ID, and a  constant 1 (which at this point doesn't do anything). Recursion takes place in  the second query (lower half of the UNION ALL) to build the list. To understand  how the list is built, examine the following excerpts from the solution: first,  the third SELECT-list item from the second query in the union:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;x.list ||','|| e.ename&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;and then the WHERE clause from that same query:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; where e.deptno = x.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   and e.empno &gt; x.empno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The solution works by first ensuring the employees are in the  same department. Then, for every employee returned by the upper portion of the  UNION ALL, append the name of the employees who have a greater EMPNO. By doing  this, you ensure that no employee will have his own name appended. The  expression&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;x.len+1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;increments LEN (which starts at 1) every time an employee has  been evaluated. When the incremented value equals the number of employees in the  department:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;where len = cnt&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;you know you have evaluated all the employees and have  completed building the list. That is crucial to the query as it not only signals  when the list is complete, but also stops the recursion from running longer than  necessary.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The function GROUP_CONCAT does all the work. It concatenates  the values found in the column passed to it, in this case ENAME. It's an  aggregate function, thus the need for GROUP BY in the query.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The first step to understanding the Oracle query is to break it  down. Running the inline view by itself (lines 410), you generate a result set  that includes the following for each employee: her department, her name, a rank  within her respective department that is derived by an ascending sort on EMPNO,  and a count of all employees in her department. For example:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         row_number() over&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   (partition by deptno order by empno) rn,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         count(*) over (partition by deptno) cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO ENAME      RN CNT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ---------- -- ---&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 CLARK       1   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 KING        2   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 MILLER      3   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SMITH       1   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 JONES       2   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SCOTT       3   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 ADAMS       4   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 FORD        5   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 ALLEN       1   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 WARD        2   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 MARTIN      3   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 BLAKE       4   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 TURNER      5   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 JAMES       6   6&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The purpose of the rank (aliased RN in the query) is to allow  you to walk the tree. Since the function ROW_NUMBER generates an enumeration  starting from one with no duplicates or gaps, just subtract one (from the  current value) to reference a prior (or parent) row. For example, the number  prior to 3 is 3 minus 1, which equals 2. In this context, 2 is the parent of 3;  you can observe this on line 12. Additionally, the lines&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;start with rn = 1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; connect by prior deptno = deptno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;identify the root for each DEPTNO as having RN equal to 1 and  create a new list whenever a new department is encountered (whenever a new  occurrence of 1 is found for RN).&lt;/p&gt; &lt;p class="docText"&gt;At this point, it's important to stop and look at the ORDER BY  portion of the ROW_NUMBER function. Keep in mind the names are ranked by EMPNO  and the list will be created in that order. The number of employees per  department is calculated (aliased CNT) and is used to ensure that the query  returns only the list that has all the employee names for a department. This is  done because SYS_CONNECT_ BY_PATH builds the list iteratively, and you do not  want to end up with partial &lt;a name="idx-CHP-6-0279"&gt;&lt;/a&gt;lists.&lt;/p&gt; &lt;p class="docText"&gt;For heirarchical &lt;a name="idx-CHP-6-0280"&gt;&lt;/a&gt;queries, the  pseudocolumn LEVEL starts with 1 (for queries not using CONNECT BY, LEVEL is 0,  unless you are on 10g and later when LEVEL is only available when using CONNECT  BY) and increments by one after each employee in a department has been evaluated  (for each level of depth in the hierarchy). Because of this, you know that once  LEVEL reaches CNT, you have reached the last EMPNO and will have a complete  list.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-10.3.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;PostgreSQL's solution requires you to know in advance the  maximum number of employees in any one department. Running the inline view by  itself (lines 1118) generates a result set that includes (for each employee) his  department, his name with a comma appended, the number of employees in his  department, and the number of employees who have an EMPNO that is less than  his:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;deptno  |  emps    | cnt | pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------+----------+-----+-----&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20  |  SMITH,  |   5 |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  ALLEN,  |   6 |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  WARD,   |   6 |   2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20  |  JONES,  |   5 |   2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  MARTIN, |   6 |   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  BLAKE,  |   6 |   4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10  |  CLARK,  |   3 |   1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20  |  SCOTT,  |   5 |   3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10  |  KING,   |   3 |   2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  TURNER, |   6 |   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20  |  ADAMS,  |   5 |   4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30  |  JAMES,  |   6 |   6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20  |  FORD,   |   5 |   5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10  |  MILLER, |   3 |   3&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The scalar subquery, POS (lines 14-15), is used to rank each  employee by EMPNO. For example, the line&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;max(case when pos = 1 then ename else '' end)||&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;evaluates whether or not POS equals 1. The CASE expression  returns the employee name when POS is 1, and otherwise returns NULL.&lt;/p&gt; &lt;p class="docText"&gt;You must query your table first to find the largest number of  values that could be in any one list. Based on the EMP table, the largest number  of employees in any one department is six, so the largest number of items in a  list is six.&lt;/p&gt; &lt;p class="docText"&gt;The next step is to begin creating the list. Do this by  performing some conditional logic (in the form of CASE expressions) on the rows  returned from the inline view.&lt;/p&gt; &lt;p class="docText"&gt;You must write as many CASE expressions as there are possible  values to be concatenated together.&lt;/p&gt; &lt;p class="docText"&gt;If POS equals one, the current name is added to the list. The  second CASE expression evaluates whether or not POS equals two; if it does, then  the second name is appended to the first. If there is no second name, then an  additional comma is appended to the first name (this process is repeated for  each distinct value of POS until the last one is reached).&lt;/p&gt; &lt;p class="docText"&gt;The use of the MAX function is necessary because you want to  build only one list per department (you can also use MIN; it makes no difference  in this case, since POS returns only one value for each case evaluation).  Whenever an aggregate function is used, any items in the SELECT list not acted  upon by the aggregate must be specified in the GROUP BY clause. This guarantees  you will have only one row per item in the SELECT list not acted upon by the  aggregate function.&lt;/p&gt; &lt;p class="docText"&gt;Notice that you also need the function RTRIM to remove trailing  commas; the number of commas will always be equal to the maximum number of  values that could potentially be in a list (in this case, six).&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2419546698266692521?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2419546698266692521/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2419546698266692521&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2419546698266692521'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2419546698266692521'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-creating-delimited-list-from.html' title='SQL Server : Creating a Delimited List from Table Rows'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-4109559167208450295</id><published>2008-06-17T03:11:00.000-07:00</published><updated>2008-06-17T03:14:16.743-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL - Ordering by a Number in a String</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want order your result set based on a number within a  string. Consider the following view:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create view V as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.ename ||' '||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         cast(e.empno as char(4))||' '||&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         d.dname as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp e, dept d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where e.deptno=d.deptno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This view returns the following data:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; DATA&lt;br /&gt;  ----------------------------&lt;br /&gt;  CLARK   7782 ACCOUNTING&lt;br /&gt;  KING    7839 ACCOUNTING&lt;br /&gt;  MILLER  7934 ACCOUNTING&lt;br /&gt;  SMITH   7369 RESEARCH&lt;br /&gt;  JONES   7566 RESEARCH&lt;br /&gt;  SCOTT   7788 RESEARCH&lt;br /&gt;  ADAMS   7876 RESEARCH&lt;br /&gt;  FORD    7902 RESEARCH&lt;br /&gt;  ALLEN   7499 SALES&lt;br /&gt;  WARD    7521 SALES&lt;br /&gt;  MARTIN  7654 SALES&lt;br /&gt;  BLAKE   7698 SALES&lt;br /&gt;  TURNER  7844 SALES&lt;br /&gt;  JAMES   7900 SALES&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to order the results based on the employee number,  which falls between the employee name and respective department:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH    7369 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN    7499 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD     7521 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES    7566 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN   7654 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE    7698 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK    7782 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT    7788 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING     7839 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER   7844 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS    7876 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES    7900 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD     7902 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER   7934 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Each solution uses functions and syntax specific to its DBMS,  but the method (making use of the built-in functions &lt;a name="idx-CHP-6-0265"&gt;&lt;/a&gt;REPLACE and &lt;a name="idx-CHP-6-0266"&gt;&lt;/a&gt;TRANSLATE) is the  same for each. The idea is to use REPLACE and TRANSLATE to remove non-digits  from the &lt;a name="idx-CHP-6-0267"&gt;&lt;/a&gt;strings, leaving only the numeric values  upon which to sort.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions REPLACE and TRANSLATE to order &lt;a name="idx-CHP-6-0268"&gt;&lt;/a&gt;by numeric characters in a string:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  order by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4         cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5      replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6    translate(data,repeat('#',length(data)),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7      replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8    translate(data,'##########','0123456789'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 9             '#','')),'#','') as integer)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions REPLACE and TRANSLATE to order &lt;a name="idx-CHP-6-0269"&gt;&lt;/a&gt;by numeric characters in a string:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  order by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4         to_number(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6         translate(data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8         translate(data,'0123456789','##########'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 9                  '#'),rpad('#',20,'#')),'#'))&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions REPLACE and TRANSLATE to order by  numeric characters in a string:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  order by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4         cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5      replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6    translate(data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7      replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8    translate(data,'0123456789','##########'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 9             '#',''),rpad('#',20,'#')),'#','') as integer)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;As of the time of this writing, neither vendor supplies the  TRANSLATE function.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-9.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The purpose of view V is only to supply rows on which to  demonstrate this recipe's solution. The view simply concatenates several columns  from the EMP table. The solution shows how to take such concatenated text as  input and sort it by the employee number embedded within.&lt;/p&gt; &lt;p class="docText"&gt;The ORDER BY clause in each solution may look a bit  intimidating but performs quite well and is pretty straightforward once you  examine it piece by piece. To order by the numbers in the string, it's easiest  to remove any characters that are not numbers. Once the non-numeric characters  are removed all that is left to do is cast the string of numerals into a number,  then sort as you see fit. Before examining each function call it is important to  understand the order in which each function is called. Starting with the  innermost call, TRANSLATE (line 8 from each of the original solutions), you see  that:&lt;/p&gt; &lt;div style="font-weight: bold;"&gt; &lt;ol class="docList" type="1"&gt;&lt;li&gt; &lt;div style="font-weight: normal;"&gt; &lt;p class="docList"&gt;TRANSLATE (line 8) is called and the results are returned  to&lt;/p&gt;&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div style="font-weight: normal;"&gt; &lt;p class="docList"&gt;REPLACE (line 7) and those results are returned to&lt;/p&gt;&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div style="font-weight: normal;"&gt; &lt;p class="docList"&gt;TRANSLATE (line 6) and those results are returned to&lt;/p&gt;&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div style="font-weight: normal;"&gt; &lt;p class="docList"&gt;REPLACE (line 5) and those results are returned and  finally&lt;/p&gt;&lt;/div&gt; &lt;/li&gt;&lt;li&gt; &lt;div style="font-weight: normal;"&gt; &lt;p class="docList"&gt;cast into a number&lt;/p&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt; &lt;p class="docText"&gt;The first step is to convert the numbers into characters that  do not exist in the rest of the string. For this example, I chose "#" and used  TRANSLATE to convert all non-numeric characters into occurrences of "#". For  example, the following query shows the original data on the left and the results  from the first translation:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data,&lt;br /&gt;        translate(data,'0123456789','##########') as tmp&lt;br /&gt;   from V&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                           TMP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ------------------------------ -----------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK   7782 ACCOUNTING        CLARK   #### ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING    7839 ACCOUNTING        KING    #### ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER  7934 ACCOUNTING        MILLER  #### ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH   7369 RESEARCH          SMITH   #### RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES   7566 RESEARCH          JONES   #### RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT   7788 RESEARCH          SCOTT   #### RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS   7876 RESEARCH          ADAMS   #### RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD    7902 RESEARCH          FORD    #### RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN   7499 SALES             ALLEN   #### SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  WARD    7521 SALES             WARD    #### SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MARTIN  7654 SALES             MARTIN  #### SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE   7698 SALES             BLAKE   #### SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER 7844 SALES          TURNER  #### SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES  7900 SALES          JAMES   #### SALES&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;TRANSLATE finds the numerals in each string and converts each  one to to the "#" character. The modified &lt;a name="idx-CHP-6-0270"&gt;&lt;/a&gt;strings are  then returned to REPLACE (line 11), which removes all occurrences of "#":&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; translate(data,'0123456789','##########'),'#') as tmp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from V&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                           TMP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ------------------------------ -------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK   7782 ACCOUNTING        CLARK   ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING    7839 ACCOUNTING        KING    ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER  7934 ACCOUNTING        MILLER  ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH   7369 RESEARCH          SMITH   RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES   7566 RESEARCH          JONES   RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT   7788 RESEARCH          SCOTT   RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS   7876 RESEARCH          ADAMS   RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD    7902 RESEARCH          FORD    RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN   7499 SALES             ALLEN   SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  WARD    7521 SALES             WARD    SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MARTIN  7654 SALES             MARTIN  SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE   7698 SALES             BLAKE   SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER  7844 SALES             TURNER  SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES   7900 SALES             JAMES   SALES&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;a name="idx-CHP-6-0271"&gt;&lt;/a&gt;strings are then returned to  TRANSLATE once again, but this time it's the second (outermost) TRANSLATE in the  solution. TRANSLATE searches the original string for any characters that match  the characters in TMP. If any are found, they too are converted to "#"s. This  conversion allows all non-numeric characters to be treated as a single character  (because they are all transformed to the same character):&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data, translate(data,&lt;br /&gt;              replace(&lt;br /&gt;              translate(data,'0123456789','##########'),&lt;br /&gt;              '#'),&lt;br /&gt;              rpad('#',length(data),'#')) as tmp&lt;br /&gt;   from V&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                           TMP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------ ---------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK   7782 ACCOUNTING        ########7782###########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING    7839 ACCOUNTING        ########7839###########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER  7934 ACCOUNTING        ########7934###########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH   7369 RESEARCH          ########7369#########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES   7566 RESEARCH          ########7566#########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT   7788 RESEARCH          ########7788#########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS   7876 RESEARCH          ########7876#########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD    7902 RESEARCH          ########7902#########&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN   7499 SALES             ########7499######&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD    7521 SALES             ########7521######&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN  7654 SALES             ########7654######&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE   7698 SALES             ########7698######&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER  7844 SALES             ########7844######&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES   7900 SALES             ########7900######&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The next step is to remove all "#" characters through a call to  REPLACE (line 8), leaving you with only numbers:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data, replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              translate(data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;            translate(data,'0123456789','##########'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                      '#'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                      rpad('#',length(data),'#')),'#') as tmp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from V&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                           TMP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------ -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK   7782 ACCOUNTING        7782&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING    7839 ACCOUNTING        7839&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER  7934 ACCOUNTING        7934&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH   7369 RESEARCH          7369&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES   7566 RESEARCH          7566&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT   7788 RESEARCH          7788&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS   7876 RESEARCH          7876&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD    7902 RESEARCH          7902&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN   7499 SALES             7499&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD    7521 SALES             7521&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN  7654 SALES             7654&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE   7698 SALES             7698&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER  7844 SALES             7844&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES   7900 SALES             7900&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Finally, cast TMP to a number (line 4) using the appropriate  DBMS function (often CAST) to accomplish this:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data, to_number(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;               replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              translate(data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        translate(data,'0123456789','##########'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  '#'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  rpad('#',length(data),'#')),'#')) as tmp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from V&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                                  TMP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------ ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK   7782 ACCOUNTING              7782&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING    7839 ACCOUNTING              7839&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER  7934 ACCOUNTING              7934&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH   7369 RESEARCH                7369&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES   7566 RESEARCH                7566&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT   7788 RESEARCH                7788&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS   7876 RESEARCH                7876&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD    7902 RESEARCH                7902&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN   7499 SALES                   7499&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD    7521 SALES                   7521&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN  7654 SALES                   7654&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE   7698 SALES                   7698&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER  7844 SALES                   7844&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES   7900 SALES                   7900&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;When developing &lt;a name="idx-CHP-6-0272"&gt;&lt;/a&gt;queries like this,  it's helpful to work with your expressions in the SELECT list. That way, you can  easily view the intermediate results as you work toward a final solution.  However, because the point of this recipe is to order the results, ultimately  you should place all the function calls into the ORDER &lt;a name="idx-CHP-6-0273"&gt;&lt;/a&gt;BY clause:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  order by&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         to_number(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         translate( data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         translate( data,'0123456789','##########'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   '#'),rpad('#',length(data),'#')),'#'))&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH   7369 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN   7499 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD    7521 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES   7566 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN  7654 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE   7698 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK   7782 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT   7788 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING    7839 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER  7844 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS   7876 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES   7900 SALES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD    7902 RESEARCH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER  7934 ACCOUNTING&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;As a final note, the data in the view is comprised of three  fields, only one being numeric. Keep in mind that if there had been multiple  numeric fields, they would have all been concatenated into one number before the  rows were sorted.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-4109559167208450295?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/4109559167208450295/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=4109559167208450295&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4109559167208450295'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4109559167208450295'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-ordering-by-number-in-string.html' title='SQL - Ordering by a Number in a String'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-664027547398042583</id><published>2008-06-17T03:10:00.000-07:00</published><updated>2008-06-17T03:11:35.219-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Ordering by Parts of a String : SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to order your result set based on a substring.  Consider the following records:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want the records to be ordered based on the &lt;span class="docEmphasis"&gt;last&lt;/span&gt; two characters of each name:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-8.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The key to this solution is to find and use your DBMS's  built-in function to extract the substring on which you wish to sort. This is  typically done with the &lt;a name="idx-CHP-6-0263"&gt;&lt;/a&gt;SUBSTR function.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-8.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2, Oracle, MySQL, and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use a combination of the built-in functions LENGTH and SUBSTR  to order by a specific part of a string:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;1 select ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  order by substr(ename,length(ename)-1,)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-8.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Use functions SUBSTRING and LEN to order by a specific part of  a string:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;1 select ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  order by substring(ename,len(ename)-1,2)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-8.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;By using a SUBSTR expression in your ORDER BY clause, you can  pick any part of a string to use in ordering a result set. You're not limited to  SUBSTR either. You can order rows by the result of almost any expression.&lt;/p&gt;&lt;a href="23961536.html"&gt;&lt;img alt="" src="images/pixel.jpg" border="0" height="1" width="1" /&gt;&lt;/a&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-664027547398042583?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/664027547398042583/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=664027547398042583&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/664027547398042583'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/664027547398042583'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/ordering-by-parts-of-string-sql.html' title='Ordering by Parts of a String : SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-384970099102241717</id><published>2008-06-13T01:30:00.000-07:00</published><updated>2008-06-13T01:31:33.540-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Extracting Initials from a Name - SQL SERVER</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want convert a full name into initials. Consider the  following name:&lt;/p&gt;&lt;pre&gt;  Stewie Griffin&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You would like to return:&lt;/p&gt;&lt;pre&gt;  S.G.&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;It's important to keep in mind that SQL does not provide the  flexibility of languages such as C or Python; therefore, creating a generic  solution to deal with any name format is not something particularly easy to do  in SQL. The solutions presented here expect the &lt;a name="idx-CHP-6-0256"&gt;&lt;/a&gt;names  to be either first and last name, or first, middle name/middle initial, and last  name.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions REPLACE, TRANSLATE, and REPEAT to  extract the initials:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select replace(&lt;br /&gt; 2        replace(&lt;br /&gt; 3        translate(replace('Stewie Griffin', '.', ''),&lt;br /&gt; 4                  repeat('#',26),&lt;br /&gt; 5                  'abcdefghijklmnopqrstuvwxyz'),&lt;br /&gt; 6                   '#','' ), ' ','.' )&lt;br /&gt; 7                  ||'.'&lt;br /&gt; 8   from t1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions CONCAT, CONCAT_WS, SUBSTRING, and  SUBSTRING_ INDEX to extract the &lt;a name="idx-CHP-6-0257"&gt;&lt;/a&gt;initials:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select case&lt;br /&gt;  2          when cnt = 2 then&lt;br /&gt;  3            trim(trailing '.' from&lt;br /&gt;  4                 concat_ws('.',&lt;br /&gt;  5                  substr(substring_index(name,' ',1),1,1),&lt;br /&gt;  6                  substr(name,&lt;br /&gt;  7                         length(substring_index(name,' ',1))+2,1),&lt;br /&gt;  8                  substr(substring_index(name,' ',-1),1,1),&lt;br /&gt;  9                  '.'))&lt;br /&gt; 10          else&lt;br /&gt; 11            trim(trailing '.' from&lt;br /&gt; 12                 concat_ws('.',&lt;br /&gt; 13                  substr(substring_index(name,' ',1),1,1),&lt;br /&gt; 14                  substr(substring_index(name,' ',-1),1,1)&lt;br /&gt; 15                  ))&lt;br /&gt; 16          end as initials&lt;br /&gt; 17   from (&lt;br /&gt; 18 select name,length(name)-length(replace(name,' ','')) as cnt&lt;br /&gt; 19   from (&lt;br /&gt; 20 select replace('Stewie Griffin','.','') as name from t1&lt;br /&gt; 21        )y&lt;br /&gt; 22        )x&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions REPLACE, TRANSLATE, and RPAD to  extract the initials:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2        replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        translate(replace('Stewie Griffin', '.', ''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4                  'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5                  rpad('#',26,'#') ), '#','' ),' ','.' ) ||'.'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6   from t1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;As of the time of this writing, neither TRANSLATE nor CONCAT_WS  is supported in SQL Server.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;By isolating the capital letters you can extract the initials  from a name. The following sections describe each vendor-specific solution in  detail.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;The REPLACE function will remove any periods in the name (to  handle middle &lt;a name="idx-CHP-6-0258"&gt;&lt;/a&gt;initials), and the TRANSLATE function  will convert all non-uppercase letters to #.&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select translate(replace('Stewie Griffin', '.', ''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  repeat('#',26),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  'abcdefghijklmnopqrstuvwxyz')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TRANSLATE('STE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; S##### G######&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;At this point, the initials are the characters that are not #.  The function REPLACE is then used to remove all the # characters:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        translate(replace('Stewie Griffin', '.', ''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   repeat('#',26),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   'abcdefghijklmnopqrstuvwxyz'),'#','')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; REP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; S G&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The next step is to replace the white space with a period by  using REPLACE again:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; &lt;b&gt;select replace(&lt;br /&gt;        replace(&lt;br /&gt;        translate(replace('Stewie Griffin', '.', ''),&lt;br /&gt;                  repeat('#',26),&lt;br /&gt;                 'abcdefghijklmnopqrstuvwxyz'),'#',''),' ','.') || '.'&lt;br /&gt;   from t1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; REPLA&lt;br /&gt; -----&lt;br /&gt; S.G&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to append a decimal to the end of the  initials.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;The REPLACE function will remove any periods in the name (to  handle middle initials), and the TRANSLATE function will convert all  non-uppercase letters to '#'.&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; &lt;b&gt;&lt;br /&gt; select translate(replace('Stewie Griffin','.',''),&lt;br /&gt;                  'abcdefghijklmnopqrstuvwxyz',&lt;br /&gt;                  rpad('#',26,'#'))&lt;br /&gt;   from t1&lt;/b&gt;&lt;br /&gt; &lt;br /&gt; TRANSLATE('STE&lt;br /&gt; --------------&lt;br /&gt; S##### G######&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;At this point, the &lt;a name="idx-CHP-6-0259"&gt;&lt;/a&gt;initials are the  characters that are not "#". The function REPLACE is then used to remove all the  # characters:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        translate(replace('Stewie Griffin','.',''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   rpad('#',26,'#')),'#','')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; REP&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; S G&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The next step is to replace the white space with a period by  using REPLACE again:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      translate(replace('Stewie Griffin','.',''),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                rpad('#',26,'#') ),'#',''),' ','.') || '.'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; REPLA&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -----&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; S.G&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The final step is to append a decimal to the end of the  initials.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-7.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The inline view Y is used to remove any period from the name.  The inline view X finds the number of white spaces in the name so the SUBSTR  function can be called the correct number of times to extract the initials. The  three calls to SUBSTRING_ INDEX parse the string into individual &lt;a name="idx-CHP-6-0260"&gt;&lt;/a&gt;names based on the location of the white space. Because  there is only a first and last name, the code in the ELSE portion of the case  statement is executed:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select substr(substring_index(name, ' ',1),1,1) as a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        substr(substring_index(name,' ',-1),1,1) as b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select 'Stewie Griffin' as name from t1) x&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; A B&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; - -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; S G&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;If the name in question has a middle name or initial, the  initial would be returned by executing&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;substr(name,length(substring_index(name, ' ',1))+2,1)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;which finds the end of the first name then moves two spaces to  the beginning of the middle name or initial; that is, the start position for  SUBSTR. Because only onecharacter is kept, the middle name or initial is  successfully returned. The &lt;a name="idx-CHP-6-0261"&gt;&lt;/a&gt;initials are then passed  to CONCAT_WS, which separates the initials by a period:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; &lt;b&gt;&lt;br /&gt; select concat_ws('.',&lt;br /&gt;                  substr(substring_index(name, ' ',1),1,1),&lt;br /&gt;                  substr(substring_index(name,' ',-1),1,1),&lt;br /&gt;                  '.' ) a&lt;br /&gt;   from (select 'Stewie Griffin' as name from t1) x&lt;/b&gt;&lt;br /&gt; &lt;br /&gt; A&lt;br /&gt; -----&lt;br /&gt; S.G..&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The last step is to trim the extraneous period from the  initials.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-384970099102241717?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/384970099102241717/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=384970099102241717&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/384970099102241717'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/384970099102241717'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/extracting-initials-from-name-sql.html' title='Extracting Initials from a Name - SQL SERVER'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6976814169616286299</id><published>2008-06-13T01:28:00.000-07:00</published><updated>2008-06-13T01:29:57.380-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Determining Whether a String Is Alphanumeric : SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to return rows from a table only when a column of  interest contains no characters other than numbers and letters. Consider the  following view V (SQL Server users will use the operator "+" for concatenation  instead of "||"):&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create view V as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename||', $'|| cast(sal as char(4)) ||'.00' as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename|| cast(deptno as char(4)) as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=30&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The view V represents your table, and it returns the  following:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; DATA&lt;br /&gt; --------------------&lt;br /&gt; CLARK&lt;br /&gt; KING&lt;br /&gt; MILLER&lt;br /&gt; SMITH, $800.00&lt;br /&gt; JONES, $2975.00&lt;br /&gt; SCOTT, $3000.00&lt;br /&gt; ADAMS, $1100.00&lt;br /&gt; FORD, $3000.00&lt;br /&gt; ALLEN30&lt;br /&gt; WARD30&lt;br /&gt; MARTIN30&lt;br /&gt; BLAKE30&lt;br /&gt; TURNER30&lt;br /&gt; JAMES30&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;However, from the view's data you want to return only the  following records:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER30&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES30&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;In short, you wish to omit those rows containing data other  than letters and digits.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;It may seem intuitive at first to solve the problem by  searching for all the possible non-&lt;a name="idx-CHP-6-0249"&gt;&lt;/a&gt;alphanumeric  characters that can be found in a string, but, on the contrary, you will find it  easier to do the exact opposite: find all the alphanumeric characters. By doing  so, you can treat all the alphanumeric characters as one by converting them to  one single character. The reason you want to do this is so the alphanumeric  characters can be manipulated together, as a whole. Once you've generated a copy  of the string in which all alphanumeric characters are represented by a single  character of your choosing, it is easy to isolate the alphanumeric characters  from any other characters.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the function TRANSLATE to convert all &lt;a name="idx-CHP-6-0250"&gt;&lt;/a&gt;alphanumeric characters to a single character, then  identify any rows that have characters other than the converted alphanumeric  character. For DB2 users, the CAST function calls in view V are necessary;  otherwise, the view cannot be created due to type conversion errors. Take extra  care when working with casts to CHAR as they are fixed length (padded):&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;br /&gt; 2   from V&lt;br /&gt; 3  where translate(lower(data),&lt;br /&gt; 4                  repeat('a',36),&lt;br /&gt; 5                  '0123456789abcdefghijklmnopqrstuvwxyz') =&lt;br /&gt; 6                  repeat('a',length(data))&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The syntax for view V is slightly different in MySQL:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create view V as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select concat(ename,', $',sal,'.00') as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select concat(ename,deptno) as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where deptno=30&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Use a regular expression to easily find rows that contain  non-alphanumeric data:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where data regexp '[^0-9a-zA-Z]' = 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the function TRANSLATE to convert all alphanumeric  characters to a single character, then identify any rows that have characters  other than the converted alphanumeric character. The CAST function calls in view  V are not needed for Oracle and PostgreSQL. Take extra care when working with  casts to CHAR as they are fixed length (padded). If you decide to cast, cast to  VARCHAR or VARCHAR2:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from V&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where translate(lower(data),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4                  '0123456789abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5                  rpad('a',36,'a')) = rpad('a',length(data),'a')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Because SQL Server does not support a &lt;a name="idx-CHP-6-0251"&gt;&lt;/a&gt;TRANSLATE function, you must walk each row and find any  that contains a character that contains a non-&lt;a name="idx-CHP-6-0252"&gt;&lt;/a&gt;alphanumeric value. That can be done many ways, but the  following solution uses an ASCII-value evaluation:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select v.data, iter.pos,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        substring(v.data,iter.pos,1) c,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        ascii(substring(v.data,iter.pos,1)) val&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6   from v,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        ( select id as pos from t100 ) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8  where iter.pos &lt;= len(v.data)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10  group by data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11  having min(val) between 48 and 122&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The key to these solutions is being able to reference multiple  characters concurrently. By using the function TRANSLATE you can easily  manipulate all numbers or all characters without having to "iterate" and inspect  each character one by one.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2, Oracle, and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Only 9 of the 14 rows from view V are alphanumeric. To find the  rows that are alphanumeric only, simply use the function TRANSLATE. In this  example, TRANSLATE converts characters 09 and az to "a". Once the conversion is  done, the converted row is then compared with a string of all "a" with the same  length (as the row). If the length is the same, then you know all the characters  are alphanumeric and nothing else.&lt;/p&gt; &lt;p class="docText"&gt;By using the TRANSLATE function (using the Oracle syntax):&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; where translate(lower(data),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                 '0123456789abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                  rpad('a',36,'a'))&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;you convert all numbers and letters into a distinct character  (I chose "a"). Once the data is converted, all strings that are indeed  alphanumeric can be identified as a string comprising only a single character  (in this case, "a"). This can be seen by running TRANSLATE by itself:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data, translate(lower(data),&lt;br /&gt;                   '0123456789abcdefghijklmnopqrstuvwxyz',&lt;br /&gt;                    rpad('a',36,'a'))&lt;br /&gt;   from V&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                 TRANSLATE(LOWER(DATA)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -------------------- ---------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK                aaaaa&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH, $800.00       aaaaa, $aaa.aa&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN30              aaaaaaa&lt;/span&gt;&lt;br /&gt; …&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The &lt;a name="idx-CHP-6-0253"&gt;&lt;/a&gt;alphanumeric values are  converted, but the string lengths have not been modified. Because the lengths  are the same, the rows to keep are the ones for which the call to TRANSLATE  returns all a's. You keep those rows, rejecting the others, by comparing each  original string's length with the length of its corresponding string of a's:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data, translate(lower(data),&lt;br /&gt;                   '0123456789abcdefghijklmnopqrstuvwxyz',&lt;br /&gt;                    rpad('a',36,'a')) translated,&lt;br /&gt;         rpad('a',length(data),'a') fixed&lt;br /&gt;   from V&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                 TRANSLATED           FIXED&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -------------------- -------------------- ----------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK                aaaaa                aaaaa&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH, $800.00       aaaaa, $aaa.aa       aaaaaaaaaaaaaa&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN30              aaaaaaa              aaaaaaa&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; …&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The last step is to keep only the strings where TRANSLATED  equals FIXED.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;The expression in the WHERE clause:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;where data regexp '[^0-9a-zA-Z]' = 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;causes rows that have only numbers or characters to be  returned. The value ranges in the brackets, "0-9a-zA-Z", represent all possible  numbers and letters. The character "^" is for negation, so the expression can be  stated as "not numbers or letters." A return value of 1 is true and 0 is false,  so the whole expression can be stated as "return rows where anything other than  numbers and letters is false."&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-6.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The first step is to walk each row returned by view V. Each  character in the value returned for DATA will itself be returned as a row. The  values returned by C represent each individual character for the values returned  by DATA:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;+-----------------+------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | data            | pos  | c    | val  |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; +-----------------+------+------+------+&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    1 | A    |   65 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    2 | D    |   68 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    3 | A    |   65 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    4 | M    |   77 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    5 | S    |   83 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    6 | ,    |   44 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    7 |      |   32 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    8 | $    |   36 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |    9 | 1    |   49 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   10 | 1    |   49 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   11 | 0    |   48 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   12 | 0    |   48 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   13 | .    |   46 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   14 | 0    |   48 |&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; | ADAMS, $1100.00 |   15 | 0    |   48 |&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Inline view Z not only returns each character in the column  DATA row by row, it also provides the ASCII value for each character. For this  particular implementation of SQL Server, the range 48122 represents &lt;a name="idx-CHP-6-0254"&gt;&lt;/a&gt;alphanumeric characters. With that knowledge, you can  group each row in DATA and filter out any such that the minimum ASCII value is  not in the 48122 range.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6976814169616286299?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6976814169616286299/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6976814169616286299&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6976814169616286299'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6976814169616286299'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/determining-whether-string-is.html' title='Determining Whether a String Is Alphanumeric : SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8253500756938862096</id><published>2008-06-13T01:25:00.000-07:00</published><updated>2008-06-13T01:28:21.358-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server : Separating Numeric and Character Data</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You have (unfortunately) stored numeric data along with  character data together in one column. You want to separate the character data  from the numeric data. Consider the following result set:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; DATA&lt;br /&gt; ---------------&lt;br /&gt; SMITH800&lt;br /&gt; ALLEN1600&lt;br /&gt; WARD1250&lt;br /&gt; JONES2975&lt;br /&gt; MARTIN1250&lt;br /&gt; BLAKE2850&lt;br /&gt; CLARK2450&lt;br /&gt; SCOTT3000&lt;br /&gt; KING5000&lt;br /&gt; TURNER1500&lt;br /&gt; ADAMS1100&lt;br /&gt; JAMES950&lt;br /&gt; FORD3000&lt;br /&gt; MILLER1300&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You would like the result to be:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME             SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH             800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN            1600&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD             1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES            2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN           1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE            2850&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK            2450&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT            3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING             5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER           1500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS            1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES             950&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD             3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER           1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-5.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the built-in functions &lt;a name="idx-CHP-6-0242"&gt;&lt;/a&gt;TRANSLATE  and &lt;a name="idx-CHP-6-0243"&gt;&lt;/a&gt;REPLACE to isolate the character from the &lt;a name="idx-CHP-6-0244"&gt;&lt;/a&gt;numeric data. Like other recipes in this chapter, the  trick is to use TRANSLATE to transform multiple characters into a single  character you can reference. This way you are no longer searching for multiple  numbers or characters, rather one character to represent all numbers or one  character to represent all characters.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-5.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the functions TRANSLATE and REPLACE to isolate and separate  the numeric from the character data:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2      translate(data,'0000000000','0123456789'),'0','') ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3        cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4      replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    translate(lower(data),repeat('z',26),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6           'abcdefghijklmnopqrstuvwxyz'),'z','') as integer) sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7    from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8  select ename||cast(sal as char(4)) data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9    from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10         ) x&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-5.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use the functions TRANSLATE and REPLACE to isolate and separate  the numeric from the character data:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2      translate(data,'0123456789','0000000000'),'0') ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3      to_number(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        translate(lower(data),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7                  'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8                   rpad('z',26,'z')),'z')) sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10 select ename||sal data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12        )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-5.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the functions TRANSLATE and REPLACE to isolate and separate  the numeric from the character data:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2      translate(data,'0123456789','0000000000'),'0','') as ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3           cast(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        replace(&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5      translate(lower(data),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6                'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7                rpad('z',26,'z')),'z','') as integer) as sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9 select ename||sal as data&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11        ) x&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-5.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The syntax is a bit different for each DBMS, but the technique  is the same. I will use the solution for Oracle in the discussion section. The  key to solving this problem is to isolate the &lt;a name="idx-CHP-6-0245"&gt;&lt;/a&gt;numeric  and character data. You can use TRANSLATE and REPLACE to do this. To extract the  numeric data, first isolate all character data using TRANSLATE:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        translate(lower(data),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                 'abcdefghijklmnopqrstuvwxyz',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                 rpad('z',26,'z')) sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select ename||sal data from emp)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DATA                 SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -------------------- -------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH800             zzzzz800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN1600            zzzzz1600&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD1250             zzzz1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES2975            zzzzz2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN1250           zzzzzz1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE2850            zzzzz2850&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK2450            zzzzz2450&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT3000            zzzzz3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING5000             zzzz5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER1500           zzzzzz1500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS1100            zzzzz1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES950             zzzzz950&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD3000             zzzz3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER1300           zzzzzz1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;By using TRANSLATE you convert every non-&lt;a name="idx-CHP-6-0246"&gt;&lt;/a&gt;numeric character into a lowercase Z. The next step is  to remove all instances of lowercase Z from each record using REPLACE, leaving  only numerical characters that can then be cast to a number:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data,&lt;br /&gt;        to_number(&lt;br /&gt;          replace(&lt;br /&gt;        translate(lower(data),&lt;br /&gt;                  'abcdefghijklmnopqrstuvwxyz',&lt;br /&gt;                  rpad('z',26,'z')),'z')) sal&lt;br /&gt;   from (select ename||sal data from emp)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DATA                        SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  -------------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH800                    800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN1600                  1600&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  WARD1250                   1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES2975                  2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MARTIN1250                 1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE2850                  2850&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK2450                  2450&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT3000                  3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING5000                   5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER1500                 1500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS1100                  1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES950                    950&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD3000                   3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER1300                 1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;To extract the non-numeric characters, isolate the numeric  characters using TRANSLATE:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select data,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        translate(data,'0123456789','0000000000') ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select ename||sal data from emp)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DATA                 ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  -------------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH800             SMITH000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN1600            ALLEN0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  WARD1250             WARD0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES2975            JONES0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MARTIN1250           MARTIN0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE2850            BLAKE0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK2450            CLARK0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT3000            SCOTT0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING5000             KING0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER1500           TURNER0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS1100            ADAMS0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES950             JAMES000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD3000             FORD0000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER1300           MILLER0000&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;By using TRANSLATE you convert every &lt;a name="idx-CHP-6-0247"&gt;&lt;/a&gt;numeric character into a zero. The next step is to  remove all instances of zero from each record using REPLACE, leaving only  non-numeric characters:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select data,&lt;br /&gt;        replace(translate(data,'0123456789','0000000000'),'0') ename&lt;br /&gt;   from (select ename||sal data from emp)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DATA                 ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  -------------------- -------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH800             SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN1600            ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  WARD1250             WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES2975            JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MARTIN1250           MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE2850            BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK2450            CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT3000            SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING5000             KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER1500           TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS1100            ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES950             JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD3000             FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER1300           MILLER&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Put the two techniques together and you have your solution.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8253500756938862096?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8253500756938862096/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8253500756938862096&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8253500756938862096'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8253500756938862096'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-separating-numeric-and.html' title='SQL Server : Separating Numeric and Character Data'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-5070700562461862554</id><published>2008-06-12T06:56:00.002-07:00</published><updated>2008-06-12T06:57:48.284-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL SERVER : Removing Unwanted Characters from a String</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to remove specific characters from your data. Consider  this result set:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME             SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH             800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN            1600&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD             1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES            2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN           1250&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE            2850&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK            2450&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT            3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING             5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER           1500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS            1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES             950&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD             3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER           1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to remove all zeros and vowels as shown by the  following values in columns STRIPPED1 and STRIPPED2:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME      STRIPPED1         SAL STRIPPED2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ---------- ---------- ---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SMITH      SMTH              800 8&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALLEN      LLN              1600 16&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; WARD       WRD              1250 125&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JONES      JNS              2975 2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MARTIN     MRTN             1250 125&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; BLAKE      BLK              2850 285&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK      CLRK             2450 245&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SCOTT      SCTT             3000 3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING       KNG              5000 5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TURNER     TRNR             1500 15&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ADAMS      DMS              1100 11&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; JAMES      JMS               950 95&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; FORD       FRD              3000 3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER     MLLR             1300 13&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-4.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Each DBMS provides functions for removing unwanted characters  from a string. The functions &lt;a name="idx-CHP-6-0239"&gt;&lt;/a&gt;REPLACE and &lt;a name="idx-CHP-6-0240"&gt;&lt;/a&gt;TRANSLATE are most useful for this problem.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-4.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions TRANSLATE and REPLACE to remove  unwanted characters and strings:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2        replace(translate(ename,'aaaaa','AEIOU'),'a','') stripped1,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4        replace(cast(sal as char(4)),'0','') stripped2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5   from emp&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-4.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;MySQL and SQL Server do not offer a TRANSLATE function, so  several calls to REPLACE are needed:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select ename,&lt;br /&gt;  2        replace(&lt;br /&gt;  3        replace(&lt;br /&gt;  4        replace(&lt;br /&gt;  5        replace(&lt;br /&gt;  6        replace(ename,'A',''),'E',''),'I',''),'O',''),'U','')&lt;br /&gt;  7        as stripped1,&lt;br /&gt;  8        sal,&lt;br /&gt;  9        replace(sal,0,'') stripped2&lt;br /&gt; 10   from emp&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-4.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the built-in functions TRANSLATE and REPLACE to remove  unwanted characters and strings:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select ename,&lt;br /&gt; 2        replace(translate(ename,'AEIOU','aaaaa'),'a')&lt;br /&gt; 3        as stripped1,&lt;br /&gt; 4        sal,&lt;br /&gt; 5        replace(sal,0,'') as stripped2&lt;br /&gt; 6   from emp&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-4.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The built-in function REPLACE removes all occurrences of zeros.  To remove the vowels, use TRANSLATE to convert all vowels into one specific  character (I used "a"; you can use any character), then use REPLACE to remove  all occurrences of that character.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-5070700562461862554?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/5070700562461862554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=5070700562461862554&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5070700562461862554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5070700562461862554'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-removing-unwanted-characters.html' title='SQL SERVER : Removing Unwanted Characters from a String'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6246088727094171982</id><published>2008-06-12T06:56:00.001-07:00</published><updated>2008-06-12T06:56:52.543-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Counting the Occurrences of a Character in a String</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to count the number of times a character or substring  occurs within a given string. Consider the following string:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;10,CLARK,MANAGER&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to determine how many commas are in the string.&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-3.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Subtract the length of the string without the commas from the  original length of the string to determine the number of commas in the string.  Each DBMS provides functions for obtaining the length of a string and removing  characters from a string. In most cases, these functions are LENGTH and REPLACE,  respectively (SQL Server users will use the built-in function LEN rather than  LENGTH):&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select (length('10,CLARK,MANAGER')-&lt;br /&gt; 2        length(replace('10,CLARK,MANAGER',',','')))/length(',')&lt;br /&gt; 3        as cnt&lt;br /&gt; 4   from t1&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-3.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;You arrive at the solution by using simple subtraction. The  call to LENGTH on line 1 returns the original size of the string, and the first  call to LENGTH on line 2 returns the size of the string without the commas,  which are removed by REPLACE.&lt;/p&gt; &lt;p class="docText"&gt;By subtracting the two lengths you obtain the difference in  terms of characters, which is the number of commas in the string. The last  operation divides the difference by the length of your search string. This  division is necessary if the string you are looking for has a length greater  than 1. In the following example, counting the occurrence of "LL" in the string  "HELLO HELLO" without dividing will return an incorrect result:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (length('HELLO HELLO')-&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        length(replace('HELLO HELLO','LL','')))/length('LL')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        as correct_cnt,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (length('HELLO HELLO')-&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        length(replace('HELLO HELLO','LL',''))) as incorrect_cnt&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CORRECT_CNT INCORRECT_CNT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------- -------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           2             4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6246088727094171982?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6246088727094171982/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6246088727094171982&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6246088727094171982'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6246088727094171982'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/counting-occurrences-of-character-in.html' title='Counting the Occurrences of a Character in a String'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-5444527022627357632</id><published>2008-06-12T06:55:00.000-07:00</published><updated>2008-06-12T06:56:19.280-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL : Embedding Quotes Within String Literals</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to embed quote marks within string literals. You would  like to produce results such as the following with SQL:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; QMARKS&lt;br /&gt; --------------&lt;br /&gt; g'day mate&lt;br /&gt; beavers' teeth&lt;br /&gt; '&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-2.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The following three SELECTs highlight different ways you can  create quotes: in the middle of a string and by themselves:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select 'g''day mate' qmarks from t1 union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2 select 'beavers'' teeth'    from t1 union all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3 select ''''                 from t1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-2.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When working with quotes, it's often useful to think of them  like parentheses. When you have an opening parenthesis, you must always have a  closing parenthesis. The same goes for quotes. Keep in mind that you should  always have an even number of quotes across any given string. To embed a single  quote within a string you need to use two quotes:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select 'apples core', 'apple''s core',&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         case when '' is null then 0 else 1 end&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from t1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  'APPLESCORE 'APPLE''SCOR CASEWHEN''ISNULLTHEN0ELSE1END&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ----------- ------------ -----------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  apples core apple's core                             0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Following is the solution stripped down to its bare elements.  You have two outer quotes defining a string literal, and, within that string  literal you have two quotes that together represent just one quote in the string  that you actually get:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; &lt;b&gt;&lt;br /&gt; select '''' as quote from t1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt; Q&lt;br /&gt; -&lt;br /&gt; '&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;When working with quotes, be sure to remember that a string  literal comprising two quotes alone, with no intervening characters, is  NULL.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-5444527022627357632?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/5444527022627357632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=5444527022627357632&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5444527022627357632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5444527022627357632'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-embedding-quotes-within-string.html' title='SQL : Embedding Quotes Within String Literals'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-7996517127104639802</id><published>2008-06-12T06:53:00.000-07:00</published><updated>2008-06-12T06:55:30.760-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Walking a String - SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to traverse a string to return each character as a  row, but SQL lacks a loop operation. For example, you want to display the ENAME  "KING" from table EMP as four rows, where each row contains just characters from  "KING".&lt;/p&gt;&lt;a name="sqlckbk-CHP-6-SECT-1.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a Cartesian product to generate the number of rows needed  to return each character of a string on its own line. Then use your DBMS's  built-in string parsing function to extract the characters you are interested in  (SQL Server users will use SUBSTRING instead of SUBSTR):&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select substr(e.ename,iter.pos,1) as C&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from (select ename from emp where ename = 'KING') e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        (select id as pos from t10) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4  where iter.pos &lt;= length(e.ename)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; C&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; K&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; I&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; N&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; G&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-6-SECT-1.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The key to iterating through a string's characters is to join  against a table that has enough rows to produce the required number of  iterations. This example uses table T10, which contains 10 rows (it has one  column, ID, holding the values 1 through 10). The maximum number of rows that  can be returned from this query is 10.&lt;/p&gt; &lt;p class="docText"&gt;The following example shows the Cartesian product between E and  ITER (i.e., between the specific name and the 10 rows from T10) without parsing  ENAME:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select ename, iter.pos&lt;br /&gt;   from (select ename from emp where ename = 'KING') e,&lt;br /&gt;        (select id as pos from t10) iter&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME             POS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                4&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                6&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                7&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                8&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                9&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING               10&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The cardinality of inline view E is 1, and the cardinality of  inline view ITER is 10. The Cartesian product is then 10 rows. Generating such a  product is the first step in mimicking a loop in SQL.&lt;/p&gt;&lt;p class="docText"&gt;The solution uses a WHERE clause to break out of the loop after  four rows have been returned. To restrict the result set to the same number of  rows as there are characters in the name, that WHERE clause specifies ITER.POS  &lt;= LENGTH(E. ENAME) as the condition:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename, iter.pos&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (select ename from emp where ename = 'KING') e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (select id as pos from t10) iter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where iter.pos &lt;= length(e.ename)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME             POS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING                4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Now that you have one row for each character in E.ENAME, you  can use ITER.POS as a parameter to SUBSTR, allowing you to navigate through the  characters in the string. ITER.POS increments with each row, and thus each row  can be made to return a successive character from E.ENAME. This is how the  solution example works.&lt;/p&gt; &lt;p class="docText"&gt;Depending on what you are trying to accomplish you may or may  not need to generate a row for every single character in a string. The following  query is an example of walking E.ENAME and exposing different portions (more  than a single character) of the string:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; select substr(e.ename,iter.pos) a,&lt;br /&gt;        substr(e.ename,length(e.ename)-iter.pos+1) b&lt;br /&gt;   from (select ename from emp where ename = 'KING') e,&lt;br /&gt;        (select id pos from t10) iter&lt;br /&gt;  where iter.pos &lt;= length(e.ename)&lt;br /&gt;&lt;br /&gt; A          B&lt;br /&gt; ---------- ------&lt;br /&gt; KING       G&lt;br /&gt; ING        NG&lt;br /&gt; NG         ING&lt;br /&gt; G          KING&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The most common scenarios for the recipes in this chapter  involve walking the whole string to generate a row for each character in the  string, or walking the string such that the number of rows generated reflects  the number of particular characters or delimiters that are present in the  string.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-7996517127104639802?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/7996517127104639802/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=7996517127104639802&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7996517127104639802'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7996517127104639802'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/walking-string-sql.html' title='Walking a String - SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6445534746417255024</id><published>2008-06-11T07:45:00.000-07:00</published><updated>2008-06-11T07:46:28.575-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Describing the Data Dictionary Views in an Oracle Database</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You are using Oracle. You can't remember what &lt;a name="idx-CHP-5-0234"&gt;&lt;/a&gt;data dictionary views are available to you, nor can you  remember their column definitions. Worse yet, you do not have convenient access  to vendor documentation.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-7.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;This is an Oracle-specific recipe. Oracle not only maintains a  robust set of data dictionary views, but there are even data dictionary views to  document the data dictionary views. It's all so wonderfully circular.&lt;/p&gt; &lt;p class="docText"&gt;Query the view named DICTIONARY to list data dictionary views  and their purposes:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select table_name, comments&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dictionary&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   order by table_name;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; TABLE_NAME                     COMMENTS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------ --------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALL_ALL_TABLES                 Description of all object and relational&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                                tables accessible to the user&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ALL_APPLY                      Details about each apply process that&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                                dequeues from the queue visible to the&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                                current user&lt;/span&gt;&lt;br /&gt; …&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Query DICT_COLUMNS to describe the columns in given a data  dictionary view:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; select column_name, comments&lt;br /&gt;      from dict_columns&lt;br /&gt;  where table_name = 'ALL_TAB_COLUMNS';&lt;br /&gt;&lt;br /&gt; COLUMN_NAME                     COMMENTS&lt;br /&gt; ------------------------------- --------------------------------------------&lt;br /&gt; OWNER&lt;br /&gt; TABLE_NAME                      Table, view or cluster name&lt;br /&gt; COLUMN_NAME                     Column name&lt;br /&gt; DATA_TYPE                       Datatype of the column&lt;br /&gt; DATA_TYPE_MOD                   Datatype modifier of the column&lt;br /&gt; DATA_TYPE_OWNER                 Owner of the datatype of the column&lt;br /&gt; DATA_LENGTH                     Length of the column in bytes&lt;br /&gt; DATA_PRECISION                  Length: decimal digits (NUMBER) or binary&lt;br /&gt;                                 digits (FLOAT)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-7.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Back in the day when Oracle's documentation set wasn't so  freely available on the Web, it was incredibly convenient that Oracle made the  DICTIONARY and DICT_ COLUMNS views available. Knowing just those two views, you  could bootstrap to learning about all the other views, and from thence to  learning about your entire database.&lt;/p&gt; &lt;p class="docText"&gt;Even today, it's convenient to know about DICTIONARY and  DICT_COLUMNS. Often, if you aren't quite certain which view describes a given  object type, you can issue a wildcard query to find out. For example, to get a  handle on what views might describe tables in your schema:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select table_name, comments&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dictionary&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where table_name LIKE '%TABLE%'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  order by table_name;&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This query returns all data dictionary view names that include  the term "TABLE". This approach takes advantage of Oracle's fairly consistent  data dictionary view naming conventions. Views describing tables are all likely  to contain "TABLE" in their name. (Sometimes, as in the case of ALL_TAB_COLUMNS,  TABLE is abbreviated TAB.)&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6445534746417255024?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6445534746417255024/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6445534746417255024&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6445534746417255024'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6445534746417255024'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/describing-data-dictionary-views-in.html' title='Describing the Data Dictionary Views in an Oracle Database'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2073225167325549013</id><published>2008-06-11T07:44:00.000-07:00</published><updated>2008-06-11T07:45:39.887-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Using SQL to Generate SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to create &lt;a name="idx-CHP-5-0231"&gt;&lt;/a&gt;dynamic SQL  statements, perhaps to automate maintenance tasks. You want to accomplish three  tasks in particular: count the number of rows in your tables, disable foreign  key constraints defined on your tables, and generate insert &lt;a name="idx-CHP-5-0232"&gt;&lt;/a&gt;scripts from the data in your tables.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-6.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The concept is to use strings to build SQL statements, and the  values that need to be filled in (such as the object name the command acts upon)  will be supplied by data from the tables you are selecting from. Keep in mind,  the queries only generate the statements; you must then run these statements via  script, manually, or however you execute your SQL statements. The examples below  are queries that would work on an Oracle system. For other RDBMSs the technique  is exactly the same, the only difference being things like the names of the data  dictionary tables and date formatting. The output shown from the queries below  are a portion of the rows returned from an instance of Oracle on my laptop. Your  result sets will of course vary.&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; /* generate SQL to count all the rows in all your tables */&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select 'select count(*) from '||table_name||';' cnts&lt;br /&gt;   from user_tables;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CNTS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from ANT;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from BONUS;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from DEMO1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from DEMO2;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from DEPT;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from DUMMY;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from EMP;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from EMP_SALES;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from EMP_SCORE;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from PROFESSOR;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from T;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from T1;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from T2;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from T3;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from TEACH;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from TEST;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from TRX_LOG;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select count(*) from X;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; /* disable foreign keys from all tables */&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select 'alter table '||table_name||&lt;br /&gt;        ' disable constraint '||constraint_name||';' cons&lt;br /&gt;   from user_constraints&lt;br /&gt;  where constraint_type = 'R';&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CONS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table ANT disable constraint ANT_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table BONUS disable constraint BONUS_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table DEMO1 disable constraint DEMO1_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table DEMO2 disable constraint DEMO2_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table DEPT disable constraint DEPT_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table DUMMY disable constraint DUMMY_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table EMP disable constraint EMP_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table EMP_SALES disable constraint EMP_SALES_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table EMP_SCORE disable constraint EMP_SCORE_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; alter table PROFESSOR disable constraint PROFESSOR_FK;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; /* generate an insert script from some columns in table EMP */&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select 'insert into emp(empno,ename,hiredate) '||chr(10)||&lt;br /&gt;        'values( '||empno||','||''''||ename&lt;br /&gt;        ||''',to_date('||''''||hiredate||''') );' inserts&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 10;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; INSERTS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------------------------------------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into emp(empno,ename,hiredate)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values( 7782,'CLARK',to_date('09-JUN-1981 00:00:00') );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into emp(empno,ename,hiredate)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values( 7839,'KING',to_date('17-NOV-1981 00:00:00') );&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into emp(empno,ename,hiredate)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values( 7934,'MILLER',to_date('23-JAN-1982 00:00:00') );&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-6.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Using SQL to generate SQL is particularly useful for creating  portable scripts such as you might use when testing on multiple environments.  Additionally, as can be seen by the examples above, using SQL to generate SQL is  useful for performing batch maintenance, and for easily finding out information  about multiple objects in one go. Generating SQL with SQL is an extremely simple  operation, and the more you experiment with it the easier it will become. The  examples provided should give you a nice base on how to build your own "dynamic"  SQL scripts because, quite frankly, there's not much to it. Work on it and  you'll get it.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2073225167325549013?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2073225167325549013/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2073225167325549013&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2073225167325549013'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2073225167325549013'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/using-sql-to-generate-sql.html' title='Using SQL to Generate SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8287435645270887224</id><published>2008-06-11T07:42:00.001-07:00</published><updated>2008-06-11T07:44:50.976-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL : Listing Foreign Keys Without Corresponding Indexes</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to list tables that have foreign key columns that are  not indexed. For example, you want to determine if the &lt;a name="idx-CHP-5-0224"&gt;&lt;/a&gt;foreign keys on table EMP are indexed.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Query SYSCAT.TABCONST, SYSCAT.KEYCOLUSE, SYSCAT.INDEXES, and  SYSCAT.INDEXCOLUSE:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select fkeys.tabname,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         fkeys.constname,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         fkeys.colname,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         ind_cols.indname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6  select a.tabschema, a.tabname, a.constname, b.colname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7    from syscat.tabconst a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         syscat.keycoluse b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9  where a.tabname    = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    and a.tabschema  = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11    and a.type       = 'F'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12    and a.tabname    = b.tabname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13    and a.tabschema  = b.tabschema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14        ) fkeys&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15        left join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16        (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  select a.tabschema,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18         a.tabname,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19         a.indname,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20         b.colname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21    from syscat.indexes a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22         syscat.indexcoluse b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 23  where a.indschema  = b.indschema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 24    and a.indname    = b.indname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 25        ) ind_cols&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 26     on (     &lt;/span&gt;&lt;a style="color: rgb(204, 0, 0);" name="idx-CHP-5-0225"&gt;&lt;/a&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;fkeys.tabschema = ind_cols.tabschema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 27          and fkeys.tabname   = ind_cols.tabname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 28          and fkeys.colname   = ind_cols.colname )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 29  where ind_cols.indname is null&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.ALL_CONS_COLUMNS, SYS.ALL_CONSTRAINTS, and SYS.ALL_  IND_COLUMNS:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select a.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         a.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         a.column_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         c.index_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from all_cons_columns a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         all_constraints b,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7         all_ind_columns c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8  where a.table_name      = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9    and a.owner           = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    and b.constraint_type = 'R'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11    and a.owner           = b.owner&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12    and a.table_name      = b.table_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13    and a.constraint_name = b.constraint_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14    and a.owner           = c.table_owner (+)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15    and a.table_name      = c.table_name (+)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16    and a.column_name     = c.column_name (+)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17    and c.index_name      is null&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Query INFORMATION_SCHEMA.KEY_COLUMN_USAGE, INFORMATION_  SCHEMA.REFERENTIAL_CONSTRAINTS, INFORMATION_SCHEMA.COL-UMNS, and  PG_CATALOG.PG_INDEXES:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select fkeys.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         fkeys.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         fkeys.column_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         ind_cols.indexname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6  select a.constraint_schema,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7         a.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         a.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9         a.column_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    from information_schema.key_column_usage a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11         information_schema.referential_constraints b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12   where a.constraint_name   = b.constraint_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13     and a.constraint_schema = b.constraint_schema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14     and a.constraint_schema = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15     and a.table_name        = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16         ) fkeys&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17         left join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18         (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19  select a.schemaname, a.tablename, a.indexname, b.column_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20    from pg_catalog.pg_indexes a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21         information_schema.columns b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22   where a.tablename  = b.table_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 23     and a.schemaname = b.table_schema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 24         ) ind_cols&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 25      on (  fkeys.constraint_schema = ind_cols.schemaname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 26           and fkeys.table_name     = ind_cols.tablename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 27           and fkeys.column_name    = ind_cols.column_name )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 28   where ind_cols.indexname is null&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;You can use the SHOW INDEX command to retrieve index  information such as index name, columns in the index, and ordinal position of  the columns in the index. Additionally, you can query  INFORMATION_SCHEMA.KEY_COLUMN_USAGE to list the &lt;a name="idx-CHP-5-0226"&gt;&lt;/a&gt;foreign keys for a given table. In MySQL 5, &lt;a name="idx-CHP-5-0227"&gt;&lt;/a&gt;foreign keys are said to be indexed automatically, but  can in fact be dropped. To determine whether a foreign key column's index has  been dropped you can execute SHOW INDEX for a particular table and compare the  output with that of INFORMATION_SCHEMA.KEY_ COLUMN_USAGE.COLUMN_NAME for the  same table. If the COLUMN_NAME is listed in KEY_COLUMN_USAGE but is not returned  by SHOW INDEX, you know that column is not indexed.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.TABLES, SYS.FOREIGN_KEYS, SYS.COLUMNS, SYS.INDEXES,  and SYS.INDEX_COLUMNS:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select fkeys.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         fkeys.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         fkeys.column_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         ind_cols.index_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6  select a.object_id,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7         d.column_id,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8         a.name table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9         b.name constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10         d.name column_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11    from sys.tables a&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12         join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13         sys.foreign_keys b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14      on ( a.name          = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15           and a.object_id = b.parent_object_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16         )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17         join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 18         sys.&lt;/span&gt;&lt;a style="color: rgb(204, 0, 0);" name="idx-CHP-5-0228"&gt;&lt;/a&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;foreign_key_columns c&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 19     on (  b.object_id = c.constraint_object_id )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 20        join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 21        sys.columns d&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 22     on (    c.constraint_column_id = d.column_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 23         and a.object_id            = d.object_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 24        )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 25        ) fkeys&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 26        left join&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 27        (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 28 select a.name index_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 29        b.object_id,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 30        b.column_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 31   from sys.indexes a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 32        sys.index_columns b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 33  where a.index_id = b.index_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 34        ) ind_cols&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 35     on (     fkeys.object_id = ind_cols.object_id&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 36          and fkeys.column_id = ind_cols.column_id )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 37  where ind_cols.index_name is null&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-5.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Each vendor uses its own locking mechanism when modifying rows.  In cases where there is a parent-child relationship enforced via &lt;a name="idx-CHP-5-0229"&gt;&lt;/a&gt;foreign key, having indexes on the child column(s) can  reducing locking (see your specific RDBMS documentation for details). In other  cases, it is common that a child table is joined to a parent table on the  foreign key column, so an index may help improve performance in that scenario as  well.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8287435645270887224?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8287435645270887224/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8287435645270887224&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8287435645270887224'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8287435645270887224'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-listing-foreign-keys-without.html' title='SQL : Listing Foreign Keys Without Corresponding Indexes'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-7203282140458970825</id><published>2008-06-09T22:08:00.000-07:00</published><updated>2008-06-09T22:09:46.535-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL : Listing Constraints on a Table</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to list the constraints defined for a table in some  schema and the columns they are defined on. For example, you want to find the  constraints and the columns they are on for table EMP.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-4.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt;&lt;a name="sqlckbk-CHP-5-SECT-4.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Query SYSCAT.TABCONST and SYSCAT.COLUMNS:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1  select a.tabname, a.constname, b.colname, a.type&lt;br /&gt; 2    from syscat.tabconst a,&lt;br /&gt; 3         syscat.columns b&lt;br /&gt; 4  where a.tabname   = 'EMP'&lt;br /&gt; 5    and a.tabschema = 'SMEAGOL'&lt;br /&gt; 6    and a.tabname   = b.tabname&lt;br /&gt; 7    and a.tabschema = b.tabschema&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-4.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.ALL_CONSTRAINTS and SYS.ALL_CONS_COLUMNS:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select a.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         a.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         b.column_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         a.constraint_type&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from all_constraints a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         all_cons_columns b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7  where a.table_name      = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8    and a.owner           = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9    and a.table_name      = b.table_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    and a.owner           = b.owner&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11    and a.constraint_name = b.constraint_name&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-4.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL, MySQL, and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Query INFORMATION_SCHEMA.TABLE_CONSTRAINTS and INFORMATION_  SCHEMA.KEY_COLUMN_USAGE:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1  select a.table_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2         a.constraint_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3         b.column_name,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4         a.constraint_type&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5    from information_schema.table_constraints a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6         information_schema.key_column_usage b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7  where a.table_name      = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8    and a.table_schema    = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9    and a.table_name      = b.table_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10    and a.table_schema    = b.table_schema&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11    and a.constraint_name = b.constraint_name&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-4.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Constraints are such a critical part of relational databases  that it should go without saying why you need to know what constraints are on  your tables. Listing the constraints on tables is useful for a variety of  reasons: you may want to find tables missing a primary key, you may want to find  which columns should be &lt;a name="idx-CHP-5-0222"&gt;&lt;/a&gt;foreign keys but are not  (i.e., child tables have data different from the parent tables and you want to  know how that happened), or you may want to know about check constraints (Are  columns nullable? Do they have to satisfy a specific condition? etc.).&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-7203282140458970825?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/7203282140458970825/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=7203282140458970825&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7203282140458970825'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/7203282140458970825'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-listing-constraints-on-table.html' title='SQL : Listing Constraints on a Table'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3103834498326202112</id><published>2008-06-09T22:07:00.000-07:00</published><updated>2008-06-09T22:08:18.090-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server : Listing Indexed Columns for a Table</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want list indexes, their columns, and the column position  (if available) in the index for a given table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The vendor-specific solutions that follow all assume that you  are listing indexes for the table EMP in the SMEAGOL schema.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Query SYSCAT.INDEXES:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1  select a.tabname, b.indname, b.colname, b.colseq&lt;br /&gt; 2    from syscat.indexes a,&lt;br /&gt; 3         syscat.indexcoluse b&lt;br /&gt; 3   where a.tabname   = 'EMP'&lt;br /&gt; 4     and a.tabschema = 'SMEAGOL'&lt;br /&gt; 5     and a.indschema = b.indschema&lt;br /&gt; 6     and a.indname   = b.indname&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.ALL_IND_COLUMNS:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; select table_name, index_name, column_name, column_position&lt;br /&gt;   from sys.all_ind_columns&lt;br /&gt;  where table_name  = 'EMP'&lt;br /&gt;    and table_owner = 'SMEAGOL'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Query PG_CATALOG.PG_INDEXES and INFORMATION_SCHEMA.COLUMNS:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1  select a.tablename,a.indexname,b.column_name&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2    from pg_catalog.pg_indexes a,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3         information_schema.columns b&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   where a.schemaname = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5     and a.tablename  = b.table_name&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the SHOW INDEX command:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;show index from emp&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.2.5"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.TABLES, SYS.INDEXES, SYS.INDEX_COLUMNS, and  SYS.COLUMNS:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1  select a.name table_name,&lt;br /&gt;  2         b.name index_name,&lt;br /&gt;  3         d.name column_name,&lt;br /&gt;  4         c.index_column_id&lt;br /&gt;  5    from sys.tables a,&lt;br /&gt;  6         sys.indexes b,&lt;br /&gt;  7         sys.index_columns c,&lt;br /&gt;  8         sys.columns d&lt;br /&gt;  9  where a.object_id = b.object_id&lt;br /&gt; 10    and b.object_id = c.object_id&lt;br /&gt; 11    and b.index_id  = c.index_id&lt;br /&gt; 12    and c.object_id = d.object_id&lt;br /&gt; 13    and c.column_id = d.column_id&lt;br /&gt; 14    and a.name      = 'EMP'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-3.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When it comes to queries, it's important to know what columns  are/aren't indexed. Indexes can provide good performance for queries against  columns that are frequently used in filters and that are fairly selective.  Indexes are also useful when joining between tables. By knowing what columns are  indexed, you are already one step ahead of performance problems if they should  occur. Additionally, you might want to find information about the indexes  themselves: how many levels deep they are, how many distinct keys, how many leaf  blocks, and so forth. Such information is also available from the views/tables  queried in this recipe's solutions.&lt;/p&gt;&lt;a href="23961536.html"&gt;&lt;img alt="" src="images/pixel.jpg" border="0" height="1" width="1" /&gt;&lt;/a&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3103834498326202112?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3103834498326202112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3103834498326202112&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3103834498326202112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3103834498326202112'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-listing-indexed-columns-for.html' title='SQL Server : Listing Indexed Columns for a Table'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2026830444084118636</id><published>2008-06-09T22:06:00.000-07:00</published><updated>2008-06-09T22:07:06.711-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL ; Listing a Table's Columns</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to list the &lt;a name="idx-CHP-5-0217"&gt;&lt;/a&gt;columns in a  table, along with their data types, and their position in the table they are  in.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-2.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The following solutions assume that you wish to list columns,  their data types, and their numeric position in the table named EMP in the  schema SMEAGOL.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-2.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Query SYSCAT.COLUMNS:&lt;/p&gt;&lt;pre&gt; &lt;a name="idx-CHP-5-0218"&gt;&lt;/a&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select colname, typename, colno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from syscat.columns&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where tabname   = 'EMP'&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4    and tabschema = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-2.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Query ALL_TAB_COLUMNS:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select column_name, data_type, column_id&lt;br /&gt; 2   from all_tab_columns&lt;br /&gt; 3  where owner      = 'SMEAGOL'&lt;br /&gt; 4    and table_name = 'EMP'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-2.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL, MySQL, and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Query INFORMATION_SCHEMA.COLUMNS:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select column_name, data_type, ordinal_position&lt;br /&gt; 2   from information_schema.columns&lt;br /&gt; 3  where table_schema = 'SMEAGOL'&lt;br /&gt; 4    and table_name   = 'EMP'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-2.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Each vendor provides ways for you to get detailed information  about your column data. In the examples above only the column name, data type,  and position are returned. Additional useful items of information include  length, nullability, and default values.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2026830444084118636?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2026830444084118636/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2026830444084118636&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2026830444084118636'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2026830444084118636'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-listing-tables-columns.html' title='SQL ; Listing a Table&apos;s Columns'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6640380480901073554</id><published>2008-06-09T22:00:00.000-07:00</published><updated>2008-06-09T22:03:13.444-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Listing Tables in a Schema - SQL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to see a list all the tables you've created in a given  schema.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-1.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The solutions that follow all assume you are working with the  SMEAGOL schema. The basic approach to a solution is the same for all RDBMSs: you  query a system table (or view) containing a row for each table in the  database.&lt;/p&gt;&lt;a name="sqlckbk-CHP-5-SECT-1.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Query SYSCAT.TABLES:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select tabname&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from syscat.tables&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where tabschema = 'SMEAGOL'&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-5-SECT-1.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Query SYS.ALL_TABLES:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; select table_name&lt;br /&gt;   from all_tables&lt;br /&gt;  where owner = 'SMEAGOL'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-1.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL, MySQL, and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Query &lt;a name="idx-CHP-5-0216"&gt;&lt;/a&gt;INFORMATION_SCHEMA.TABLES:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select table_name&lt;br /&gt; 2   from information_schema.tables&lt;br /&gt; 3  where table_schema = 'SMEAGOL'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-5-SECT-1.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;In a delightfully circular manner, databases expose information  about themselves through the very mechanisms that you create for your own  applications: tables and views. Oracle, for example, maintains an extensive  catalog of system views, such as ALL_TABLES, that you can query for information  about tables, indexes, grants, and any other database object.&lt;/p&gt;&lt;span style="color: rgb(51, 102, 255);"&gt;TIPS : &lt;/span&gt;&lt;br /&gt;&lt;p style="color: rgb(51, 102, 255);" class="docText"&gt;Oracle's catalog views are just that, views. They are based on  an underlying set of tables that contain the information in a very  user-unfriendly form. The views put a very usable face on Oracle's catalog  data.&lt;/p&gt;&lt;p class="docText"&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6640380480901073554?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6640380480901073554/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6640380480901073554&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6640380480901073554'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6640380480901073554'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/listing-tables-in-schema-sql.html' title='Listing Tables in a Schema - SQL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2035034989930274615</id><published>2008-06-09T06:19:00.001-07:00</published><updated>2008-06-09T06:19:52.586-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Deleting Records Referenced from Another Table - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to delete records from one table when those records  are referenced from some other table. Consider the following table, named  DEPT_ACCIDENTS, which contains one row for each accident that occurs in a  manufacturing business. Each row records the department in which an accident  occurred and also the type of accident.&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create table dept_accidents&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ( deptno         integer,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   accident_name  varchar(20) )&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (10,'BROKEN FOOT')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (10,'FLESH WOUND')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (20,'FIRE')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (20,'FIRE')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (20,'FLOOD')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept_accidents values (30,'BRUISED GLUTE')&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select * from dept_accidents&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO ACCIDENT_NAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- --------------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 BROKEN FOOT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 FLESH WOUND&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 FIRE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 FIRE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 FLOOD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 BRUISED GLUTE&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to delete from EMP the records for those employees  working at a department that has three or more accidents.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-17.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a subquery and the aggregate function COUNT to find the  departments with three or more accidents. Then delete all employees working in  those departments:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 delete from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2  where deptno in ( select deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3                      from dept_accidents&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4                     group by deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5                    having count(*) &gt;= 3 )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-17.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The subquery will identify which departments have three or more  accidents:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dept_accidents&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; having count(*) &gt;= 3&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The DELETE will then delete any employees in the departments  returned by the subquery (in this case, only in department 20).&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2035034989930274615?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2035034989930274615/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2035034989930274615&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2035034989930274615'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2035034989930274615'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/deleting-records-referenced-from.html' title='Deleting Records Referenced from Another Table - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8329396152184590794</id><published>2008-06-09T06:18:00.000-07:00</published><updated>2008-06-09T06:19:06.712-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Deleting Duplicate Records : SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to delete duplicate records from a table. Consider the  following table:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; create table dupes (id integer, name varchar(10))&lt;/b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (1, 'NAPOLEON')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (2, 'DYNAMITE')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (3, 'DYNAMITE')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (4, 'SHE SELLS')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (5, 'SEA SHELLS')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (6, 'SEA SHELLS')&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dupes values (7, 'SEA SHELLS')&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select * from dupes order by 1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         ID NAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          1 NAPOLEON&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          2 DYNAMITE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          3 DYNAMITE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          4 SHE SELLS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          5 SEA SHELLS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          6 SEA SHELLS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;          7 SEA SHELLS&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;For each group of duplicate names, such as "SEA SHELLS", you  wish to arbitrarily retain one ID and delete the rest. In the case of "SEA  SHELLS" you don't care whether you delete 5 and 6, or 5 and 7, or 6 and 7, but  in the end you want just one record for "SEA SHELLS".&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-16.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a subquery with an aggregate function such as MIN to  arbitrarily choose the ID to retain (in this case only the NAME with the  smallest value for ID is not deleted):&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1  delete from dupes&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   where id not in ( select min(id)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3                        from dupes&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4                       group by name )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-16.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The first thing to do when deleting duplicates is to define  exactly what it means for two rows to be considered "duplicates" of each other.  For my example in this recipe, the definition of "duplicate" is that two records  contain the same value in their NAME column. Having that definition in place,  you can look to some other column to discriminate among each set of duplicates,  to identify those records to retain. It's best if this discriminating column (or  columns) is a primary key. I used the ID column, which is a good choice because  no two records have the same ID.&lt;/p&gt; &lt;p class="docText"&gt;The key to the solution is that you group by the values that  are duplicated (by NAME in this case), and then use an aggregate function to  pick off just one key value to retain. The subquery in the "Solution" example  will return the smallest ID for each NAME, which represents the row you will not  delete:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select min(id)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dupes&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by name&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     MIN(ID)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;           4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The DELETE then deletes any ID in the table that is not  returned by the subquery (in this case IDs 3, 6, and 7). If you are having  trouble seeing how this works, run the subquery first and include the NAME in  the SELECT list:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select name, min(id)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dupes&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by name&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; NAME          MIN(ID)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DYNAMITE            2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; NAPOLEON            1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SEA SHELLS          5&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; SHE SELLS           4&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The rows returned by the subquery represent those to be  retained. The NOT IN predicate in the DELETE statement causes all other rows to  be deleted.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8329396152184590794?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8329396152184590794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8329396152184590794&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8329396152184590794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8329396152184590794'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/deleting-duplicate-records-sql-server.html' title='Deleting Duplicate Records : SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3988993084276480983</id><published>2008-06-09T06:17:00.000-07:00</published><updated>2008-06-09T06:18:11.408-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Deleting Referential Integrity Violations - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You wish to delete records from a table when those records  refer to nonexistent records in some other table. Example: some employees are  assigned to departments &lt;a name="idx-CHP-4-0210"&gt;&lt;/a&gt;that do not exist. You wish  to delete those employees.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-15.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the &lt;a name="idx-CHP-4-0211"&gt;&lt;/a&gt;NOT EXISTS predicate with a  subquery to test the validity of department numbers:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; delete from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where not exists (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    select * from dept&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     where dept.deptno = emp.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Alternatively, you can write the query using a NOT IN  predicate:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; delete from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; where deptno not in (select deptno from dept)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-15.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Deleting is really all about selecting: the real work lies in  writing WHERE clause conditions to correctly describe those records that you  wish to delete.&lt;/p&gt; &lt;p class="docText"&gt;The NOT EXISTS solution uses a correlated subquery to test for  the existence of a record in DEPT having a DEPTNO matching that in a given EMP  record. If such a record exists, then the EMP record is retained. Otherwise, it  is deleted. Each EMP record is checked in this manner.&lt;/p&gt; &lt;p class="docText"&gt;The IN solution uses a subquery to retrieve a list of valid  department numbers. DEPTNOs from each EMP record are then checked against that  list. When an EMP record is found with a DEPTNO not in the list, the EMP record  is deleted.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3988993084276480983?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3988993084276480983/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3988993084276480983&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3988993084276480983'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3988993084276480983'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/deleting-referential-integrity.html' title='Deleting Referential Integrity Violations - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-8546509711130778838</id><published>2008-06-09T06:15:00.000-07:00</published><updated>2008-06-09T06:17:32.114-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Deleting a Single Record : SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You wish to delete a single record from a table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-14.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;This is a special case of "Deleting Specific Records." The key  is to ensure that your selection criterion is narrow enough to specify only the  one record that you wish to delete. Often you will want to delete based on the  primary key. For example, to delete employee CLARK (EMPNO 7782):&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;delete from emp where empno = 7782&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-14.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Deleting is always about identifying the rows to be deleted,  and the impact of a DELETE always comes down to its WHERE clause. Omit the WHERE  clause and the scope of a DELETE is the entire table. By writing conditions in  the WHERE clause, you can narrow the scope to a group of records, or to a single  record. When deleting a single record, you should typically be identifying that  record based on its primary key or on one of its unique keys.&lt;br /&gt;&lt;/p&gt;&lt;p style="color: rgb(102, 51, 255);" class="docText"&gt;TIPS :&lt;br /&gt;&lt;/p&gt;&lt;p style="color: rgb(102, 51, 255);" class="docText"&gt;If your deletion criterion is based on a primary or unique key,  then you can be sure of deleting only one record. (This is because your RDBMS  will not allow two rows to contain the same primary or unique key values.)  Otherwise, you may want to check first, to be sure you aren't about to  inadvertently delete more records than you intend.&lt;/p&gt;&lt;p class="docText"&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-8546509711130778838?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/8546509711130778838/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=8546509711130778838&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8546509711130778838'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/8546509711130778838'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/deleting-single-record-sql-server.html' title='Deleting a Single Record : SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6947303473343758985</id><published>2008-06-09T06:14:00.002-07:00</published><updated>2008-06-09T06:15:20.173-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL : Deleting Specific Records</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You wish to delete records meeting a specific criterion from a  table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-13.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the &lt;a name="idx-CHP-4-0205"&gt;&lt;/a&gt;DELETE command with a WHERE  clause specifying which rows to delete. For example, to delete all employees in  department 10:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;delete from emp where deptno = 10&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-13.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;By using a WHERE clause with the DELETE command, you can delete  a subset of rows in a table rather than all the rows.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6947303473343758985?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6947303473343758985/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6947303473343758985&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6947303473343758985'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6947303473343758985'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-deleting-specific-records.html' title='SQL : Deleting Specific Records'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-9211834232556220893</id><published>2008-06-09T06:14:00.001-07:00</published><updated>2008-06-09T06:14:35.708-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL  Server : Deleting All Records from a Table</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to delete all the records from a table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-12.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the &lt;a name="idx-CHP-4-0203"&gt;&lt;/a&gt;DELETE command to delete  records from a table. For example, to delete all records from EMP:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; delete from emp&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-12.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When using the DELETE command without a WHERE clause, you will  delete all rows from the table specified.&lt;/p&gt;&lt;a href="23961536.html"&gt;&lt;img alt="" src="images/pixel.jpg" border="0" height="1" width="1" /&gt;&lt;/a&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-9211834232556220893?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/9211834232556220893/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=9211834232556220893&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/9211834232556220893'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/9211834232556220893'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-deleting-all-records-from.html' title='SQL  Server : Deleting All Records from a Table'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-4971568028889178288</id><published>2008-06-09T06:12:00.000-07:00</published><updated>2008-06-09T06:14:04.605-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server : Merging Records</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to conditionally insert, update, or delete records in  a table depending on whether or not corresponding records exist. (If a record  exists, then update; if not,then insert; if after updating a row fails to meet a  certain condition, delete it.) For example, you want to modify table  EMP_COMMISSION such that:&lt;/p&gt; &lt;ul&gt;&lt;li&gt; &lt;p class="docList"&gt;If any employee in EMP_COMMISSION also exists in table EMP,  then update their commission (COMM) to 1000.&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p class="docList"&gt;For all employees who will potentially have their COMM updated  to 1000, if their SAL is less than 2000, delete them (they should not be exist  in EMP_COMMISSION).&lt;/p&gt; &lt;/li&gt;&lt;li&gt; &lt;p class="docList"&gt;Otherwise, insert the EMPNO, ENAME, and DEPTNO values from  table EMP into table EMP_COMMISSION.&lt;/p&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p class="docText"&gt;Essentially, you wish to execute either an UPDATE or an INSERT  depending on whether a given row from EMP has a match in EMP_COMMISSION. Then  you wish to execute a DELETE if the result of an UPDATE causes a commission  that's too high.&lt;/p&gt; &lt;p class="docText"&gt;The following rows are currently in tables EMP and  EMP_COMMISSION, respectively:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno,empno,ename,comm&lt;br /&gt;   from emp&lt;br /&gt;  order by 1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO      EMPNO ENAME        COMM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ---------- ------ ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10       7782  CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10       7839  KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10       7934  MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20       7369  SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20       7876  ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20       7902  FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20       7788  SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20       7566  JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7499  ALLEN        300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7698  BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7654  MARTIN      1400&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7900  JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7844  TURNER         0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30       7521  WARD         500&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno,empno,ename,comm&lt;br /&gt;   from emp_commission&lt;br /&gt;  order by 1&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO      EMPNO ENAME            COMM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ---------- ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10       7782 CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10       7839 KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10       7934 MILLER&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-11.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Oracle is currently the only RDBMS with a statement designed to  solve this problem. That statement is the &lt;a name="idx-CHP-4-0200"&gt;&lt;/a&gt;MERGE  statement, and it can perform either an UPDATE or an INSERT, as needed. For  example:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1  merge into emp_commission ec&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2  using (select * from emp) emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3     on (ec.empno=emp.empno)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   when matched then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5        update set ec.comm = 1000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6        delete where (sal &lt;&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7   when not matched then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8        insert (ec.empno,ec.ename,ec.deptno,ec.comm)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 9        values (emp.empno,emp.ename,emp.deptno,emp.comm)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-11.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The join on line 3 of the solution determines what rows already  exist and will be updated. The join is between EMP_COMMISSION (aliased as EC)  and the subquery (aliased as emp). When the join succeeds, the two rows are  considered "matched" and the UPDATE specified in the WHEN MATCHED clause is  executed. Otherwise, no match is found and the INSERT in WHEN NOT MATCHED is  executed. Thus, rows from table EMP that do not have corresponding rows based on  EMPNO in table EMP_COMMISSION will be inserted into EMP_COMMISSION. Of &lt;a name="idx-CHP-4-0201"&gt;&lt;/a&gt;all the employees in table EMP only those in DEPTNO 10  should have their COMM updated in EMP_COMMISSION, while the rest of the  employees are inserted. Additionally, since MILLER is in DEPTNO 10 he is a  candidate to have his COMM updated, but because his SAL is less than 2000 it is  deleted from EMP_COMMISSION.&lt;/p&gt; ** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-4971568028889178288?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/4971568028889178288/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=4971568028889178288&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4971568028889178288'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4971568028889178288'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-merging-records.html' title='SQL Server : Merging Records'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2592204029139518368</id><published>2008-06-06T00:46:00.000-07:00</published><updated>2008-06-06T00:47:45.034-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Updating with Values from Another Table - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You wish to update rows in one table using values from another.  For example, you have a table called NEW_SAL, which holds the new salaries for  certain employees. The contents of table NEW_SAL are:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select *&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from new_sal&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO        SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10       4000&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Column DEPTNO is the primary key of table NEW_SAL. You want to  update the salaries and commission of certain employees in table EMP using &lt;a name="idx-CHP-4-0192"&gt;&lt;/a&gt;values table NEW_SAL if there is a match between  EMP.DEPTNO and NEW_SAL.DEPTNO, update EMP.SAL to NEW_SAL.SAL, and update  EMP.COMM to 50% of NEW_SAL.SAL. The rows in EMP are as follows:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select deptno,ename,sal,comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  order by 1&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO ENAME             SAL       COMM&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ---------- ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 CLARK           2450            &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 KING            5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10 MILLER          1300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SMITH            800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 ADAMS           1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 FORD            3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SCOTT           3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 JONES           2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 ALLEN           1600         300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 BLAKE           2850&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 MARTIN          1250        1400&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 JAMES            950&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 TURNER          1500           0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     30 WARD            1250         500&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a join between NEW_SAL and EMP to find and return the new  COMM values to the &lt;a name="idx-CHP-4-0193"&gt;&lt;/a&gt;UPDATE statement. It is quite  common for updates such as this one to be performed via correlated subquery.  Another technique involves creating a view (traditional or inline, depending on  what your database supports), then updating that view.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and MySQL&lt;/h5&gt; &lt;p class="docText"&gt;Use a correlated subquery to set new SAL and COMM values in  EMP. Also use a correlated subquery to identify which rows from EMP should be  updated:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 update emp e set (e.sal,e.comm) = (select ns.sal, ns.sal/2&lt;br /&gt; 2                                      from new_sal ns&lt;br /&gt; 3                                     where ns.deptno=e.deptno)&lt;br /&gt; 4  where exists ( select null&lt;br /&gt; 5                   from new_sal ns&lt;br /&gt; 6                  where ns.deptno = e.deptno )&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;The method for the DB2 solution will certainly work for Oracle,  but as an alternative, you can update an inline view:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 update (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2  select e.sal as emp_sal, e.comm as emp_comm,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3         ns.sal as ns_sal, ns.sal/2 as ns_comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4    from emp e, new_sal ns&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5   where e.deptno = ns.deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6 ) set emp_sal = ns_sal, emp_comm = ns_comm&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;The method used for the DB2 solution will work for PostgreSQL,  but as an alternative you can (quite conveniently) join directly in the &lt;a name="idx-CHP-4-0194"&gt;&lt;/a&gt;UPDATE statement:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 update emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2    set sal = ns.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        comm = ns.sal/2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   from new_sal ns&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5  where ns.deptno = emp.deptno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.2.4"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The method used for the DB2 solution will work for SQL Server,  but as an alternative you can (similarly to the PostgreSQL solution) join  directly in the UPDATE statement:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 update e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2    set e.sal  = ns.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3        e.comm = ns.sal/2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   from emp e,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5        new_sal ns&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6  where ns.deptno = e.deptno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Before discussing the different solutions, I'd like to mention  something important regarding updates that use &lt;a name="idx-CHP-4-0195"&gt;&lt;/a&gt;queries to supply new values. A WHERE clause in the  subquery of a correlated update is not the same as the WHERE clause of the table  being updated. If you look at the UPDATE statement in the "Problem" section, the  join on DEPTNO between EMP and NEW_SAL is done and returns rows to the SET  clause of the UPDATE statement. For employees in DEPTNO 10, valid values are  returned because there is a match DEPTNO in table NEW_SAL. But what about  employees in the other departments? NEW_SAL does not have any other departments,  so the SAL and COMM for employees in DEPTNOs 20 and 30 are set to NULL. Unless  you are doing so via LIMIT or TOP or whatever mechanism your vendor supplies for  limiting the number of rows returned in a result set, the only way to restrict  rows from a table in SQL is to use a WHERE clause. To correctly perform this  UPDATE, use a WHERE clause on the table being updated along &lt;a name="idx-CHP-4-0196"&gt;&lt;/a&gt;with a WHERE clause in the correlated subquery.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2 and MySQL&lt;/h5&gt; &lt;p class="docText"&gt;To ensure you do not update every row in table EMP, remember to  include a correlated subquery in the WHERE clause of the UPDATE. Performing the  join (the correlated subquery) in the SET clause is not enough. By using a WHERE  clause in the UPDATE, you ensure that only rows in EMP that match on DEPTNO to  table NEW_SAL are updated. This holds true for all RDBMSs.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;In the Oracle solution using the update join view, you are  using equi-joins to determine which rows will be updated. You can confirm which  rows are being updated by executing the query independently. To be able to  successfully use this type of UPDATE, you must first understand the concept of  key-preservation. The DEPTNO column of the table NEW_SAL is the primary key of  that table, thus its values are unique &lt;a name="idx-CHP-4-0197"&gt;&lt;/a&gt;within the  table. When joining between EMP and NEW_SAL, however, NEW_SAL.DEPTNO is not  unique in the result set, as can be seen below:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.empno, e.deptno e_dept, ns.sal, ns.deptno ns_deptno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp e, new_sal ns&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where e.deptno = ns.deptno&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; EMPNO     E_DEPT        SAL  NS_DEPTNO&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----- ---------- ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7782         10       4000         10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7839         10       4000         10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7934         10       4000         10&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;To enable Oracle to update this join, one of the tables must be  key-preserved, meaning that if its values are not unique in the result set, it  should at least be unique in the table it comes from. In this case NEW_SAL has a  primary key on DEPTNO, which makes it unique in the table. Because it is unique  in its table, it may appear multiple times in the result set and will still be  considered key-preserved, thus allowing the update to complete  successfully.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-10.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;PostgreSQL and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;The syntax is a bit different between these two platforms, but  the technique is the same. Being able to join directly in the &lt;a name="idx-CHP-4-0198"&gt;&lt;/a&gt;UPDATE statement is extremely convenient. Since you  specify which table to update (the table listed after the UPDATE keyword)  there's no confusion as to which table's rows are modified. Additionally,  because you are using joins in the update (since there is an explicit WHERE  clause), you can avoid some of the pitfalls when coding correlated subquery  updates; in particular, if you missed a join here, it would be very obvious  you'd have a problem.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2592204029139518368?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2592204029139518368/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2592204029139518368&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2592204029139518368'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2592204029139518368'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/updating-with-values-from-another-table.html' title='Updating with Values from Another Table - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2420106410145334558</id><published>2008-06-06T00:45:00.000-07:00</published><updated>2008-06-06T00:46:29.230-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Updating when Corresponding Rows Exist ::SQL Server::</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to update rows in one table when corresponding rows  exist in another. For example, if an employee appears in table EMP_BONUS, you  want to increase that employee's salary (in table EMP) by 20 percent. The  following result set represents the data currently in table EMP_BONUS:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select empno, ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp_bonus&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      EMPNO ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       7369 SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       7900 JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       7934 MILLER&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-9.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a subquery in your &lt;a name="idx-CHP-4-0190"&gt;&lt;/a&gt;UPDATE  statement's WHERE clause to find employees in table EMP that are also in table  EMP_BONUS. Your UPDATE will then act only on those rows, enabling you to  increase their salary by 20 percent:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 update emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2    set sal=sal*1.20&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where empno in ( select empno from emp_bonus )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-9.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The results from the subquery represent the rows that will be  updated in table EMP. The IN predicate tests values of EMPNO from the EMP table  to see whether they are in the list of EMPNO values returned by the subquery.  When they are, the corresponding SAL values are updated.&lt;/p&gt; &lt;p class="docText"&gt;Alternatively, you can use EXISTS instead of IN:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; update emp&lt;br /&gt;    set sal = sal*1.20&lt;br /&gt;  where exists ( select null&lt;br /&gt;                   from emp_bonus&lt;br /&gt;                  where emp.empno=emp_bonus.empno )&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You may be surprised to see NULL in the SELECT list of the  EXISTS subquery. Fear not, that NULL does not have an adverse effect on the  update. In my opinion it increases readability as it reinforces the fact that,  unlike the solution using a subquery with an IN operator, what will drive the  update (i.e., which rows will be updated) will be controlled by the WHERE clause  of the subquery, not the values returned as a result of the subquery's SELECT  list.&lt;/p&gt;&lt;a href="23961536.html"&gt;&lt;img alt="" src="images/pixel.jpg" border="0" height="1" width="1" /&gt;&lt;/a&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2420106410145334558?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2420106410145334558/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2420106410145334558&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2420106410145334558'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2420106410145334558'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/updating-when-corresponding-rows-exist.html' title='Updating when Corresponding Rows Exist ::SQL Server::'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3524042686239526681</id><published>2008-06-06T00:44:00.002-07:00</published><updated>2008-06-06T00:45:38.105-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server : Modifying Records in a Table</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to modify values for some or all &lt;a name="idx-CHP-4-0187"&gt;&lt;/a&gt;rows in a table. For example, you might want to increase  the salaries of everyone in department 20 by 10%. The following result set shows  the DEPTNO, ENAME, and SAL for employees in that department:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno,ename,sal&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 20&lt;br /&gt;  order by 1,3&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO ENAME             SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SMITH             800&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 ADAMS            1100&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 JONES            2975&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SCOTT            3000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 FORD             3000&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to bump all the SAL values by 10%.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-8.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the &lt;a name="idx-CHP-4-0188"&gt;&lt;/a&gt;UPDATE statement to modify  existing rows in a database table. For example:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 update emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2    set sal = sal*1.10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where deptno = 20&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-8.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Use the UPDATE statement along with a WHERE clause to specify  which rows to update; if you exclude a WHERE clause, then all rows are updated.  The expression SAL*1.10 in this solution returns the salary increased by  10%.&lt;/p&gt; &lt;p class="docText"&gt;When preparing for a mass update, you may wish to preview the  results. You can do that by issuing a SELECT statement that includes the  expressions you plan to put into your SET clauses. The following SELECT shows  the result of a 10% salary increase:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno,&lt;br /&gt;        ename,&lt;br /&gt;        sal      as orig_sal,&lt;br /&gt;        sal*.10  as amt_to_add,&lt;br /&gt;        sal*1.10 as new_sal&lt;br /&gt;   from emp&lt;br /&gt;  where deptno=20&lt;br /&gt;  order by 1,5&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO ENAME  ORIG_SAL AMT_TO_ADD  NEW_SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ ------ -------- ----------  -------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SMITH       800         80      880&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 ADAMS      1100        110     1210&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 JONES      2975        298     3273&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 SCOTT      3000        300     3300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     20 FORD       3000        300     3300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The salary increase is broken down into two columns: one to  show the increase over the old salary, and the other to show the new salary.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3524042686239526681?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3524042686239526681/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3524042686239526681&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3524042686239526681'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3524042686239526681'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-modifying-records-in-table.html' title='SQL Server : Modifying Records in a Table'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-1011266759763888953</id><published>2008-06-06T00:44:00.001-07:00</published><updated>2008-06-06T00:44:51.433-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Blocking Inserts to Certain Columns - Sql Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You wish to prevent users, or an errant software application,  from &lt;a name="idx-CHP-4-0186"&gt;&lt;/a&gt;inserting values into certain table columns. For  example, you wish to allow a program to insert into EMP, but only into the  EMPNO, ENAME, and JOB columns.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-7.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Create a view on the table exposing only those columns you wish  to expose. Then force all inserts to go through that view.&lt;/p&gt; &lt;p class="docText"&gt;For example, to create a view exposing the three columns in  EMP:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; create view new_emps as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select empno, ename, job&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Grant access to this view to those users and programs allowed  to populate only the three fields in the view. Do not grant those users insert  access to the EMP table. Users may then create new EMP records by inserting into  the NEW_EMPS view, but they will not be able to provide values for columns other  than the three that are specified in the view definition.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-7.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;When you insert into a simple view such as in the solution,  your database server will translate that insert into the underlying table. For  example, the following insert:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into new_emps&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    (empno ename, job)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    values (1, 'Jonathan', 'Editor')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;will be translated behind the scenes into:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    (empno ename, job)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    values (1, 'Jonathan', 'Editor')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;It is also possible, but perhaps less useful, to insert into an  inline view (currently only supported by Oracle):&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   (select empno, ename, job&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      from emp)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values (1, 'Jonathan', 'Editor')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;View insertion is a complex topic. The rules become very  complicated very quickly for all but the simplest of views. If you plan to make  use of the ability to insert into views, it is imperative that you consult and  fully understand your vendor documentation on the matter.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-1011266759763888953?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/1011266759763888953/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=1011266759763888953&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1011266759763888953'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/1011266759763888953'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/blocking-inserts-to-certain-columns-sql.html' title='Blocking Inserts to Certain Columns - Sql Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3882975971120239071</id><published>2008-06-06T00:43:00.000-07:00</published><updated>2008-06-06T00:44:03.061-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Inserting into Multiple Tables at Once **SQL Server**</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to take rows returned by a query and insert those rows  into multiple target tables. For example, you want to insert rows from DEPT into  tables DEPT_EAST, DEPT_WEST, and DEPT_MID. All three tables have the same  structure (same columns and data types) as DEPT and are currently empty.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The solution is to insert the result of a query into the target  tables. The difference from "Copying Rows from One Table into Another" is that  for this problem you have multiple target tables.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Use either the &lt;a name="idx-CHP-4-0181"&gt;&lt;/a&gt;INSERT ALL or &lt;a name="idx-CHP-4-0182"&gt;&lt;/a&gt;INSERT FIRST statement. Both share the same syntax  except for the choice between the ALL and FIRST keywords. The following  statement uses INSERT ALL to cause all possible target tables to be  considered:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1   insert all&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2     when loc in ('NEW YORK','BOSTON') then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3       &lt;/span&gt;&lt;a style="color: rgb(204, 0, 0);" name="idx-CHP-4-0183"&gt;&lt;/a&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;into dept_east (deptno,dname,loc) values (deptno,dname,loc)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4     when loc = 'CHICAGO' then&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5       into dept_mid (deptno,dname,loc) values (deptno,dname,loc)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6     else&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7       into dept_west (deptno,dname,loc) values (deptno,dname,loc)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 8     select deptno,dname,loc&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 9       from dept&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Insert into an inline view that performs a UNION ALL on the  tables to be inserted. You must also be sure to place constraints on the tables  that will ensure each row goes into the correct table:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; create table dept_east&lt;br /&gt; ( deptno integer,&lt;br /&gt;   dname  varchar(10),&lt;br /&gt;   loc    varchar(10) check (loc in ('NEW YORK','BOSTON')))&lt;br /&gt;&lt;br /&gt; create table dept_mid&lt;br /&gt; ( deptno integer,&lt;br /&gt;   dname  varchar(10),&lt;br /&gt;   loc    varchar(10) check (loc = 'CHICAGO'))&lt;br /&gt;&lt;br /&gt; create table dept_west&lt;br /&gt; ( deptno integer,&lt;br /&gt;   dname  varchar(10),&lt;br /&gt;   loc    varchar(10) check (loc = 'DALLAS'))&lt;br /&gt;&lt;br /&gt; 1  insert into (&lt;br /&gt; 2    select * from dept_west union all&lt;br /&gt; 3    select * from dept_east union all&lt;br /&gt; 4    select * from dept_mid&lt;br /&gt; 5  ) select * from dept&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL, PostgreSQL, and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;As of the time of this writing, these vendors do not support  multi-table inserts.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt; &lt;p class="docText"&gt;Oracle's multi-table insert uses WHEN-THEN-ELSE clauses to  evaluate the rows from the nested SELECT and insert them accordingly. In this  recipe's example, INSERT ALL and INSERT FIRST would produce the same result, but  there is a difference between the two. INSERT FIRST will break out of the  WHEN-THEN-ELSE evaluation as soon as it encounters a condition evaluating to  true; INSERT ALL will evaluate all conditions even if prior tests evaluate to  true. Thus, you can use INSERT ALL to insert the same row &lt;a name="idx-CHP-4-0184"&gt;&lt;/a&gt;into more than one table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;My DB2 solution is a bit of a hack. It requires that the tables  to be inserted into have constraints defined to ensure that each row evaluated  from the subquery will go into the correct table. The technique is to insert  into a view that is defined as the UNION ALL of the tables. If the check  constraints are not unique amongst the tables in the INSERT (i.e., multiple  tables have the same check constraint), the INSERT statement will not know where  to put the rows and it will fail.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-6.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;MySQL, PostgreSQL, and SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;As of the time of this writing, only Oracle and DB2 currently  provide mechanisms to insert rows returned by a query into one or more of  several tables within the same statement.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3882975971120239071?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3882975971120239071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3882975971120239071&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3882975971120239071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3882975971120239071'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/inserting-into-multiple-tables-at-once.html' title='Inserting into Multiple Tables at Once **SQL Server**'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3255279958554684867</id><published>2008-06-04T23:11:00.000-07:00</published><updated>2008-06-04T23:12:41.057-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Copying a Table Definition - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to create a new table having the same set of columns  as an existing table. For example, you want to create a copy of the DEPT table  and call it DEPT_2. You do not want to copy the rows, only the column structure  of the table.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;Use the LIKE clause with the &lt;a name="idx-CHP-4-0177"&gt;&lt;/a&gt;CREATE  TABLE command:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;create table dept_2 like dept&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle, MySQL, and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;Use the CREATE TABLE command with a subquery that returns no  rows:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 create table dept_2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2 as&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3 select *&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4   from dept&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5  where 1 = 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.2.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Use the INTO clause with a subquery that returns no rows:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;1 select *&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   into dept_2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3   from dept&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4  where 1 = 0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.3.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2&lt;/h5&gt; &lt;p class="docText"&gt;DB2's &lt;a name="idx-CHP-4-0178"&gt;&lt;/a&gt;CREATE TABLE…LIKE command  allows you to easily use one table as the pattern for creating another. Simply  specify your pattern table's name following the LIKE keyword.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.3.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle, MySQL, and PostgreSQL&lt;/h5&gt; &lt;p class="docText"&gt;When using Create Table As Select (CTAS), all rows from your  query will be used to populate the new table you are creating unless you specify  a false condition in the WHERE clause. In the solution provided, the expression  "1 = 0" in the WHERE clause of the query causes no rows to be returned. Thus the  result of the CTAS statement is an empty table based on the columns in the  SELECT clause of the query.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-5.3.3"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;When using INTO to copy a table, all rows from your query will  be used to populate the new table you are creating unless you specify a false  condition in the WHERE clause of your query. In the solution provided, the  expression "1 = 0" in the predicate of the query causes no rows to be returned.  The result is an empty table based on the columns in the SELECT clause of the  query.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3255279958554684867?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3255279958554684867/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3255279958554684867&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3255279958554684867'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3255279958554684867'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/copying-table-definition-sql-server.html' title='Copying a Table Definition - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-4380946686567645775</id><published>2008-06-04T23:10:00.000-07:00</published><updated>2008-06-04T23:11:44.546-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Copying Rows from One Table into Another **SQL SERVER**</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to copy rows from one table to another by using a  query. The query may be complex or simple, but ultimately you want the result to  be inserted into another table. For example, you want to copy rows from the DEPT  table to the DEPT_EAST table. The DEPT_EAST table has already been created with  the same structure (same columns and data types) as DEPT and is currently  empty.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-4.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the INSERT statement followed by a query to produce the  rows you want:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 insert into dept_east (deptno,dname,loc)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2 select deptno,dname,loc&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3   from dept&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4  where loc in ( 'NEW YORK','BOSTON' )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-4-SECT-4.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Simply follow the INSERT statement with a query that returns  the desired rows. If you want to copy all rows from the source table, exclude  the WHERE clause from the query. Like a regular insert, you do not have to  explicitly specify which columns you are inserting into. But if you do not  specify your target columns, you must insert into &lt;span class="docEmphasis"&gt;all&lt;/span&gt; of the table's columns, &lt;a name="idx-CHP-4-0176"&gt;&lt;/a&gt;and you must be mindful of the order of the values in  the SELECT list as described earlier in "Inserting a New Record."&lt;/p&gt;&lt;a href="23961536.html"&gt;&lt;img alt="" src="images/pixel.jpg" border="0" height="1" width="1" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-4380946686567645775?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/4380946686567645775/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=4380946686567645775&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4380946686567645775'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4380946686567645775'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/copying-rows-from-one-table-into.html' title='Copying Rows from One Table into Another **SQL SERVER**'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-4347821026207002028</id><published>2008-06-04T23:09:00.000-07:00</published><updated>2008-06-04T23:10:56.441-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL Server - Overriding a Default Value with NULL</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You are inserting into a column having a default value, and you  wish to override that default value by setting the column to NULL. Consider the  following table:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;create table D (id integer default 0, foo VARCHAR(10))&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;You wish to insert a row with a NULL value for ID.&lt;a name="sqlckbk-CHP-4-SECT-3.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;You can explicitly specify NULL in your values list:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into d (id, foo) values (null, 'Brighten')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;a name="sqlckbk-CHP-4-SECT-3.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;Not everyone realizes that you can explicitly specify NULL in  the values list of an &lt;a name="idx-CHP-4-0174"&gt;&lt;/a&gt;INSERT statement. Typically,  when you do not wish to specify a value for a column, you leave that column out  of your column and values lists:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into d (foo) values ('Brighten')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;Here, no value for ID is specified. Many would expect the  column to taken on the null value, but, alas, a default value was specified at  table creation time, so the result of the preceding INSERT is that ID takes on  the value 0 (the default). By specifying NULL as the value for a column, you can  set the column to NULL despite any default value.&lt;br /&gt;&lt;br /&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-4347821026207002028?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/4347821026207002028/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=4347821026207002028&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4347821026207002028'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/4347821026207002028'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/sql-server-overriding-default-value.html' title='SQL Server - Overriding a Default Value with NULL'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6666864746135423147</id><published>2008-06-04T23:08:00.000-07:00</published><updated>2008-06-04T23:09:27.167-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Inserting Default Values - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;A table can be defined to take default values for specific  columns. You want to insert a row of default values without having to specify  those values. Consider the following table:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;create table D (id integer default 0)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You want to insert zero without explicitly specifying zero in  the values list of an INSERT statement. You want to explicitly insert the  default, whatever that default is.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-2.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;All brands support use of the &lt;a name="idx-CHP-4-0170"&gt;&lt;/a&gt;DEFAULT keyword as a way of explicitly specifying the  default value for a column. Some brands provide additional ways to solve the  problem.&lt;/p&gt; &lt;p class="docText"&gt;The following example illustrates the use of the &lt;a name="idx-CHP-4-0171"&gt;&lt;/a&gt;DEFAULT keyword:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into D values (default)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You may also explicitly specify the column name, which you'll  need to do anytime you are not inserting into all columns of a table:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into D (id) values (default)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Oracle8&lt;span class="docEmphasis"&gt;i&lt;/span&gt; Database and prior  versions do not support the DEFAULT keyword. Prior to Oracle9&lt;span class="docEmphasis"&gt;i&lt;/span&gt; Database, there was no way to explicitly insert a  default column value.&lt;/p&gt; &lt;p class="docText"&gt;MySQL allows you to specify an empty values list if all columns  have a default value defined:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into D values ()&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;In this case, all columns will be set to their default  values.&lt;/p&gt; &lt;p class="docText"&gt;PostgreSQL and SQL Server support a &lt;a name="idx-CHP-4-0172"&gt;&lt;/a&gt;DEFAULT VALUES clause:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into D default values&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The DEFAULT VALUES clause causes all columns to take on their  default values.&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-2.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The DEFAULT keyword in the values list will insert the value  that was specified as the default for a particular column during table creation.  The keyword is available for all DBMSs.&lt;/p&gt; &lt;p class="docText"&gt;MySQL, PostgreSQL, and SQL Server users have another option  available if all columns in the table are defined with a default value (as table  D is in this case). You may use an empty VALUES list (MySQL) or specify the  DEFAULT VALUES clause (PostgreSQL and SQL Server) to create a new row with all  default values; otherwise, you need to specify DEFAULT for each column in the  table.&lt;/p&gt; &lt;p class="docText"&gt;For tables with a mix of default and non-default columns,  inserting default values for a column is as easy as excluding the column from  the insert list; you do not need to use the DEFAULT keyword. Say that table D  had an additional column that was not defined with a default value:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;create table D (id integer default 0, foo varchar(10))&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You can insert a default for ID by listing only FOO in the  insert list:&lt;/p&gt;&lt;pre&gt; &lt;span style="color: rgb(204, 0, 0);"&gt;insert into D (name) values ('Bar')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This statement will result in a row in which ID is 0 and FOO is  "Bar". ID takes on its default value because no other value is specified.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6666864746135423147?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6666864746135423147/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6666864746135423147&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6666864746135423147'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6666864746135423147'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/inserting-default-values-sql-server.html' title='Inserting Default Values - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-5315293760376296699</id><published>2008-06-04T23:06:00.000-07:00</published><updated>2008-06-04T23:07:58.264-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Inserting a New Record - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to insert a new record into a table. For example, you  want to insert a new record into the DEPT table. The value for DEPTNO should be  50, DNAME should be "PROGRAMMING", and LOC should be "BALTIMORE".&lt;/p&gt;&lt;a name="sqlckbk-CHP-4-SECT-1.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use the &lt;a name="idx-CHP-4-0167"&gt;&lt;/a&gt;INSERT statement &lt;a name="idx-CHP-4-0168"&gt;&lt;/a&gt;with the &lt;a name="idx-CHP-4-0169"&gt;&lt;/a&gt;VALUES clause to  insert one row at a time:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; insert into dept (deptno,dname,loc)&lt;br /&gt; values (50,'PROGRAMMING','BALTIMORE')&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;For DB2 and MySQL you have the option of inserting one row at a  time or multiple rows at a time by including multiple VALUES lists:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; /* multi row insert */&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept (deptno,dname,loc)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values (1,'A','B'),&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        (2,'B','C')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-4-SECT-1.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The INSERT statement allows you to create new rows in database  tables. The syntax for inserting a single row is consistent across all database  brands.&lt;/p&gt; &lt;p class="docText"&gt;As a shortcut, you can omit the column list in an INSERT  statement:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into dept&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; values (50,'PROGRAMMING','BALTIMORE')&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;However, if you do not list your target columns, you must  insert into &lt;span class="docEmphasis"&gt;all&lt;/span&gt; of the columns in the table, and  be mindful of the order of the values in the VALUES list; you must supply values  in the same order in which the database displays columns in response to a SELECT  * query.&lt;/p&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-5315293760376296699?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/5315293760376296699/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=5315293760376296699&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5315293760376296699'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/5315293760376296699'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/inserting-new-record-sql-server.html' title='Inserting a New Record - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-6907145759782264253</id><published>2008-06-04T03:18:00.000-07:00</published><updated>2008-06-04T03:19:08.096-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Using NULLs in Operations and Comparisons **SQL**</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;NULL is never equal to or not equal to any value, not even  itself, but you want to evaluate values returned by a nullable column like you  would evaluate real values. For example, you want to find all employees in EMP  whose commission (COMM) is less than the commission of employee "WARD".  Employees with a NULL commission should be included as well.&lt;/p&gt;&lt;a name="sqlckbk-CHP-3-SECT-12.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a function such as COALESCE to transform the NULL value  into a real value that can be used in standard evaluation:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select ename,comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3  where coalesce(comm,0) &lt; ( select comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4                                  from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5                                 where ename = 'WARD' )&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-12.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The &lt;a name="idx-CHP-3-0164"&gt;&lt;/a&gt;COALESCE function will return  the first non-NULL value from the list of values passed to it. When a NULL value  is encountered it is replaced by zero, which is then compared with Ward's  commission. This can be seen by putting the COALESCE function in the SELECT  list:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select ename,comm,coalesce(comm,0)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where coalesce(comm,0) &lt; ( select comm&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                                  from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                                 where ename = 'WARD' )&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ENAME            COMM COALESCE(COMM,0)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ---------- ---------- ----------------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SMITH                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ALLEN             300              300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JONES                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  BLAKE                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  CLARK                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  SCOTT                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  KING                                 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  TURNER              0                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ADAMS                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  JAMES                                0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  FORD                                 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  MILLER                               0&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-6907145759782264253?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/6907145759782264253/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=6907145759782264253&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6907145759782264253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/6907145759782264253'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/using-nulls-in-operations-and.html' title='Using NULLs in Operations and Comparisons **SQL**'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-3973435321074973138</id><published>2008-06-04T03:16:00.000-07:00</published><updated>2008-06-04T03:18:17.482-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Returning Missing Data from Multiple Tables : SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to return &lt;a name="idx-CHP-3-0157"&gt;&lt;/a&gt;missing data from  multiple tables simultaneously. Returning rows from table DEPT that do not exist  in table EMP (any departments that have no employees) requires an outer join.  Consider the following query, which returns all DEPTNOs and DNAMEs from DEPT  along with the names of all the employees in each department (if there is an  employee in a particular department):&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select d.deptno,d.dname,e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dept d left outer join emp e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     on (d.deptno=e.deptno)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    DEPTNO DNAME          ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------- -------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        20 RESEARCH       SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        20 RESEARCH       JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        10 ACCOUNTING     CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        20 RESEARCH       SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        10 ACCOUNTING     KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        20 RESEARCH       ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        30 SALES          JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        20 RESEARCH       FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        10 ACCOUNTING     MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        40 OPERATIONS&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The last row, the OPERATIONS department, is returned despite  that department not having any employees, because table EMP was outer joined to  table DEPT. Now, suppose there was an employee without a department. How would  you return the above result set along with a row for the employee having no  department? In other words, you want to outer join to both table EMP and table  DEPT, and in the same query. After creating the new employee, a first attempt  may look like this:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; insert into emp (empno,ename,job,mgr,hiredate,sal,comm,deptno)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select 1111,'YODA','JEDI',null,hiredate,sal,comm,null&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where ename = 'KING'&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select d.deptno,d.dname,e.ename&lt;br /&gt;   from dept d right outer join emp e&lt;br /&gt;     on (d.deptno=e.deptno)&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO DNAME        ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ------------ ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                         YODA&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This outer join manages to return the new employee but lost the  OPERATIONS department from the original result set. The final result set should  return a row for YODA as well as OPERATIONS, such as the following:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     DEPTNO DNAME        ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ------------ --------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         10 ACCOUNTING   MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         20 RESEARCH     SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         30 SALES        WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;         40 OPERATIONS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                          YODA&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-11.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;Use a full outer join to return &lt;a name="idx-CHP-3-0158"&gt;&lt;/a&gt;missing &lt;a name="idx-CHP-3-0159"&gt;&lt;/a&gt;data from both tables  based on a common value.&lt;/p&gt;&lt;a name="sqlckbk-CHP-3-SECT-11.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2, MySQL, PostgreSQL, SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Use the explicit FULL OUTER JOIN command to return missing rows  from both tables along with matching rows:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select d.deptno,d.dname,e.ename&lt;br /&gt; 2   from dept d full outer join emp e&lt;br /&gt; 3     on (d.deptno=e.deptno)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Alternatively, union the results of two different outer  joins:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 1 select d.deptno,d.dname,e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 2   from dept d right outer join emp e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 3     on (d.deptno=e.deptno)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 4  union&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 5 select d.deptno,d.dname,e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 6   from dept d left outer join emp e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 7     on (d.deptno=e.deptno)&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-11.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt;&lt;a name="idx-CHP-3-0160"&gt;&lt;/a&gt; &lt;p class="docText"&gt;If you are on Oracle9&lt;span class="docEmphasis"&gt;i&lt;/span&gt; &lt;a name="idx-CHP-3-0161"&gt;&lt;/a&gt;Database or later, you can use either of the preceding  solutions. Alternatively, you can use Oracle's proprietary outer join syntax,  which is the only choice for users on Oracle8&lt;span class="docEmphasis"&gt;i&lt;/span&gt;  Database and earlier:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; 1 select d.deptno,d.dname,e.ename   &lt;br /&gt; 2   from dept d, emp e&lt;br /&gt; 3  where d.deptno = e.deptno(+)&lt;br /&gt; 4  union&lt;br /&gt; 5 select d.deptno,d.dname,e.ename&lt;br /&gt; 6   from dept d, emp e&lt;br /&gt; 7  where d.deptno(+) = e.deptno&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-11.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The full outer join is simply the combination of &lt;a name="idx-CHP-3-0162"&gt;&lt;/a&gt;outer joins on both tables. To see how a full outer join  works "under the covers," simply run each outer join, then union the results.  The following query returns rows from table DEPT and any matching rows from  table EMP (if any).&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select d.deptno,d.dname,e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dept d left outer join emp e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     on (d.deptno = e.deptno)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   DEPTNO DNAME          ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   ------ -------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       20 RESEARCH       SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          ALLEN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       20 RESEARCH       JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       10 ACCOUNTING     CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       20 RESEARCH       SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       10 ACCOUNTING     KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       20 RESEARCH       ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       30 SALES          JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       20 RESEARCH       FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       10 ACCOUNTING     MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       40 OPERATIONS&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;This next query returns rows from table EMP and any matching  rows from table DEPT (if any):&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select d.deptno,d.dname,e.ename&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from dept d right outer join emp e&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     on (d.deptno = e.deptno)&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DEPTNO DNAME          ENAME&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ------ -------------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      10 ACCOUNTING     MILLER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      10 ACCOUNTING     KING&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      10 ACCOUNTING     CLARK&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 RESEARCH       FORD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 RESEARCH       ADAMS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 RESEARCH       SCOTT&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 RESEARCH       JONES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      20 RESEARCH       SMITH&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          JAMES&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          TURNER&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          BLAKE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          MARTIN&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          WARD&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      30 SALES          ALLEN    &lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                        YODA&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The results from these two queries are unioned to provide the  final result set.&lt;/p&gt;&lt;br /&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-3973435321074973138?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/3973435321074973138/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=3973435321074973138&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3973435321074973138'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/3973435321074973138'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/returning-missing-data-from-multiple.html' title='Returning Missing Data from Multiple Tables : SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-2956585429007074113</id><published>2008-06-04T03:15:00.000-07:00</published><updated>2008-06-04T03:16:52.448-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>Performing Outer Joins when Using Aggregates - SQL Server</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;Begin with the same problem as in 3.9, but modify table  EMP_BONUS such that the difference in this case is not all employees in  department 10 have been given bonuses. Consider the EMP_BONUS table and a query  to (ostensibly) find both the sum of all salaries for department 10 and the sum  of all bonuses for all employees in department 10:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select * from emp_bonus&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      EMPNO RECEIVED          TYPE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       7934 17-MAR-2005          1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       7934 15-FEB-2005          2&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; &lt;/span&gt;&lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select deptno,&lt;br /&gt;        sum(sal) as total_sal,&lt;br /&gt;        sum(bonus) as total_bonus&lt;br /&gt;   from (&lt;br /&gt; select e.empno,&lt;br /&gt;        e.ename,&lt;br /&gt;        e.sal,&lt;br /&gt;        e.deptno,&lt;br /&gt;        e.sal*case when eb.type = 1 then .1&lt;br /&gt;                   when eb.type = 2 then .2&lt;br /&gt;                   else .3 end as bonus&lt;br /&gt;   from emp e, emp_bonus eb&lt;br /&gt;  where e.empno  = eb.empno&lt;br /&gt;    and e.deptno = 10&lt;br /&gt;        )&lt;br /&gt;  group by deptno&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  DEPTNO  TOTAL_SAL TOTAL_BONUS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  ------ ---------- -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      10       2600         390&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;The result for TOTAL_BONUS is correct, but the value returned  for TOTAL_SAL does not represent the sum of all salaries in department 10. The  following query shows why the TOTAL_SAL is incorrect:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal*case when eb.type = 1 then .1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   when eb.type = 2 then .2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              else .3 end as bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp e, emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where e.empno = eb.empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    and e.deptno = 10&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     EMPNO ENAME          SAL     DEPTNO      BONUS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------- ---------  ------- ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      7934 MILLER        1300         10        130&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;      7934 MILLER        1300         10        260&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Rather than sum all salaries in department 10, only the salary  for "MILLER" is summed and it is erroneously summed twice. Ultimately, you would  like to return the following result set:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt; DEPTNO TOTAL_SAL TOTAL_BONUS&lt;br /&gt; ------ --------- -----------&lt;br /&gt;     10      8750         390&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-10.2"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Solution&lt;/h4&gt; &lt;p class="docText"&gt;The solution is similar to that of 3.9, but here you &lt;a name="idx-CHP-3-0150"&gt;&lt;/a&gt;outer join to EMP_BONUS to ensure all employees from  department 10 are included.&lt;/p&gt;&lt;a name="sqlckbk-CHP-3-SECT-10.2.1"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;DB2, MySQL, PostgreSQL, SQL Server&lt;/h5&gt; &lt;p class="docText"&gt;Outer join to EMP_BONUS, then perform the sum on only distinct  salaries from department 10:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2        sum(distinct sal) as total_sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3        sum(bonus) as total_bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5 select e.empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        e.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        e.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9        e.sal*case when eb.type is null then 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10                   when eb.type = 1 then .1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11                   when eb.type = 2 then .2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12                   else .3 end as bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13   from emp e left outer join emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14     on (e.empno = eb.empno)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15  where e.deptno = 10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16        )&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17  group by deptno&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;You can also use the window function SUM OVER:&lt;/p&gt;&lt;pre&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  1 select distinct deptno,total_sal,total_bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  2   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  3 select e.empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  4        e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  5        sum(distinct e.sal) over&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  6        (partition by e.deptno) as total_sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7        e.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  8        sum(e.sal*case when eb.type is null then 0&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  9                       when eb.type = 1 then .1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 10                       when eb.type = 2 then .2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 11                       else .3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 12                  end) over&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 13        (partition by deptno) as total_bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 14   from emp e left outer join emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 15     on (e.empno = eb.empno)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 16  where e.deptno = 10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; 17        ) x&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;a name="sqlckbk-CHP-3-SECT-10.2.2"&gt;&lt;/a&gt; &lt;h5 class="docSection3Title"&gt;Oracle&lt;/h5&gt;&lt;a name="idx-CHP-3-0151"&gt;&lt;/a&gt; &lt;p class="docText"&gt;If you are using Oracle9&lt;span class="docEmphasis"&gt;i&lt;/span&gt; &lt;a name="idx-CHP-3-0152"&gt;&lt;/a&gt;Database or later you can use the preceding solution.  Alternatively, you can use the proprietary Oracle outer-join syntax, which is  mandatory for users on Oracle8&lt;span class="docEmphasis"&gt;i&lt;/span&gt; Database and  earlier:&lt;/p&gt;&lt;pre style="color: rgb(204, 0, 0);"&gt;  1 select deptno,&lt;br /&gt;  2        sum(distinct sal) as total_sal,&lt;br /&gt;  3        sum(bonus) as total_bonus&lt;br /&gt;  4   from (&lt;br /&gt;  5 select e.empno,&lt;br /&gt;  6        e.ename,&lt;br /&gt;  7        e.sal,&lt;br /&gt;  8        e.deptno,&lt;br /&gt;  9        e.sal*case when eb.type is null then 0&lt;br /&gt; 10                   when eb.type = 1 then .1&lt;br /&gt; 11                   when eb.type = 2 then .2&lt;br /&gt; 12                   else .3 end as bonus&lt;br /&gt; 13   from emp e, emp_bonus eb&lt;br /&gt; 14  where e.empno  = eb.empno (+)&lt;br /&gt; 15    and e.deptno = 10&lt;br /&gt; 16        )&lt;br /&gt; 17  group by deptno&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Oracle 8&lt;span class="docEmphasis"&gt;i&lt;/span&gt; Database users can  also use the &lt;a name="idx-CHP-3-0153"&gt;&lt;/a&gt;SUM OVER syntaxshown for DB2 and the  other databases, but must modify it to use the proprietary Oracle &lt;a name="idx-CHP-3-0154"&gt;&lt;/a&gt;outer-join syntax shown in the preceding query.&lt;/p&gt;&lt;a name="sqlckbk-CHP-3-SECT-10.3"&gt;&lt;/a&gt; &lt;h4 class="docSection2Title"&gt;Discussion&lt;/h4&gt; &lt;p class="docText"&gt;The second query in the "Problem" section of this recipe joins  table EMP and table EMP_BONUS and returns only rows for employee "MILLER", which  is what causes the error on the sum of EMP.SAL (the other employees in DEPTNO 10  do not have bonuses and their salaries are not included in the sum). The  solution is to outer join table EMP to table EMP_BONUS so even employees without  a bonus will be included in the result. If an employee does not have a bonus,  NULL will be returned for EMP_BONUS.TYPE. It is important to keep this in mind  as the CASE statement has been modified and is slightly different from solution  3.9. If EMP_BONUS.TYPE is NULL, the CASE expression returns zero, which has no  effect on the sum.&lt;/p&gt; &lt;p class="docText"&gt;The following query is an alternative solution. The sum of all  salaries in department 10 is computed first, then joined to table EMP, which is  then joined to table EMP_BONUS (thus avoiding the outer join). The following  query works for all DBMSs:&lt;/p&gt;&lt;pre&gt; &lt;b style="color: rgb(204, 0, 0);"&gt;&lt;br /&gt; select d.deptno,&lt;br /&gt;        d.total_sal,&lt;br /&gt;        sum(e.sal*case when eb.type = 1 then .1&lt;br /&gt;                       when eb.type = 2 then .2&lt;br /&gt;                       else .3 end) as total_bonus&lt;br /&gt;   from emp e,&lt;br /&gt;        emp_bonus eb,&lt;br /&gt;       (&lt;br /&gt; select deptno, sum(sal) as total_sal&lt;br /&gt;   from emp&lt;br /&gt;  where deptno = 10&lt;br /&gt;  group by deptno&lt;br /&gt;        ) d&lt;br /&gt;  where e.deptno = d.deptno&lt;br /&gt;    and e.empno = eb.empno&lt;br /&gt;  group by d.deptno,d.total_sal&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    DEPTNO  TOTAL_SAL TOTAL_BONUS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; --------- ---------- -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        10       8750         390&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;** If you want the Full Table detail. Refer the SQL Table in Label List. Or &lt;a target="_blank" href="http://microcodes.blogspot.com/search/label/SQL%20Table"&gt;Click here&lt;/a&gt; to View the Table&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8158036542551301938-2956585429007074113?l=microcodes.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://microcodes.blogspot.com/feeds/2956585429007074113/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8158036542551301938&amp;postID=2956585429007074113&amp;isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2956585429007074113'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8158036542551301938/posts/default/2956585429007074113'/><link rel='alternate' type='text/html' href='http://microcodes.blogspot.com/2008/06/performing-outer-joins-when-using.html' title='Performing Outer Joins when Using Aggregates - SQL Server'/><author><name>VK</name><uri>http://www.blogger.com/profile/09584364339540356821</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8158036542551301938.post-4649858123030529352</id><published>2008-06-04T03:13:00.000-07:00</published><updated>2008-06-04T03:15:34.819-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='SQL'/><title type='text'>SQL - Performing Joins when Using Aggregates</title><content type='html'>&lt;h4 class="docSection2Title"&gt;Problem&lt;/h4&gt; &lt;p class="docText"&gt;You want to perform an aggregation but your query involves &lt;a name="idx-CHP-3-0141"&gt;&lt;/a&gt;multiple tables. You want to ensure that joins do not  disrupt the aggregation. For example, you want to find the sum of the salaries  for employees in department 10 along with the sum of their bonuses. Some  employees have more than one bonus and the join between table EMP and table  EMP_BONUS is causing incorrect values to be returned by the aggregate function  SUM. For this problem, table EMP_BONUS contains the following &lt;a name="idx-CHP-3-0142"&gt;&lt;/a&gt;data:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select * from emp_bonus&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; EMPNO RECEIVED          TYPE&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----- ----------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7934 17-MAR-2005          1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7934 15-FEB-2005          2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7839 15-FEB-2005          3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  7782 15-FEB-2005          1&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Now, consider the following query that returns the salary and  bonus for all employees in department 10. Table BONUS.TYPE determines the amount  of the bonus. A type 1 bonus is 10% of an employee's salary, type 2 is 20%, and  type 3 is 30%.&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal*case when eb.type = 1 then .1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   when eb.type = 2 then .2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   else .3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              end as bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  from emp e, emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; where e.empno  = eb.empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   and e.deptno = 10&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   EMPNO ENAME             SAL     DEPTNO     BONUS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------- ---------- ---------- ---------- ---------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    7934 MILLER           1300         10       130&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    7934 MILLER           1300         10       260&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    7839 KING             5000         10      1500&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    7782 CLARK            2450         10       245&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;So far, so good. However, things go awry when you attempt a  join to the EMP_ BONUS table in order to sum the bonus amounts:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        sum(sal) as total_sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        sum(bonus) as total_bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from (&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.empno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.deptno,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal*case when eb.type = 1 then .1&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   when eb.type = 2 then .2&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;                   else .3&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;              end as bonus&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp e, emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where e.empno  = eb.empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    and e.deptno = 10&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        ) x&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  group by deptno&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; DEPTNO   TOTAL_SAL  TOTAL_BONUS&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ------ -----------  -----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;     10       10050         2135&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;While the TOTAL_BONUS is correct, the TOTAL_SAL is incorrect.  The sum of all salaries in department 10 is 8750, as the following query  shows:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select sum(sal) from emp where deptno=10&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   SUM(SAL)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;       8750&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt;Why is TOTAL_SAL incorrect? The reason is the duplicate rows in  the SAL column created by the join. Consider the following query, which &lt;a name="idx-CHP-3-0143"&gt;&lt;/a&gt;joins table EMP and EMP_ BONUS:&lt;/p&gt;&lt;pre&gt; &lt;b&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; select e.ename,&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;        e.sal&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;   from emp e, emp_bonus eb&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;  where e.empno  = eb.empno&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt;    and e.deptno = 10&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ENAME             SAL&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; ---------- ----------&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; CLARK            2450&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; KING             5000&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER           1300&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(204, 0, 0);"&gt; MILLER           1300&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p class="docText"&gt
