I have 2 fields I’m working with that are stored as smallint military structured times.
Edit I’m running on IBM Informix Dynamic Server Version 10.00.FC9
beg_tm and end_tm
Sample values
beg_tm 545
end_tm 815
beg_tm 1245
end_tm 1330
Sample output
beg_tm 5:45 am
end_tm 8:15 am
beg_tm 12:45 pm
end_tm 1:30 pm
I had this working in Perl, but I’m looking for a way to do it with SQL and case statements.
Is this even possible?
EDIT
Essentially, this formatting has to be used in an ACE report. I couldn’t find a way to format it within the output section using simple blocks of
if(beg_tm>=1300) then
beg_tm = vbeg_tm - 1200
Where vbeg_tm is a declared char(4) variable
EDIT
This works for hours >=1300 (EXCEPT FOR 2230 !!)
select substr((beg_tm-1200),0,1)||":"||substr((beg_tm-1200),2,2) from mtg_rec where beg_tm>=1300;
This works for hours < 1200 (sometimes…. 10:40 is failing)
select substr((mtg_rec.beg_tm),0,(length(cast(beg_tm as varchar(4)))-2))||":"||(substr((mtg_rec.beg_tm),2,2))||" am" beg_tm from mtg_rec where mtg_no = 1;
EDIT
Variation of casting syntax used in Jonathan Leffler’s expression approach
SELECT beg_tm,
cast((MOD(beg_tm/100 + 11, 12) + 1) as VARCHAR(2)) || ':' ||
SUBSTRING(cast((MOD(beg_tm, 100) + 100) as CHAR(3)) FROM 2) ||
SUBSTRING(' am pm' FROM (MOD(cast((beg_tm/1200) as INT), 2) * 3) + 1 FOR 3),
end_tm,
cast((MOD(end_tm/100 + 11, 12) + 1) as VARCHAR(2)) || ':' ||
SUBSTRING(cast((MOD(end_tm, 100) + 100) as CHAR(3)) FROM 2) ||
SUBSTRING(' am pm' FROM (MOD(cast((end_tm/1200) as INT), 2) * 3) + 1 FOR 3)
FROM mtg_rec
where mtg_no = 39;
Please note that there is useful information at SO 440061 about converting between 12 hour and 24 hour notations for time (the opposite of this conversion); it isn’t trivial, because 12:45 am comes half an hour before 1:15 am.
Next, please note that Informix (IDS — Informix Dynamic Server) version 7.31 finally reached its end of service on 2009-09-30; it is no longer a supported product.
You should be more precise with your version number; there are considerable differences between 7.30.UC1 and 7.31.UD8, for instance.
However, you should be able to use the TO_CHAR() function to format times as you need. Although this reference is to the IDS 12.10 Information Center, I believe that you will be able to use it in 7.31 (not necessarily in 7.30, but you should not have been using that for most of the last decade).
There is a ‘%R’ format specifier for 24-hour time, it says. It also refers you to ‘GL_DATETIME‘, where it says ‘%I’ gives you the 12-hour time and ‘%p’ gives you the am/pm indicator. I also found a 7.31.UD8 instance of IDS to validate this:
I see from re-reading the question that you actually have SMALLINT values in the range 0000..2359 and need to get those converted. Often, I’d point out that Informix has a type for storing such values – DATETIME HOUR TO MINUTE – but I concede it occupies 3 bytes on disk instead of just 2, so it isn’t as compact as a SMALLINT notation.
Steve Kass showed the SQL Server notation:
The trick for getting the hour correct is neat – thanks Steve!
Translated into Informix for IDS 11.50, assuming that the table is:
The SUBSTRING notation using FROM and FOR is standard SQL notation – weird, but so.
Example results:
Caution: the values 559-601 are in the list because I ran into a problem with rounding instead of truncation in the absence of the cast to integer.
Now, this was tested on IDS 11.50; IDS 7.3x won’t have the cast notation.
However, that isn’t a problem; the next comment was going to deal with that…
As an exercise in how to write the expression in SQL without conditionals, etc, this is interesting, but if anyone wrote that more than once in an entire suite, I’d shoot them for lack of modularization. Clearly, this requires a stored procedure – and a stored procedure doesn’t need the (explicit) casts or some of the other trickery, though the assignments enforce implicit casts:
The Informix ‘[2,3]’ notation is a primitive form of sub-string operator; primitive because (for reasons that still elude me) the subscripts must be literal integers (not variables, not expressions). It happens to work usefully here; in general, it is frustrating.
This stored procedure should work on any version of Informix (OnLine 5.x, SE 7.x, IDS 7.x or 9.x, 10.00, 11.x, 12.x) that you can lay hands on.
To illustrate the equivalence of (a minor variant on) the expression and the stored procedure:
Which produces the result:
This stored procedure can now be used multiple times in a single SELECT statement inside your ACE report without further ado.
[After comments from the original poster about not working…]
IDS 7.31 doesn’t handle non-integer values passed to the MOD() function. Consequently, the divisions have to be stored in an explicit integer variable – thus:
This was tested on IDS 7.31.UD8 on Solaris 10 and worked correctly. I don’t understand the syntax error reported; but there is an outside chance of there being a version dependency – it is always crucial to report version numbers and platforms just in case. Notice that I’m careful to document where various things worked; that isn’t an accident, nor is it just fussiness — it is based on many years of experience.