0

I am currently working on a Firebird database which stores slots for a time (like appointments). In the data the timeslot is stored in an Integer format but is not the slot time.

Example: Slot Time: 11am Database Value: 660

The database value is represented as the number of minutes since midnight. So 11am is 660 minutes from midnight. Or 12noon is 720

Question How, in Firebird, can a convert this 660 to display as 1100 (still an integer) or 540 as 900 (basically 9am)?

4

2 回答 2

2

What you have stored into database is minutes since start of the day. So you just divide with 60 to get hours. Keep in mind that in SQL if both arguments of the division are integers you'll get integer as an answer, not decimal! Something like following should work for you (assuming the number of minutes is stored to timeslot field):

SELECT 
 cast(timeslot / 60 as varchar(2)) || 
   case
     when mod(timeslot, 60) < 10 then '0'|| mod(timeslot, 60)
     else mod(timeslot, 60)
   end
FROM t

This should give you 1130 for 11:30 am, not 1150 (11,5 hours).

Also see the DATEADD function.

于 2014-08-21T11:11:50.350 回答
2

First off I'd store it in the database as a TIME column instead of a number of minutes. Note that on a technical level Firebird stores TIME as the number of 100 microseconds since midnight. However if you really want to store as number of minutes, then you can use:

From minutes to TIME using DATEADD:

DATEADD(x MINUTE TO TIME'00:00')

Time to minutes using DATEDIFF:

DATEDIFF(MINUTE FROM TIME'00:00' TO y)

So:

SELECT 
  DATEADD(660 MINUTE TO TIME'00:00'), 
  DATEDIFF(MINUTE FROM TIME'00:00' TO TIME'11:00') 
FROM RDB$DATABASE

Will return: 11:00:00.000, 660

于 2014-08-21T11:57:50.950 回答