Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to write some code that will grab all my information in one SQL query but sort it so that there are two groups, and then each group is alphabetical.

My current query (which is close) is:

SELECT *, PATINDEX('%heartbeat%', lower(TimerName)) As IsHeartBeat FROM tblTimer ORDER BY IsHeartBeat, TimerName, TimerInterval, TimerPriority

The PATINDEX correctly puts all my heartbeat timers to the end of the result set ... but because it doesn't return just a 0 or 1, true or false, it orders the heartbeats according to where the word heartbeat is found in the name, rather than alphabetically.

I want an inline IF (IIF) but when I try that in management studio it says it's an invalid function.

I want to call this from VB.Net (VS2010) and the databases are dynamically created so I would prefer not to use a Stored Proc. unless I can easily create it dynamically (never done that previously but I suspect it's possible).

Suggestions/Comments/Recommendations ... all welcome.

Thanks in advance.
Posted
Comments
Bala Selvanayagam 5-Oct-11 11:30am    
Look at the SQL server CASE STATEMENT and that can help you
BHort 5-Oct-11 11:48am    
Totally what I needed. I didn't think it would be but it's exactly the solution. For other peoples benefit, I'm going to post it as a solution ... although you should really get the credit.
Bala Selvanayagam 5-Oct-11 12:04pm    
You are welcome BHort

1 solution

The solution (or one solution) is:

SQL
SELECT TimerName, TimerInterval, TimerPriority, IsHeartBeat =
CASE
 WHEN PATINDEX('%heartbeat%', lower([TimerName])) > 0 THEN 1
 ELSE 0
 END
 FROM [SEMCDatalogger].[dbo].[tblTimer] ORDER BY IsHeartBeat, TimerName, TimerInterval, TimerPriority


By using IsHeartBeat = CASE and then supplying the conditions ... the results are exactly the 0 or 1 that I need.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900