SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    How to compare between mysql datetime field DateStart and DateEnd?

    Hello guys!
    I need your help.

    I have two fields date in my table MYSQL:
    1. Field DateStart 2012-05-01 12:00:00;
    2. Field DateEnd 2012-05-05 12:00:00

    I need extract in output the greater of the two dates, in this example 2012-05-05 12:00:00.

    Thank you for the suggestion.

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    SELECT IF (DateStart > DateEnd, DateStart, DateEnd) AS GreaterDate FROM TABLE

  3. #3
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cpradio View Post
    SELECT IF (DateStart > DateEnd, DateStart, DateEnd) AS GreaterDate FROM TABLE
    thanks a lot!
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,274
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    SELECT GREATEST(DateStart,DateEnd) AS GreaterDate FROM daTable

    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,162
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    SELECT GREATEST(DateStart,DateEnd) AS GreaterDate FROM daTable

    I just learned something new. Didn't realize that existed in MySQL.

  6. #6
    SitePoint Evangelist cms9651's Avatar
    Join Date
    Mar 2010
    Posts
    407
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Great, thanks a lot!
    Code:
    mysql> SELECT
    	GREATEST(DateStart, DateEnd) AS GreaterDate
    FROM
    	tbl_dates;
    +---------------------+
    | GreaterDate         |
    +---------------------+
    | 2012-05-05 12:00:00 |
    +---------------------+
    1 row in set
    Thanks you very much for your help
    I'm really happy for your quickly answer.
    Good bye


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •