0

我已经研究了 Cast and Convert,但我找不到这样做的方法。我需要将四位数字转换为小时格式。例如,0800 将变为 8:00 或 1530 将变为 15:30。我不能使用函数,我使用的是 InterSystem 的 CacheSQL。有什么建议么?提前致谢!

编辑:如果更方便的话,我可以将四位数字除以 100 以从原始 1500 获得 15 或从 0830 获得 8.30 之类的值。这是否使转换为小时:分钟格式更容易?

4

4 回答 4

2

对于 CacheSQL,您可以这样做:

SELECT {fn TRIM(LEADING '0' FROM LEFT(col_name, 2) || ':' || RIGHT(col_name, 2)) }
FROM table_name
于 2012-08-29T22:00:54.487 回答
0

Well, if it is something like Oracle you might have a try with the to_date() function.

Read more here.

Example:

SELECT to_date(yourColumn, 'HH24MI') FROM ...

EDIT (why? see comments): If necessary (I'm actually not familiar with Oracle) you can wrap another function like TIME() around it.

SELECT TIME(to_date(yourColumn, 'HH24MI')) FROM ...

Read more about TIME() here.

</EDIT>

In MySQL the equivalent would be the STR_TO_DATE() function:

SELECT STR_TO_DATE(yourColumn, '%H%i') FROM ...

Read about STR_TO_DATE() and its parameters under the DATE_FORMAT() function.

于 2012-08-29T21:39:20.823 回答
0

In SQL Server 2008, given data that looks like

create table #data
(
  HHMM int not null ,
)
insert #data values ( 0800 )
insert #data values ( 0815 )
insert #data values ( 1037 )
insert #data values ( 2359 )

You can say:

select * ,
       strTime = right( '0' + convert(varchar, HHMM / 100 ) , 2 )
               + ':'
               + right( '0' + convert(varchar, HHMM % 100 ) , 2 ) ,
       myTime  = convert(time ,
                   right( '0' + convert(varchar, HHMM / 100 ) , 2 )
                 + ':'
                 + right( '0' + convert(varchar, HHMM % 100 ) , 2 ) ,
                 120
                 )
from #data

Other SQL implementations likely have similar functionality.

In earlier versions of SQL Server that lack the time datatype, just use datetime, thus:

select * ,
       strTime = right( '0' + convert(varchar, HHMM / 100 ) , 2 )
               + ':'
               + right( '0' + convert(varchar, HHMM % 100 ) , 2 ) ,
       myTime  = convert(datetime,
                   right( '0' + convert(varchar, HHMM / 100 ) , 2 )
                 + ':'
                 + right( '0' + convert(varchar, HHMM % 100 ) , 2 ) ,
                 120
                 )
from #data

You'll get a datetime value that is 1 Jan 1900 with the desired time-of-day.

于 2012-08-29T21:40:00.067 回答
0
left( case when (EndTime / 100) < 10 then  ('0'+ convert(varchar, EndTime / 100 )) else convert(varchar, EndTime / 100 )  end, 2 )
                 + ':'
                 + right( '0' + convert(varchar, EndTime % 100 ) , 2 )
于 2020-03-19T13:57:04.373 回答