SYED
SYED

Reputation: 1

SQL Query with Order by Strings

dim Symbol_Val as string

I put my preference in entering the stocks according to my order in symbol_val

For example

Symbol val="stock1,stock2,stock3,stock4,stock5,stock6,stock7,stock8,stock9"

Then I execute the query

"Select Symbol, trade_date , sec_close,volume from stockPrice WHERE symbol in (" & Symbol_Val & ") and NO_OF_RECORDS= (SELECT max(no_of_records) FROM stockPrice_Alert) ORDER BY" & Symbol_Val

When I execute the query I am not getting the records as per my preference can anyone tell me what the query is required to be execute in SQL?

Upvotes: 0

Views: 47

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1271231

One method is to use charindex() to find the value. A simplistic method is:

Select Symbol, trade_date, sec_close, volume
from stockPrice
where symbol in (" & Symbol_Val & ") and NO_OF_RECORDS = (SELECT max(no_of_records) FROM stockPrice_Alert)
order by charindex(Symbol_Val, val)

It is better to take the delimiters into account:

order by charindex(','+Symbol_Val+',', ','+val+',')

Upvotes: 1

Related Questions