问题
I have a data set that pulls back visit time in hour/min format (e.g. - 12:33PM would be 1233). I am trying to use the DATEPART
function but it keeps giving me a syntax error stating that :
"expecting something between '(' and the 'HOUR' keyword.
The section below is the piece of the SQL that is giving me errors:
CASE WHEN DATEPART(HOUR , VSL.VISIT_TIME) > 12 THEN DATEPART (HOUR, VSL.VISIT_TIME) -12 ELSE DATEPART (HOUR, VSL.VISIT_TIME) END AS THE_TIME
I am trying to just get the hour part of the time.
回答1:
Doing this through a series of casts, you could do something like:
SELECT CAST(CAST(CAST(CAST((1420 * 100 (FORMAT '99:99:99')) AS CHAR(8)) AS time ) AS TIME FORMAT 'HH:MIBT') AS CHAR(8))
Or you could use some math to sort it out and cast at the end:
SELECT
1420 AS intTime,
FLOOR(intTime/100) AS intHours,
intTime - (intHours * 100) AS intMinutes,
intHours * 60 + intMinutes AS totalIntMinutes,
TIME '00:00:00' + (totalIntMinutes * INTERVAL '1' MINUTE) AS totalTime,
CAST(CAST(totalTime AS TIME FORMAT 'HH:MIBT') AS CHAR(8)) AS char12HrTime
Or just parse and math the thing:
SELECT
1420 AS intTime,
Trim(CASE WHEN FLOOR(intTime/100) > 12 then FLOOR(intTime/100) - 12 ELSE FLOOR(intTime/100) END) || ':' || TRIM(intTime - (FLOOR(intTime/100) * 100))
There's probably a few other ways to cut this up, perhaps a little more elegantly.
Either way you cut it this is going to be a little ugly since you are storing time as something other than a time
type.
来源:https://stackoverflow.com/questions/51200255/convert-military-time-to-standard-time-error