SQL syntax for parsing

I have a table TableA with the following  field value for column 'Property'

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
Who is Participating?
LowfatspreadConnect With a Mentor Commented:
'order by '+replace(replace(replace(property,';',','),'|0',' Desc '),'|1',' Asc')
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.
'order by '+replace(replace(property,';',','),'|',' ')
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...
spiroseAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.