homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Databases
Forum Library, Charter, Moderators: physics

Databases Forum

Not sure how to query this?

 12:51 pm on Jul 14, 2010 (gmt 0)

Hello all,
I have a query that is getting info and sorting alphabetically on a text column...no problem.

Now the client wants to sort alphabetically but at the top of the list he wants all rows that contain the string "CD" before the rest of them.

Is there an easy query to do this?




 1:29 pm on Jul 14, 2010 (gmt 0)

Using Sql Server you do this
SELECT 'CD'+<ColumnName> FROM <TableName>

But you need to make sure that the Column is a string datatype.


 1:43 pm on Jul 14, 2010 (gmt 0)

I need to get all columns of data, will this work for that? I need all columns but sorted starting where ColA contains "CD".


 1:55 pm on Jul 14, 2010 (gmt 0)

OK...solved my problem but had to use 2 querys to do it.

First get all that contain "CD", second get all that don't contain "CD":

...WHERE product LIKE '%CD%'...

...WHERE product NOT LIKE '%CD%'...

 2:27 pm on Jul 14, 2010 (gmt 0)

Sorry Completely miss read your original post my suggesting was no help at all


 7:14 pm on Jul 14, 2010 (gmt 0)

You are not going to need two queries. Related to this thread [webmasterworld.com], try an expression.

select * from table order by field like '%CD%' desc, field asc;

You many need to change the like to 'CD%' or even field = 'CD' if it's the total content of the field, or use a regex, but that will do it in one select.


 7:28 pm on Jul 14, 2010 (gmt 0)

thanks, will give that a go!

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Databases
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved