SQLTeam.com | Weblogs | Forums

How to loop thru the alphabet

I have this sql:

SELECT avg(datediff(day,dtRequestDate,dtDeliveryDate))/30.5 from orders
where substring(strName,1,1) = ?

As written, I enter a letter of the alphabet and I get a result. Instead I would like to have it create a table with a result for each letter of alphabet.

SELECT substring(strName, 1, 1) AS first_letter,
    avg(datediff(day,dtRequestDate,dtDeliveryDate))/30.5 AS avg_days
into dbo.new_table_name
from orders
group by substring(strName, 1, 1)

Thanks. That looks great. I took out the creation of the new table (don't have write access), but it gives me what I need.

Sorry, I based the output on your original request as I interpreted it:

1 Like

Yes, indeed. I shouldn't have said "table" when I only wanted to see the grid. Thanks again.