Are you sure it ignores the order by d desc?
Can you try to run it twice once with desc and one without desc? is the results are the same?
Main Topics
Browse All TopicsHi I have the following code
SELECT *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
Except (SELECT top 1 *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
)
order by d desc
what i want to do is only output the top 5, so I tried
SELECT top 5 *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
Except (SELECT top 1 *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
)
order by d desc
but then that ignores order by d desc this statement
Please help
Thanks,
R8VI
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is a shorter version in regards with date conversion. You dont have to convert a date back to datetime after you converted it to varchar. The comparition or usage in a datetime function will do that automatically for you. To a datetime you can add integers directly and that means adding days.
In regards to order it should work.
You didn't specify an ORDER BY in the EXCEPT ( SELECT TOP 1 ... ), so the row is essentially randomly selected.
Which row are you trying to except/exclude? The latest/last one?
Something like this should be easier:
SELECT *
FROM (
SELECT top 5 *
FROM (
SELECT top 6 *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
order by d desc
) as derived
order by d asc
) as derived2
order by d desc
That is, get the top 6, get the last 5 of those, then re-sort then into desc order. An extra sort of only 5 rows will be *very* fast.
Business Accounts
Answer for Membership
by: gkernPosted on 2009-01-07 at 06:13:12ID: 23314674
select top 5 * from (
SELECT *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
Except (SELECT top 1 *
FROM N
WHERE d < DATEADD(day, 1, CONVERT(datetime, CONVERT(varchar(10), GETDATE(), 120), 120))
)
order by d desc
)