MySQL Syntax for Limit Characters

How do I alter the following SQL Syntact to limit only 25 characters being returned?

SELECT distinct program FROM course_Finder ORDER BY program ASC
pda4meAsked:
Who is Participating?
 
nizsmoDeveloperCommented:
You can limit the characters to 25 max like this (in PHP):
<select name="program" class="search">
         <option value="">-- sort by --</option>
         <?php
do {  
?>
         <option value="<?php echo $row_Program['program']; ?>"><?php echo substr($row_Program['program'],0,25); ?></option>
         <?php
} while ($row_Program = mysql_fetch_assoc($Program));
  $rows = mysql_num_rows($Program);
  if($rows > 0) {
      mysql_data_seek($Program, 0);
        $row_Program = mysql_fetch_assoc($Program);
  }
?>
            </select>
            
            

Open in new window

0
 
steelseth12Commented:
SELECT distinct program FROM course_Finder ORDER BY program ASC LIMIT 0,25
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you mean 25 characters of the column program?

SELECT distinct substr(program,1,25) FROM course_Finder ORDER BY substr(program,1,25) ASC
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
dis1931Commented:
Hello pda4me,

Try:
SELECT distinct LEFT(program,25) FROM course_Finder ORDER BY program ASC

Regards,

dis1931
0
 
GawaiCommented:
you can use a keyword LIMIT
0
 
pda4meAuthor Commented:
None of those seem to be doing the trick?  Here is the full code, what am I doing wrong?

mysql_select_db($database_memLook, $memLook);
$query_Program = "SELECT distinct program FROM course_Finder ORDER BY program ASC";
$Program = mysql_query($query_Program, $memLook) or die(mysql_error());
$row_Program = mysql_fetch_assoc($Program);
$totalRows_Program = mysql_num_rows($Program);
0
 
pda4meAuthor Commented:
I am using this to populate a select dropdown form element...is it possible to just limit the characters on the form element?

<select name="program" class="search">
         <option value="">-- sort by --</option>
         <?php
do {  
?>
         <option value="<?php echo $row_Program['program']?>"><?php echo $row_Program['program']?></option>
         <?php
} while ($row_Program = mysql_fetch_assoc($Program));
  $rows = mysql_num_rows($Program);
  if($rows > 0) {
      mysql_data_seek($Program, 0);
        $row_Program = mysql_fetch_assoc($Program);
  }
?>
            </select>
0
 
nizsmoDeveloperCommented:
I didn't do it for your value attribute of your options tag, since I was unsure if you want to limit the string to 25 characters there. If you do, it is exactly the same procedure.
0
 
pda4meAuthor Commented:
Thanks Nizsmo!
0
 
nizsmoDeveloperCommented:
glad to be of assistance :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.