SQL syntax for parsing

I have a table TableA with the following  field value for column 'Property'
[EmployeeID]|ASC;[AddressID]|DESC        

Then I need the output as
EmployeeID ASC, AddressID DESC (eliminating the pipes in the same order as they appear)
If there were sorts on other columns, then it would be appended following a semicolon.

We cannot predetermine on how many columns the sorting is done..
So the value of 'Property' could be:
[EmployeeID]|ASC;[AddressID]|DESC;[Name]|ASC;[Title]|DESC   (and so on)
The above field value for 'Property' in this case needs to be parsed to get the following value:
ORDER BY EmployeeID ASC, AddressID DESC, Name ASC, Title DESC
       
spiroseAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmgarnettCommented:
If you use a user defined function or stored procedure, you can build up your SQL command as a string and use the replace function to substitute the ";" characters for a "," character.

Once the string contains the parsed command, you can then use the EXEC('string with command') the run the real query and then return the results.

That way you can pass your sort order string into the function/procedure. It can parse it for you and then return the results of the search.
0
LowfatspreadCommented:
'order by '+replace(replace(property,';',','),'|',' ')
0
spiroseAuthor Commented:
Thanks lowfatspread but one more Question how do you incorporate changes like this?

[EmployeeID]|0;[AddressID]|1;[Name]|0;[Title]|1   (and so on)

If it is 0 then DESC and if it is 1 then ASC in the code you provided above...
thanks
0
LowfatspreadCommented:
'order by '+replace(replace(replace(property,';',','),'|0',' Desc '),'|1',' Asc')
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
spiroseAuthor Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.