Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. SQL FUNCTION USING DATE TIME

SQL FUNCTION USING DATE TIME

Scheduled Pinned Locked Moved Database
databasehelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Thanusree Duth
    wrote on last edited by
    #1

    FIRSTTIMEIN LASTTIMEOUT 7:10:09:000PM 9:40:45:000PM 10:20:32:000PM 12:15:27:000AM From this,i will have to get only 7:10:09:000PM as FIRSTTIMEIN and 12:15:27:000AM as LASTTIMEOUT.I used min and max function.but it was not possible.help me

    J 1 Reply Last reply
    0
    • T Thanusree Duth

      FIRSTTIMEIN LASTTIMEOUT 7:10:09:000PM 9:40:45:000PM 10:20:32:000PM 12:15:27:000AM From this,i will have to get only 7:10:09:000PM as FIRSTTIMEIN and 12:15:27:000AM as LASTTIMEOUT.I used min and max function.but it was not possible.help me

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      MIN and MAX are indeed the right functions to use, however, for them to work properly the data must be stored in a DATETIME field, because using strings to store those times will obviously yield string-based results for min & max. There is no way to calculate the min and max times from strings representing dates. Secondly, in order ot know that 12:15am is after 7:10pm you must also store the date date part along with the time part. Here is a simple test script that shows the process (and returns the results you want). You'll notice ive used yesterdays date for the 3 values before midnight, and today's date for the one after midnight.

      WITH data(firstTimeIn,lastTimeOut )
      AS
      (
      SELECT CAST('4 july 2010 7:10:09:000PM' AS DATETIME),CAST('4 july 2010 9:40:45:000PM' AS DATETIME)
      UNION
      SELECT CAST('4 july 2010 10:20:32:000PM' AS DATETIME),CAST('5 july 2010 12:15:27:000AM' AS DATETIME)
      )
      SELECT
      MIN(firstTimeIn) as firstTimeIn,
      MAX(lastTimeOut) as lastTimeOut
      FROM data

      Result: firstTimeIn: 2010-07-04 19:10:09.000 lastTimeOut: 2010-07-05 00:15:27.000

      J 1 Reply Last reply
      0
      • J J4amieC

        MIN and MAX are indeed the right functions to use, however, for them to work properly the data must be stored in a DATETIME field, because using strings to store those times will obviously yield string-based results for min & max. There is no way to calculate the min and max times from strings representing dates. Secondly, in order ot know that 12:15am is after 7:10pm you must also store the date date part along with the time part. Here is a simple test script that shows the process (and returns the results you want). You'll notice ive used yesterdays date for the 3 values before midnight, and today's date for the one after midnight.

        WITH data(firstTimeIn,lastTimeOut )
        AS
        (
        SELECT CAST('4 july 2010 7:10:09:000PM' AS DATETIME),CAST('4 july 2010 9:40:45:000PM' AS DATETIME)
        UNION
        SELECT CAST('4 july 2010 10:20:32:000PM' AS DATETIME),CAST('5 july 2010 12:15:27:000AM' AS DATETIME)
        )
        SELECT
        MIN(firstTimeIn) as firstTimeIn,
        MAX(lastTimeOut) as lastTimeOut
        FROM data

        Result: firstTimeIn: 2010-07-04 19:10:09.000 lastTimeOut: 2010-07-05 00:15:27.000

        J Offline
        J Offline
        JasonShort
        wrote on last edited by
        #3

        If at all possible don't use that string format. It will break in other locales. '2010-07-04 22:20:32:00' is the better format. It is always parsed correctly no matter what the locale.

        Jason S Short, Ph.D. VistaDB Software, Inc.

        J 1 Reply Last reply
        0
        • J JasonShort

          If at all possible don't use that string format. It will break in other locales. '2010-07-04 22:20:32:00' is the better format. It is always parsed correctly no matter what the locale.

          Jason S Short, Ph.D. VistaDB Software, Inc.

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          JasonShort wrote:

          It is always parsed correctly no matter what the locale.

          You don't say! :rolleyes: I was providing test data for the OP, which incidentally will parse in all locales as the month names are fully qualified.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups