Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
SQL select first letter of results
Old 10-03-2011, 09:30 AM SQL select first letter of results
Skilled Talker

Posts: 50
Trades: 0
I have the following SQL statement that retrieves the distinct first letter for each entry in the database. I'm using this to build a alphabetized list for a glossary.

PHP Code:
SELECT DISTINCT LEFT(art_title,1) AS first_letter FROM articles ORDER BY first_letter ASC 
The problem I'm having is when the first letter in the string is a quote, single quote or some other character.

Is there a way in the SQL statement to replace any unwanted characters.

Cheers

Wayne.
HullBorn is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-03-2011, 09:52 AM Re: SQL select first letter of results
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Using what database server?

considering that this is the PHP forum, SQL is NOT PHP and PHP can "talk" to several SQL Servers.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-03-2011, 10:07 AM Re: SQL select first letter of results
Skilled Talker

Posts: 50
Trades: 0
MySQL. Apologies for posting in the wrong forum. Could you move it to the appropriate forum.
HullBorn is offline
Reply With Quote
View Public Profile
 
Old 10-03-2011, 11:05 AM Re: SQL select first letter of results
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
Regular Expressions.

Use a pattern of ^[a-zA-Z] to match lines that start letters only, ^[0-9] to match numerics and "NOT ^[a-zA-Z0-9]"

The ^ means "Starts with" so you don't need the string slicing.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-03-2011, 11:46 AM Re: SQL select first letter of results
Skilled Talker

Posts: 50
Trades: 0
Thanks Chris,

This seems to work.

PHP Code:
SELECT DISTINCT LEFT(art_title,1) AS first_letter FROM articles WHERE art_title REGEXP '^[a-zA-Z]' ORDER BY first_letter ASC 
Out of interest, if there was 1 article titled "Zulu............." including the quotes, the letter Z would not be in the results as it doesn't start with a letter.

Can regular expressions disregard the first character if it's not a letter? so that the Z will appear in the results?

Cheers

Wayne.
HullBorn is offline
Reply With Quote
View Public Profile
 
Old 10-03-2011, 12:43 PM Re: SQL select first letter of results
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
to match a quote mark followed by a character the pattern would be

^[\'\"]+[A-Za-z0-9]

that says "a string that starts with ' or " followed by an alphanumeric character.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 10-04-2011, 04:10 AM Re: SQL select first letter of results
Skilled Talker

Posts: 50
Trades: 0
Cheers Chris
HullBorn is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to SQL select first letter of results
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51195 seconds with 12 queries