Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6138125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:48:43+00:00 2026-05-23T17:48:43+00:00

Using SQL Server 2008… I’m having some troubles in trying to order my rows

  • 0

Using SQL Server 2008…

I’m having some troubles in trying to order my rows in a specific order that I would like them to be ordered by. I’ve found a few examples that use the ORDER BY CASE clause, but am unsure whether using this method will produce the result that I want it to, thus I come to the community!

Here’s what I have:

First, I select, if it exists, a distinct year that is equal to the current year:

IF EXISTS(SELECT DISTINCT [Year]
FROM Assessment WHERE ProjectCode = @ProjectCode AND [Year] = DATENAME(YEAR, GETDATE()))

SELECT DISTINCT [Year]
FROM Assessment WHERE ProjectCode = @ProjectCode

But, then I find some confusion in ordering the results. I’d like to set the current year to the first row returned using the ORDER BY clause, then order the rest of the returned years in a descending order, here’s what I have so far:

ORDER BY (CASE WHEN [Year] = (DATENAME(YEAR, GETDATE())) THEN 1
          ELSE 100 END) ASC, [Year] desc

Next, if the current year is not contained in the query, select each year and order by year descending.

    ELSE
SELECT DISTINCT [Year]
FROM Assessment WHERE ProjectCode = @ProjectCode
ORDER BY [Year] desc

Thanks, in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:48:44+00:00Added an answer on May 23, 2026 at 5:48 pm

    You’re question isn’t very clear because you don’t specify what is broken or where you’re having issues. From what I gather, however, you don’t need an IF/ELSE. Instead you could do something like …

    SELECT DISTINCT [Year],  
            CASE [Year] 
                WHEN DATENAME(Year, GETDATE()) THEN 9999
                ELSE [Year] END  AS GarbageSoDistinctWorks
    FROM Assessment 
    WHERE ProjectCode = @ProjectCode
    ORDER BY
            CASE [Year] 
                WHEN DATENAME(Year, GETDATE()) THEN 9999
                ELSE [Year] END  DESC 
    

    FYI … i added the case to the select list as a throw away column to avoid the error I assume you’re getting.. There are other ways, like a derived table, but for now this should work..

    Msg 145, Level 15, State 1, Line 2
    ORDER BY items must appear in the select list if SELECT DISTINCT is specified.
    

    HTH,
    -eric

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to setup the Entity Framework with SQL Server 2008. I'm using Guids
(Using SQL Server 2008) I need some help visualizing a solution. Let's say I
Using SQL Server 2008 and any available features of TSQL, I am trying to
Using SQL Server 2008, I have a requirement that email addresses in my user
I'm using SQL Server 2008. I'm trying to find out which connection within to
I'm using SQL Server 2008 and just recently started having an intermittent problem while
I am using SQL Server 2008. I have a table (TBL_FILE) that stores user
Using SQL Server 2008 I'm trying to build up a string and execute it
I am using SQL Server 2008 in a asp.net/c# program. I am trying to
I'm using SQL Server 2008 with Advanced Services on my Vista Home Premium. I'd

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.