2014-11-08 2 views
1

데이터 형식이 MI:SS 인 열이 있습니다. 나는 TIME 데이터 유형이 변환해야하지만, 나는 다음과 같은 오류가 TIME으로 캐스팅하려고 할 때 :포스트그레스 - 몇 분 및 초로 varchar를 시간에 캐스트하는 방법은 무엇입니까?

select column_time::time 
그것은 HH의 형식 기대하고있는 것 같다

date/time field value out of range: "29:51"

: MI를, 그래서 29입니다 범위를 벗어나 문제를 일으켰습니다. 이 문제를 어떻게 해결할 수 있습니까?

편집 :이 작품을 찾았지만, 거기에 없었던 것으로 생각되는 몇 시간 동안은 앞서가는 00이 있습니다.

TO_TIMESTAMP(column_time, 'MI:SS')::time 

답변

0

'00:'을 붙이는 시도 :

select cast(concat('00:', column_time) as time) 
+0

, 컬럼의 데이터가 지금, 나는 최고의 00 년대없이이 작업을했다하기 전에, 난 그냥 평균을하고 있었다 믿는 것을 알고있다 "0시 29분 51초"로 표시 (CAST (column_time AS 시간)) 전에. 다른 아이디어? 필요한 경우 column_time 유형을 varchar가 아닌 다른 것으로 변환 할 수 있습니다. –

+1

@MajorMajor 24 분 미만의 분 값을 가졌기 때문에 이전에 작동했던 것 같아서 작동하는 것처럼 보였습니다. –

1

캐스트와 함수 호출 to_timestamp() 사이에 미묘한 차이가 - 심지어 하나가 작동 할 경우.

to_timestamp and to_date exist to handle input formats that cannot be converted by simple casting. These functions interpret input liberally, with minimal error checking. While they produce valid output, the conversion can yield unexpected results. For example, input to these functions is not restricted by normal ranges, thus to_date('20096040','YYYYMMDD') returns 2014-01-17 rather than causing an error. Casting does not have this behavior.

하나 다른

Per documentation:

는 당신이 필요로 할 수있다.

SELECT to_timestamp('129:51', 'MI:SS')::time AS col_overtime 

02:09:51 

을하지만이되지 않습니다 :
이 작동

SELECT cast('00:' || '129:51' AS time) AS col_overtime 

를 그리고 단순히 '29:51'::time을 캐스팅 할 때 최소한의 형식은 HH24:MI하지 MI:SS를 보낸다. 작동

SQL Fiddle.

관련 문제