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
Solved

Sorting Dynmaic sub-Query with condition

Posted on 2014-02-24
5
262 Views
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)
  begin
          process something
End
if  (ColumnName.'Text2' = Text2)
  begin
          process something
End

Thanks in Advance

SELECT
	@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,'')
FROM INFORMATION_SCHEMA.COLUMNS
WHERE (table_name = @TransactionTable
AND COLUMN_NAME IN (
				'Text1',
				'Text2',
				'Text3',
				'Text4',
				'Text5',
				'Text6',
				'Text7',
				'Text8',
				'Text9',
				'Text10'
				)
			)
ORDER BY SUBSTRING(COLUMN_NAME,5,2)

Open in new window

0
Comment
Question by:ZURINET
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 39882209
Hello,

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.

-Harish
0
 

Author Comment

by:ZURINET
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..









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

Open in new window

0
 
LVL 12

Accepted Solution

by:
Harish Varghese earned 500 total points
ID: 39882343
Hello,

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) , '_', ''))

-Harish
0
 

Author Closing Comment

by:ZURINET
ID: 39882706
It is not working.. but it is a good idea
0
 
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.
0

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