|
I have a small problem with returning SQL data in a set order back to my C# program. I'm using Visual Studio 2005 and SQL server 2005 Express Edition (also the SQL Server Management Studio Express tool for testing my SQL commands).
I have opened a Reader from my C# program to my database (works OK) and I now need to read back some data sorted in a specific way.
My database has a table of depart times (stored as text, not strictly time formatted) which I need to sort in a specific way. I want all times (24 hour clock) that are >=0300 and in time sorted order. Afterwards, I want all times that are <0300 also in time sorted order. For example:
SELECT * FROM MyData WHERE Depart BETWEEN 0300 AND 2359 ORDER BY Depart ASC, Arrival ASC
SELECT * FROM MyData WHERE Depart BETWEEN 0000 AND 0259 ORDER BY Depart ASC, Arrival ASC
I want to be able to retrieve data from both SQL commands in a single data reader without executing each SQL statement and retrieving the data separately (it may be that the second statement might not return data in some cases).
I tried to use the UNION command between the two statements to return everything, but because the ORDER BY statement is used, the UNION command returns an error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'UNION'.
How can I return the data from both of these statements in a single pass?
Each command executes perfectly when executed on it's own. If I place the UNION command between the two statements and remove the 'ORDER BY Depart ASC, Arrival ASC', it returns the data I want, but sometimes the data is not stored in the SQL database in time order, THATS why I use the ORDER BY to try and fix the order of the data.
Can anyone help me please?
Thanks,
Sean |