circular view

Marco Dell'Oca 201 Reputation points
2024-07-07T09:47:50.9833333+00:00

Good morning,

I need to create a view that should display records starting from the contents of a field.

Let me explain:

I have a series of records with a field containing the month and day sorted by month and day:

0501

0602

0709

1110

1225

For example, by selecting 0709, I would like to receive in response all the data after 0709 inclusive and all the data from 0501 up to 0602.

Basically the result of the view should be like this:

0709

1110

1225

0501

0602

Is this possible?

Thanks for your help.

Marco Dell'Oca

SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2024-07-07T10:12:14.86+00:00

    Since it is problematic to order the data inside the view, the ORDER BY can be used outside the view. Maybe it does not have sense to define a view. To achieve the desired results, try a query:

    declare @date char(4) = '0709'
    
    select * 
    from MyTable
    order by case when Field >= @date then 1 else 2 end, Field
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.