<?xml version="1.0" encoding="UTF-8"?> <rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>Fundamental Provocation &#187; SQL</title> <atom:link href="http://www.ajaymatharu.com/tag/sql/feed/" rel="self" type="application/rss+xml" /><link>http://www.ajaymatharu.com</link> <description>Blog by Ajay Matharu</description> <lastBuildDate>Sun, 06 Nov 2011 15:09:39 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" /> <item><title>Insert data from XML in SQL</title><link>http://www.ajaymatharu.com/insert-data-from-xml-in-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=insert-data-from-xml-in-sql</link> <comments>http://www.ajaymatharu.com/insert-data-from-xml-in-sql/#comments</comments> <pubDate>Fri, 26 Mar 2010 04:56:37 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Microsoft]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Insert xml in sql]]></category> <category><![CDATA[Multiple Records]]></category> <category><![CDATA[OpenXML]]></category> <category><![CDATA[SQL with XML]]></category> <category><![CDATA[xml format]]></category> <category><![CDATA[XML in SQL]]></category> <category><![CDATA[XML with SQL]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=2188</guid> <description><![CDATA[Its been very long since I have written any technical post. So here I am, with small example of how to insert data from XML into SQL. This is extremely helpful if you want to insert multiple records in one go into SQL, you just need to create the XML and pass it to SQL. [...]]]></description> <content:encoded><![CDATA[<p>Its been very long since I have written any technical post. So here I am, with small example of how to insert data from XML into SQL. This is extremely helpful if you want to insert multiple records in one go into SQL, you just need to create the XML and pass it to SQL. SQL will fetch the data from the XML and save it into its tables you have mapped.</p><p>Just to demonstrate I am creating a sample table. Since it is sample I have not created any Primary Key on it.</p><pre class="sql" name="code">CREATE TABLE [dbo].[Employees](
	[EmpId] [int] NOT NULL,
	[FirstName] [varchar](50) NULL,
	[Dept] [varchar](50) NULL
)
</pre><p><br/><br /> Once table has been created you are ready to insert data into it,</p><pre class="sql" name="code">declare @TestDoc int
declare @TestDoc int
exec sp_xml_preparedocument @TestDoc output,
N'<Root>
<Employees Dept="Marketing">
<Employee EmpID="1231" FirstName="Ethan"></Employee>
<Employee EmpID="1232" FirstName="Ashish"></Employee>
</Employees>
<Employees Dept="IT">
<Employee EmpID="1241" FirstName="Ajay"></Employee>
<Employee EmpID="1242" FirstName="Rahul"></Employee>
</Employees>
<Employees Dept="DBA">
<Employee EmpID="1251" FirstName="AJ"></Employee>
<Employee EmpID="1251" FirstName="Nilesh"></Employee>
</Employees>
</Root>'
insert into employees(empid, firstname, dept)
select empid, firstname, dept
from openxml(@TestDoc, N'/Root/Employees/Employee')
With (EmpId varchar(5) '@EmpID',
    FirstName varchar(10) '@FirstName',
    Dept varchar(10) '../@Dept')
exec sp_xml_removedocument @TestDoc
</pre><p><br/><br /> Alternatively, you can change your XML format like this,</p><pre class="sql" name="code">
declare @TestDoc int
exec sp_xml_preparedocument @TestDoc output,
N'<Root>
<Employees Dept="Marketing">
<Employee EmpID="1231">Ethans</Employee>
<Employee EmpID="1232">Ajay</Employee>
</Employees>
<Employees Dept="IT">
<Employee EmpID="1241">Rahul</Employee>
<Employee EmpID="1242">Sneha</Employee>
</Employees>
<Employees Dept="DBA">
<Employee EmpID="1251">Nilesh</Employee>
<Employee EmpID="1251">Matharu</Employee>
</Employees>
</Root>'
select *
from openxml(@TestDoc, N'/Root/Employees/Employee')
With (EmpId varchar(5) '@EmpID',
    FirstName varchar(10) '.',
    Dept varchar(10) '../@Dept')
exec sp_xml_removedocument @TestDoc
</pre><p><br/></p><p>If you want to fetch your existing data from SQL in XML format,<br /> With values as attributes,</p><pre class="sql" name="code">select * from employees
for xml auto
</pre><p><br/><br /><div id="attachment_2189" class="wp-caption aligncenter" style="width: 574px"><img class="size-full wp-image-2189" title="Output Data as Attributes" src="http://www.ajaymatharu.com/wp-content/uploads/2010/03/Data-as-attribute.png" alt="Output Data as Attributes" width="564" height="119" /><p class="wp-caption-text">Output Data as Attributes</p></div><br /> <br/><br /> With values as elements,</p><pre class="sql" name="code">select * from employees
for xml auto, elements
</pre><p><br/><br /><div id="attachment_2190" class="wp-caption aligncenter" style="width: 319px"><img class="size-full wp-image-2190" title="Output Data as Elements" src="http://www.ajaymatharu.com/wp-content/uploads/2010/03/data-as-elements.png" alt="Output Data as Elements" width="309" height="519" /><p class="wp-caption-text">Output Data as Elements</p></div></p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/insert-data-from-xml-in-sql/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>TSQL Cast Vs Convert</title><link>http://www.ajaymatharu.com/tsql-cast-vs-convert/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=tsql-cast-vs-convert</link> <comments>http://www.ajaymatharu.com/tsql-cast-vs-convert/#comments</comments> <pubDate>Thu, 10 Sep 2009 05:02:43 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Cast]]></category> <category><![CDATA[Convert]]></category> <category><![CDATA[Convert Vs Cast]]></category> <category><![CDATA[TSQL]]></category><guid isPermaLink="false">http://ajaymatharu.wordpress.com/?p=663</guid> <description><![CDATA[SQL Server programmers can choose between two functions in SQL Server 7 and 2000 for converting expressions from one type to another. In many cases there will be a need within a stored procedure or other routine to convert data from, say, a datetime type to a varchar type; CONVERT and CAST are used for [...]]]></description> <content:encoded><![CDATA[<p><span class="a3">SQL Server programmers can choose between two functions in SQL Server 7 and 2000 for converting expressions from one type to another. In many cases there will be a need within a stored procedure or other routine to convert data from, say, a <strong>datetime</strong> type to a <strong>varchar</strong> type; <strong>CONVERT</strong> and <strong>CAST</strong> are used for such things.</p><p>Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. <strong>CONVERT</strong> is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifiers.</p><p><strong>CAST</strong> is the more ANSI-standard of the two functions, meaning that while it&#8217;s more portable (i.e., a function that uses <strong>CAST</strong> can be used in other database applications more or less as-is), it&#8217;s also less powerful. <strong>CAST</strong> is also required when converting between <strong>decimal</strong> and <strong>numeric</strong> values to preserve the number of decimal places in the original expression.  For those reasons, it&#8217;s best to use <strong>CAST</strong> first, unless there is some specific thing that only <strong>CONVERT</strong> can provide in the work you&#8217;re doing.</p><p><strong>CAST</strong> and <strong>CONVERT</strong> can also be used in conjunction with each other to achieve certain effects.  For instance, a typical way to produce a <strong>char</strong> variable with the current date would be to use:</p><pre>SELECT CONVERT(CHAR(10), CURRENT_TIMESTAMP, 102)</pre><p>(The <strong>102</strong> indicates that the ANSI date format, <em>yy.mm.dd</em>, is to be used.)</p><p>However, if you wanted to cast this variable explicitly as a <strong>datetime</strong> or <strong>smalldatetime</strong> value for compatibility in a specific database column, you could use:</p><pre>SELECT CAST(CONVERT(CHAR(10),CURRENT_TIMESTAMP,102) AS DATETIME</pre><p>This would return the value <em>yy.mm.dd</em> 00:00:00 (i.e., 12:00AM as the timestamp; the time information from <strong>CURRENT_TIMESTAMP</strong> would be discarded).</p><p></span></p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/tsql-cast-vs-convert/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Insert multiple records with single Insert statement in SQL</title><link>http://www.ajaymatharu.com/insert-multiple-records-with-single-insert-statement-in-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=insert-multiple-records-with-single-insert-statement-in-sql</link> <comments>http://www.ajaymatharu.com/insert-multiple-records-with-single-insert-statement-in-sql/#comments</comments> <pubDate>Tue, 08 Sep 2009 04:31:26 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Insert]]></category> <category><![CDATA[Microsoft SQL]]></category> <category><![CDATA[Multiple Inserts]]></category> <category><![CDATA[Multiple Records]]></category> <category><![CDATA[Query]]></category> <category><![CDATA[Select]]></category> <category><![CDATA[SQL Insert]]></category> <category><![CDATA[SQL Query]]></category> <category><![CDATA[TSQL Query]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1175</guid> <description><![CDATA[This is the traditional method for inserting multiple values in a table. USE YourDBName GO INSERT INTO MyTableName (FirstCol, SecondCol) VALUES (‘First’,1); INSERT INTO MyTableName (FirstCol, SecondCol) VALUES (‘Second’,2); INSERT INTO MyTableName (FirstCol, SecondCol) VALUES (‘Third’,3); INSERT INTO MyTableName (FirstCol, SecondCol) VALUES (‘Fourth’,4); INSERT INTO MyTableName (FirstCol, SecondCol) VALUES (‘Fifth’,5); GO However this cannot be [...]]]></description> <content:encoded><![CDATA[<p>This is the traditional method for inserting multiple values in a table.</p><pre name="code" class="sql">
USE YourDBName
GO
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘First’,1);
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘Second’,2);
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘Third’,3);
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘Fourth’,4);
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘Fifth’,5);
GO
</pre><p>However this cannot be used if you want to insert multiple values into the table via your code because in this case you&#8217;ll have to write the code to insert the value in the DB in a for loop which is not at all feasible.</p><p>So you can write the code to generate the insert statement like,</p><pre name="code" class="sql">
USE YourDB
GO
INSERT INTO MyTableName (FirstCol, SecondCol)
SELECT ‘First’ ,1
UNION ALL
SELECT ‘Second’ ,2
UNION ALL
SELECT ‘Third’ ,3
UNION ALL
SELECT ‘Fourth’ ,4
UNION ALL
SELECT ‘Fifth’ ,5
GO
</pre><p>The above code inserts multiple values into DB with a single SQL insert query. This is the best means to use in your code as you&#8217;ll just have to create this query in your for loop and the query will run only once.</p><p>Also you can use the following query syntax for multiple inserts in SQL 2008</p><pre name="code" class="sql">
USE YourDB
GO
INSERT INTO MyTableName (FirstCol, SecondCol)
VALUES (‘First’,1),
(‘Second‘,2),
(‘Third‘,3),
(‘Fourth‘,4),
(‘Fifth‘,5)
</pre><p>Please let me know if you have any better ideas than this one.<br /> Njoy coding!!!</p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/insert-multiple-records-with-single-insert-statement-in-sql/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Finding a column in database</title><link>http://www.ajaymatharu.com/finding-a-column-in-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=finding-a-column-in-database</link> <comments>http://www.ajaymatharu.com/finding-a-column-in-database/#comments</comments> <pubDate>Sat, 25 Jul 2009 06:03:32 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Query]]></category> <category><![CDATA[System Query]]></category> <category><![CDATA[System Table]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1374</guid> <description><![CDATA[Here is the query that will help you to find a particular column in the database. This query will return you the table name in which that column is found. select so.name, sc.name from syscolumns sc inner join sysobjects so on sc.id = so.id where sc.name = 'SystemColumn']]></description> <content:encoded><![CDATA[<p>Here is the query that will help you to find a particular column in the database. This query will return you the table name in which that column is found.</p><pre name="code" class="sql">
select so.name, sc.name
from syscolumns sc
inner join sysobjects so on sc.id = so.id
where sc.name = 'SystemColumn'
</pre>]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/finding-a-column-in-database/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Data Scripter Add-in for SQL</title><link>http://www.ajaymatharu.com/data-scripter-add-in-for-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=data-scripter-add-in-for-sql</link> <comments>http://www.ajaymatharu.com/data-scripter-add-in-for-sql/#comments</comments> <pubDate>Fri, 24 Jul 2009 03:34:43 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[SQL]]></category> <category><![CDATA[data scripter]]></category> <category><![CDATA[sql script]]></category> <category><![CDATA[table data scripter]]></category><guid isPermaLink="false">http://ajaymatharu.wordpress.com/?p=364</guid> <description><![CDATA[Many a times you feel the need to script databases, tables, stored procedures etc. But you already have these utilities that help you to script these database objects. There is no script generator for the data present in the table. For e.g. you want to move data from one server to another, to accomplish this you have the script [...]]]></description> <content:encoded><![CDATA[<p>Many a times you feel the need to script databases, tables, stored procedures etc. But you already have these utilities that help you to script these database objects. There is no script generator for the data present in the table. For e.g. you want to move data from one server to another, to accomplish this you have the script generator for table but not for data within it.</p><p>There is an Add-on for the SQL Management studio that helps you to generate the Table data scripts. You can download the add-in from <a href="http://www.box.net/shared/gmqsoj5c7p" target="_blank">here</a>.</p><ul><li>After installing, when you right-click on a table node in Management Studio, you will see a &#8220;Script Data for [table name] option in the menu.</li><li>Selecting this will bring up a dialog letting you choose a few file options (File, Clipboard, New Query Window) and a few script options. (Disable Constraints, Triggers, etc&#8230;)</li></ul><p>This add-in works best for tables with small amounts of data. If you have a large amount of data then you should use the &#8220;Script to File&#8221; option, as you are likely to run out of memory.</p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/data-scripter-add-in-for-sql/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Query to list all tables and columns in database</title><link>http://www.ajaymatharu.com/query-to-list-all-tables-and-columns-in-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=query-to-list-all-tables-and-columns-in-database</link> <comments>http://www.ajaymatharu.com/query-to-list-all-tables-and-columns-in-database/#comments</comments> <pubDate>Thu, 25 Jun 2009 04:30:41 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Query]]></category> <category><![CDATA[System Tables]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1371</guid> <description><![CDATA[Here is the SQL query that will help you list all the tables and columns along with its data type and length in a particular database. This query fires query to the system table that stores all the information about the database. select table_name, column_name, data_type, character_maximum_length, is_nullable from information_schema.columns where table_name in (select name [...]]]></description> <content:encoded><![CDATA[<p>Here is the SQL query that will help you list all the tables and columns along with its data type and length in a particular database.</p><p>This query fires query to the system table that stores all the information about the database.</p><pre name="code" class="sql">
select table_name, column_name, data_type, character_maximum_length, is_nullable from information_schema.columns where table_name in (select name from sysobjects where xtype='U') order by table_name
</pre><p>Hope this helps, enjoy <img src='http://www.ajaymatharu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/query-to-list-all-tables-and-columns-in-database/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Cast Vs Convert in SQL</title><link>http://www.ajaymatharu.com/cast-vs-convert-in-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=cast-vs-convert-in-sql</link> <comments>http://www.ajaymatharu.com/cast-vs-convert-in-sql/#comments</comments> <pubDate>Wed, 24 Jun 2009 10:24:17 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Cast]]></category> <category><![CDATA[Cast Vs Convert]]></category> <category><![CDATA[Convert]]></category> <category><![CDATA[SQL 2005]]></category> <category><![CDATA[SQl Datatype]]></category> <category><![CDATA[SQL Query]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1376</guid> <description><![CDATA[Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifier. CAST is the more ANSI-standard of the two [...]]]></description> <content:encoded><![CDATA[<p>Because SQL Server provides both functions, there may be some confusion about which is best to use and under what circumstances. CONVERT is specific to SQL Server, and allows for a greater breadth of flexibility when converting between date and time values, fractional numbers, and monetary signifier.</p><p>CAST is the more ANSI-standard of the two functions, meaning that while it&#8217;s more portable (i.e., a function that uses CAST can be used in other database applications more or less as-is), it&#8217;s also less powerful. CAST is also required when converting between decimal and numeric values to preserve the number of decimal places in the original expression. For those reasons, it&#8217;s best to use CAST first, unless there is some specific thing that only CONVERT can provide in the work you&#8217;re doing.</p><p>CAST and CONVERT can also be used in conjunction with each other to achieve certain effects. For instance, a typical way to produce a char variable with the current date would be to use:</p><pre class="sql">SELECT CONVERT(CHAR(10), CURRENT_TIMESTAMP, 102)</pre><p>(The 102 indicates that the ANSI date format, yy.mm.dd, is to be used.)</p><p>However, if you wanted to cast this variable explicitly as a datetime or smalldatetime value for compatibility in a specific database column, you could use:</p><pre class="sql">SELECT CAST(CONVERT(CHAR(10),CURRENT_TIMESTAMP,102) AS DATETIME)</pre><p>This would return the value yy.mm.dd 00:00:00 (i.e., 12:00AM as the timestamp; the time information from CURRENT_TIMESTAMP would be discarded).</p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/cast-vs-convert-in-sql/feed/</wfw:commentRss> <slash:comments>2</slash:comments> </item> <item><title>Find out Nth highest value in SQL</title><link>http://www.ajaymatharu.com/find-out-nth-highest-value-in-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=find-out-nth-highest-value-in-sql</link> <comments>http://www.ajaymatharu.com/find-out-nth-highest-value-in-sql/#comments</comments> <pubDate>Sun, 21 Jun 2009 07:51:52 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[Microsoft]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Nth highest]]></category> <category><![CDATA[Query]]></category> <category><![CDATA[SQL 2005]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1367</guid> <description><![CDATA[Since I am looking for a job change and this is the question I&#8217;m being asked in all of my interviews, I am sharing this with everyone hoping this will help you guys. The most common type of question is find out the 5th highest value, find out the 2nd highest value, and so on. [...]]]></description> <content:encoded><![CDATA[<p>Since I am looking for a job change and this is the question I&#8217;m being asked in all of my interviews, I am sharing this with everyone hoping this will help you guys.</p><p>The most common type of question is find out the 5th highest value, find out the 2nd highest value, and so on.</p><p>The solution for this question goes like this, I am going to fetch 5th highest salary for an employee.</p><pre name="code" class="sql">
Select top 1 FirstName, Salary From Employees Where Salary Not In (Select Distinct Top 4 Salary From Employees order by Salary desc) order by Salary desc
</pre><p>Enjoy and have fun <img src='http://www.ajaymatharu.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/find-out-nth-highest-value-in-sql/feed/</wfw:commentRss> <slash:comments>3</slash:comments> </item> <item><title>Rename SQL column</title><link>http://www.ajaymatharu.com/rename-sql-column/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rename-sql-column</link> <comments>http://www.ajaymatharu.com/rename-sql-column/#comments</comments> <pubDate>Thu, 23 Apr 2009 04:06:20 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Microsoft]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[EXEC]]></category> <category><![CDATA[rename]]></category> <category><![CDATA[rename column]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=973</guid> <description><![CDATA[Here is the script to rename an SQL column, EXEC sp_rename @objname = 'table_name.old_column_name', @newname = 'new_column_name', @objtype = 'COLUMN']]></description> <content:encoded><![CDATA[<p>Here is the script to rename an SQL column,</p><pre name="code" class="sql">
EXEC sp_rename
@objname = 'table_name.old_column_name',
@newname = 'new_column_name',
@objtype = 'COLUMN'
</pre>]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/rename-sql-column/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> <item><title>Using case conditions in SQL</title><link>http://www.ajaymatharu.com/using-case-conditions-in-sql/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-case-conditions-in-sql</link> <comments>http://www.ajaymatharu.com/using-case-conditions-in-sql/#comments</comments> <pubDate>Mon, 06 Apr 2009 04:28:43 +0000</pubDate> <dc:creator>Ajay Matharu</dc:creator> <category><![CDATA[Development]]></category> <category><![CDATA[SQL]]></category> <category><![CDATA[Case]]></category> <category><![CDATA[Select]]></category><guid isPermaLink="false">http://www.ajaymatharu.com/?p=1115</guid> <description><![CDATA[This is how you can user select case in sql statements, USE AdventureWorks; GO SELECT ProductNumber, Category = CASE ProductLine WHEN 'R' THEN 'Road' WHEN 'M' THEN 'Mountain' WHEN 'T' THEN 'Touring' WHEN 'S' THEN 'Other sale items' ELSE 'Not for sale' END, Name FROM Production.Product ORDER BY ProductNumber; GO USE AdventureWorks; GO SELECT ProductNumber, [...]]]></description> <content:encoded><![CDATA[<p>This is how you can user select case in sql statements,</p><pre name="code" class="sql">
USE AdventureWorks;
GO
SELECT   ProductNumber, Category =
CASE ProductLine
WHEN 'R' THEN 'Road'
WHEN 'M' THEN 'Mountain'
WHEN 'T' THEN 'Touring'
WHEN 'S' THEN 'Other sale items'
ELSE 'Not for sale'
END,
Name
FROM Production.Product
ORDER BY ProductNumber;
GO
</pre><pre name="code" class="sql">
USE AdventureWorks;
GO
SELECT   ProductNumber, Name, 'Price Range' =
CASE
WHEN ListPrice =  0 THEN 'Mfg item - not for resale'
WHEN ListPrice &lt; 50 THEN 'Under $50'
WHEN ListPrice &gt;= 50 and ListPrice &lt; 250 THEN 'Under $250'
WHEN ListPrice &gt;= 250 and ListPrice &lt; 1000 THEN 'Under $1000'
ELSE 'Over $1000'
END
FROM Production.Product
ORDER BY ProductNumber ;
GO
</pre><p>this is similar to if condition that you use in any other programming language but this you can use within a query to check the value and perform the respective task.</p> ]]></content:encoded> <wfw:commentRss>http://www.ajaymatharu.com/using-case-conditions-in-sql/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
