Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi ,

i need a sql query for the below req i have a solution but it is not efficient

what i need is

i need to highlight all the alphabets(A-Z) in the row, if there is a corresponding
element starting with that letter in the database.

currently i am using something like this

select top 1 [col] from aspnet_Membership where col like 'a%'

and i am repeating this for 26 times and then finding that there is an element for that letter and then highlighting that alphabet.

If there is an efficient way to do the query ,do reply.
Posted

Here's something that should do the trick in a single query:
SQL
SELECT Upper(SubString(csUriQuery, 1, 1)) as Letter, COUNT(*) as Number
FROM WebLogs
WHERE  ASCII(Upper(SubString(csUriQuery, 1, 1))) >= 65 AND ASCII(Upper(SubString(csUriQuery, 1, 1))) <= 90
GROUP BY Upper(SubString(csUriQuery, 1, 1))
ORDER BY Letter


If you're not worried about filtering out non-letter characters (i.e. 1, $, etc.) you should probably remove the WHERE clause.

You also don't really need the count in the select, except for verification.

Then, you could check if the letter is in the results, and you'll only hit the DB once.

If you would rather return a single string of characters that "exist" in the DB, You could probably modify it to return a single concatenated string of the letters, and use a Contains function in your code.

But this should get you where you want to go.

Happy coding.
 
Share this answer
 
Comments
santoshamrutha 10-Jun-11 16:46pm    
Thank you for your response but i just need one record for one alphabet in the table, so i increase the efficiency of the query
santoshamrutha 10-Jun-11 16:54pm    
i placed distinct and worked for me ,Thanks again
hello,

I'm not sure if this is what you looking for. But I gave it a try anyway. You can store the letter from a-z in a table/temporary/variable table. Then write a SELECT statement to select the column where the first letter is in the list of letter.

SQL
DECLARE @temp table  (
    letter char(1)
)
INSERT INTO @temp SELECT 'a'
INSERT INTO @temp SELECT 'b'
INSERT INTO @temp SELECT 'c'
INSERT INTO @temp SELECT 'd'
INSERT INTO @temp SELECT 'e'
INSERT INTO @temp SELECT 'f'
INSERT INTO @temp SELECT 'g'
INSERT INTO @temp SELECT 'h'
INSERT INTO @temp SELECT 'i'
INSERT INTO @temp SELECT 'j'
INSERT INTO @temp SELECT 'k'
INSERT INTO @temp SELECT 'l'
INSERT INTO @temp SELECT 'm'
INSERT INTO @temp SELECT 'n'
INSERT INTO @temp SELECT 'o'
INSERT INTO @temp SELECT 'p'
INSERT INTO @temp SELECT 'q'
INSERT INTO @temp SELECT 'r'
INSERT INTO @temp SELECT 's'
INSERT INTO @temp SELECT 't'
INSERT INTO @temp SELECT 'u'
INSERT INTO @temp SELECT 'v'
INSERT INTO @temp SELECT 'w'
INSERT INTO @temp SELECT 'x'
INSERT INTO @temp SELECT 'y'
INSERT INTO @temp SELECT 'z'

SELECT TOP 1 [col] FROM aspnet_Membership 
 WHERE SUBSTRING([col], 1, 1) in (SELECT letter FROM @temp)
 
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