A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

MySQL String Functions Made Easy – LEFT, SUBSTRING, RIGHT

Here is a quick tutorial:

mysqlsubstring.gif

SELECT
*,
CONCAT(LEFT(DateRegular,4),"-",SUBSTRING(DateRegular,5,2),"-",RIGHT(DateRegular,2)) AS myDateRegularized
FROM
   myCoolTableName
WHERE
   ShortName NOT IN (SELECT employee_shortName FROM employee)
AND
  LENGTH(DateRegular) >= 1
AND
(
   CONCAT(LEFT(DateRegular,4),"-",SUBSTRING(DateRegular,5,2),"-",RIGHT(DateRegular,2)) >= "2007-07-08"
   AND
   CONCAT(LEFT(DateRegular,4),"-",SUBSTRING(DateRegular,5,2),"-",RIGHT(DateRegular,2)) <= "2007-08-31"
)
This entry was posted in General and tagged . Bookmark the permalink.

4 Responses to MySQL String Functions Made Easy – LEFT, SUBSTRING, RIGHT

  1. Idetrorce says:

    very interesting, but I don’t agree with you
    Idetrorce

  2. Peter says:

    Note that there are also shorter versions of function SUBSTRING, that act identically:

    SELECT MID(‘LongStringThatYouLike’, 5, 6) AS crop;
    SELECT SUBSTR(‘LongStringThatYouLike’, 5, 6) AS crop;

    # result: ‘String’

  3. admin says:

    Thanks Peter. MID is definitely shorter :)

Leave a Reply to Idetrorce Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>