Click here to Skip to main content
15,923,576 members
Home / Discussions / Database
   

Database

 
AnswerRe: table construction/design question Pin
Damodar Periwal24-Jan-06 12:53
Damodar Periwal24-Jan-06 12:53 
GeneralRe: table construction/design question Pin
Jim Crafton24-Jan-06 15:35
Jim Crafton24-Jan-06 15:35 
GeneralRe: table construction/design question Pin
Damodar Periwal24-Jan-06 15:46
Damodar Periwal24-Jan-06 15:46 
QuestionHow to send email on perticular date and time? Pin
vicky45723-Jan-06 13:57
vicky45723-Jan-06 13:57 
AnswerRe: How to send email on perticular date and time? Pin
Qaiser Mehmood Mughal24-Jan-06 3:33
Qaiser Mehmood Mughal24-Jan-06 3:33 
GeneralRe: How to send email on perticular date and time? Pin
vicky45724-Jan-06 6:58
vicky45724-Jan-06 6:58 
Questionarrange by nearest date? Pin
jszpila23-Jan-06 10:03
jszpila23-Jan-06 10:03 
AnswerRe: arrange by nearest date? Pin
Colin Angus Mackay23-Jan-06 11:47
Colin Angus Mackay23-Jan-06 11:47 
jszpila wrote:
As perviously mentioned the query is huge so I'd like to avoid using a union if at all possible, just to reduce clutter


Big Grin | :-D Yes, it can be done without UNIONs

SELECT dateColumn, 
       ABS(DATEDIFF(minute, dateColumn, @distanceFromThisTime)) AS TimeDifference,
       CASE WHEN dateColumn IS NULL THEN 0 ELSE 1 END AS Nothing
FROM myTable
ORDER BY CASE WHEN dateColumn IS NULL THEN 0 ELSE 1 END DESC, 
         ABS(DATEDIFF(minute, dateColumn, @distanceFromThisTime)) ASC

In the above code:
dateColumn is the column that contains the date information.
@distanceFromThisTime is a datetime variable that contains the time that the query is centred on, probably getdate() from your description.
The ORDER BY clause first of all sinks the NULLs to the bottom, then it orders the remaining stuff in ascending order.
Finally, you can obviously ditch the TimeDifference and Nothing columns, I put them in to illustrate what values might be returned if you wanted to try this code in query analyzer first.

Does this help?

ColinMackay.net
"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius
"If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell


-- modified at 17:48 Monday 23rd January, 2006

GeneralRe: arrange by nearest date? Pin
jszpila23-Jan-06 12:25
jszpila23-Jan-06 12:25 
GeneralRe: arrange by nearest date? Pin
Colin Angus Mackay23-Jan-06 12:32
Colin Angus Mackay23-Jan-06 12:32 
GeneralRe: arrange by nearest date? Pin
jszpila24-Jan-06 3:45
jszpila24-Jan-06 3:45 
QuestionGetDate() in Stored Procedure Pin
Michael Flanakin23-Jan-06 5:35
Michael Flanakin23-Jan-06 5:35 
GeneralRe: GetDate() in Stored Procedure Pin
Michael Flanakin23-Jan-06 5:38
Michael Flanakin23-Jan-06 5:38 
GeneralRe: GetDate() in Stored Procedure Pin
Michael Flanakin23-Jan-06 5:54
Michael Flanakin23-Jan-06 5:54 
AnswerRe: GetDate() in Stored Procedure Pin
Qaiser Mehmood Mughal24-Jan-06 3:15
Qaiser Mehmood Mughal24-Jan-06 3:15 
Questionusing ado Pin
yamunasenthilvel23-Jan-06 4:58
yamunasenthilvel23-Jan-06 4:58 
AnswerRe: using ado Pin
S Douglas24-Jan-06 0:53
professionalS Douglas24-Jan-06 0:53 
Questionhelp with deletion and updation with the same code.. Pin
yamunasenthilvel23-Jan-06 2:06
yamunasenthilvel23-Jan-06 2:06 
Questionis it possible?? Pin
imsathy22-Jan-06 23:21
imsathy22-Jan-06 23:21 
AnswerRe: is it possible?? Pin
Colin Angus Mackay22-Jan-06 23:30
Colin Angus Mackay22-Jan-06 23:30 
GeneralRe: is it possible?? Pin
imsathy22-Jan-06 23:43
imsathy22-Jan-06 23:43 
GeneralRe: is it possible?? Pin
Paul Conrad23-Jan-06 19:21
professionalPaul Conrad23-Jan-06 19:21 
GeneralRe: is it possible?? Pin
imsathy23-Jan-06 22:30
imsathy23-Jan-06 22:30 
GeneralRe: is it possible?? Pin
Paul Conrad24-Jan-06 3:29
professionalPaul Conrad24-Jan-06 3:29 
QuestionT-SQL Problem Pin
WDI22-Jan-06 21:35
WDI22-Jan-06 21:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.