Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Sorting Dynmaic sub-Query with condition

Posted on 2014-02-24
Last Modified: 2014-02-24
Hi all

Given the code below.. How can I use the column name in a case or if statment.
I need to send a specific code if Text1 column appear in the dynamic query.
i.e if  (ColumnName.'Text1' = Text1)
          process something
if  (ColumnName.'Text2' = Text2)
          process something

Thanks in Advance

	@DynamicQuery = ISNULL(@DynamicQuery+' UNION ','') 
	+ 'SELECT  CONVERT(varchar(max),'+COLUMN_NAME+') as varColumnName, 
	'+@VarCusHeaderID+' as fk_Header_ID,  
	'+@varProviderID+' as fk_ProviderID, 
	'+@varCurIndex+' as PositionIndex, 
	'+@varIsStatus+' as IsStatus		    
	FROM '+@TransactionTable+
	' y' +ISNULL('  WHERE '+@TableWhere,'')
WHERE (table_name = @TransactionTable

Open in new window

Question by:ZURINET
  • 3
  • 2
LVL 12

Expert Comment

by:Harish Varghese
ID: 39882209

Where do you want to use the CASE or IF statement. Is it within the above SELECT statement? One point to note is that you cannot use IF statement inside a SELECT statement. And you may not replace the functionality of IF statement by a CASE. Please exlain in detail what you really want to achieve.


Author Comment

ID: 39882298
Hi Harish

One can use If in a select statement.. see code below

Here is what I want to achive..
given the following columns ('Text1_','Text2_', 'Text10_'.. ..)

Since I am sorting the column using ORDER BY SUBSTRING(COLUMN_NAME,4,1)
and I have columns with  Text1_ & Text10_
Both are returning the same index..
Hence I need a way to make sure that for the indexposition.. I will reference Text10_ as position 10 and not as position 1 as the case may be..

                  WHEN Obsolete = 'N' or InStock = 'Y' 
                     THEN 1 
                  ELSE 0 
             END AS bit) as Salable, * 
FROM Product

Open in new window

LVL 12

Accepted Solution

Harish Varghese earned 500 total points
ID: 39882343

The code you showed in your previous post uses CASE, not IF.

Referring back to your previous question about same SQL query, I dont think you will get desired result by adding an order by at the end of your query as ORDER BY SUBSTRING(COLUMN_NAME,5,2), since that will only sort the SELECT statements in your dynamic query, not your result.

Coming back to your new question about Text1_ & Text10_. You can take two characters from 5th position, remove "_" and then convert to integer as below:
ORDER BY Convert(int, Replace(SUBSTRING(COLUMN_NAME,5,2) , '_', ''))


Author Closing Comment

ID: 39882706
It is not working.. but it is a good idea
LVL 12

Expert Comment

by:Harish Varghese
ID: 39883037
Did you try my code to use a separate id to sort the data in your previous question? Please post a new question if you are still looking for a solution.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Row insertion failed. Array 5 46
SQL trigger 5 19
SQL create line numbers for data sampling 11 26
Location of Dynamics AX Service accounts in SQL 3 15
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question