Click here to Skip to main content
15,881,832 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

how to display selected litter first in sql server table

Please help!!!!!
Advance thanks
shakeer.

What I have tried:

query to display start with first litter (seleted letter for Example: F)
Posted
Updated 21-Jul-16 2:54am
Comments
F-ES Sitecore 21-Jul-16 8:48am    
In addition to the solution below you could try UNION and UNION the results where the letter starts F with those where it doesn't. You'd need to try both methods and see which one performs better as it might depend on your data.

That's difficult - it's easy to display just the rows that begin with F:
SQL
SELECT * FROM MyTable WHERE MyColumn LIKE 'F%'

But ordering is more complex, because SQL doesn't have particularly good string handling facilities.
You can do it, but it's a cludge:
SQL
SELECT * From MyTable
ORDER BY CASE WHEN SUBSTRING(MyColumn , 1 , 1) = 'F' THEN 1 ELSE 2 END, MyColumn 
 
Share this answer
 
Comments
Richard Deeming 21-Jul-16 9:40am    
It would probably be better to use:
ORDER BY CASE WHEN MyColumn Like 'F%' THEN 1 ELSE 2 END, MyColumn

That way, the query can use an index on the column, if there is one.
OriginalGriff 21-Jul-16 9:52am    
Good thought! :thumbsup:
There are multiple option for it, either you can use LIKE as suggested by OriginalGriff or you can use LEFT function with CASE operator, see below snippet
SQL
set @checkWord= 'false' when LEFT(@checkWord, 1) = 'F' then 1 else 0 end 

OR use CharIndex as below
SQL
set (CharIndex('f', 'false') = 1))

hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900