SQL custom sorting

Source: Internet
Author: User

-- Sort case c_id when '15' then 1 else 2 end by case
Select Top 4 C. * From m3_channel c Where isnull (C. c_upload_mediaserver, '-1') <> '0'
Order by case c_id when '15' then 1 when '14' then 2 when '17' then 3 when '16' then 4 else 5 end

-- Use charindex to sort all specified
Select * From m3_channel where isnull (c_upload_mediaserver, '-1') <> '0'
Order by charindex (',' + Cast (c_id as varchar (20) + ', 14,16, 13,15, 18,17 ,')
-- The IDs of c_id in sorted by charindex are specified.
Select Top 4 * From m3_channel where isnull (c_upload_mediaserver, '-1') <> '0' and c_id in (16,15, 14,13)
Order by charindex (',' + Cast (c_id as varchar (20) + ', 14,16, 13,15 ,')

-- SQL Variables
Declare @ sortkey varchar (300)
Set @ sortkey = 'Live video Channel 4, live video channel 3, live video Channel 6, live video channel 5'
Select Top 4 C. * From m3_channel c Where isnull (C. c_upload_mediaserver, '-1') <> '0'
Order by charindex (c_name, @ sortkey)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.