Solved

Sorting Dynmaic sub-Query with condition

Posted on 2014-02-24
5
264 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
[X]
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
  • 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

726 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