﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Order By with Case statement / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 21:53:07 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>I suggested that as an alternative.    I assume that in his case, he wants to know the value that doesn't have a dash in it.  But, if that's the case, it's probably more efficient to just return the the string to the right of the dash WITHOUT converting it to an integer.And then there's the question of ordering in this sequence 1,2,11 (integer) or 1,11,2 (char).    Fun, fun, fun.   :-)</description><pubDate>Fri, 05 Jun 2009 08:44:31 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>RE: Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>[quote][b]ramadesai108 (6/3/2009)[/b][hr]Hi Bob,   Converting the results to VarChar did fix it. Thanks for your help.[/quote]Now you are doing a lot of extra work. After your IsNumeric check, just return the value instead of converting it to an int. When the IsNumeric returns 0, and you are returning a zero, return a '0' instead.</description><pubDate>Wed, 03 Jun 2009 18:17:51 GMT</pubDate><dc:creator>WayneS</dc:creator></item><item><title>RE: Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>Hi Bob,   Converting the results to VarChar did fix it. Thanks for your help.</description><pubDate>Wed, 03 Jun 2009 13:24:19 GMT</pubDate><dc:creator>ramadesai108</dc:creator></item><item><title>RE: Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>The ORDER BY wants all values to be of the same datatype.   Where you find a '-' in the string for ssid, such that a numeric value is available, you are returning that numeric value as an integer.    Where you don't find a '-', you are returning the entire string 'ABC26'.    You either need to CONVERT the integer results to varchar, or arbitrarily return a 0 or null instead of ssid, where there is no '-##' in the string.</description><pubDate>Wed, 03 Jun 2009 13:12:28 GMT</pubDate><dc:creator>The Dixie Flatline</dc:creator></item><item><title>RE: Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>What is wrong?</description><pubDate>Wed, 03 Jun 2009 12:56:21 GMT</pubDate><dc:creator>ramadesai108</dc:creator></item><item><title>Order By with Case statement</title><link>http://www.sqlservercentral.com/Forums/Topic728384-338-1.aspx</link><description>Hi All,    I am running the following SQL statement with Case statement in Order by clause:SELECT ssID FROM TblAORDER BY 	CASE WHEN CHARINDEX('-',ssID) = 0 THEN --there are no dashes		ssID     	ELSE 		 CASE WHEN ISNUMERIC(SUBSTRING(ssID,CHARINDEX('-',ssID) + 1,LEN(ssID) - 							CHARINDEX('-',ssID))) = 1 THEN			CONVERT(INT,SUBSTRING(ssID,CHARINDEX('-',ssID) + 1,LEN(ssID) - 							CHARINDEX('-',ssID)))		  ELSE --there are dashes but no numbers after dashes			ssID		  END 	 ENDHere is the data:AB26-1AB26-2AB26-3AB26-4AB26-5AB26Running the query error out with:Conversion failed when converting the nvarchar value 'AB26' to data type intIt seems to be erroring on the last line of data, "AB26", which does not have a dash.Since it does not have a dash, it should not even go thru the 2nd case statement.What is wrong here?</description><pubDate>Wed, 03 Jun 2009 10:59:39 GMT</pubDate><dc:creator>ramadesai108</dc:creator></item></channel></rss>