[英]Convert four Digit number into hour format SQL
I have looked into Cast and Convert, but I cannot find a way to do this.我已经研究过 Cast and Convert,但我找不到办法做到这一点。 I need to convert four digits into an hour format.
我需要将四位数字转换为小时格式。 For instance, 0800 would become 8:00 or 1530 would become 15:30.
例如,0800 会变成 8:00,或者 1530 会变成 15:30。 I cannot use functions, I'm using a InterSystem's CacheSQL.
我不能使用函数,我使用的是 InterSystem 的 CacheSQL。 Any suggestions?
有什么建议么? Thanks in advance!
提前致谢!
EDIT: If it is any more convenient, I can just divide the four digits by one hundred to get values like 15 from original 1500, or 8.30 from 0830. Does this make converting to hour:minute format easier?编辑:如果更方便的话,我可以将四位数字除以 100 以得到原始 1500 中的 15 或 0830 中的 8.30 之类的值。这是否使转换为小时:分钟格式更容易?
For CacheSQL, you can do this:对于 CacheSQL,您可以这样做:
SELECT {fn TRIM(LEADING '0' FROM LEFT(col_name, 2) || ':' || RIGHT(col_name, 2)) }
FROM table_name
Well, if it is something like Oracle you might have a try with the to_date()
function.好吧,如果它是像 Oracle 这样的东西,您可能会尝试使用
to_date()
函数。
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.编辑(为什么?看评论):如果有必要(我实际上不熟悉 Oracle),您可以在它周围包装另一个函数,如 TIME()。
SELECT TIME(to_date(yourColumn, 'HH24MI')) FROM ...
Read more about TIME() here . 在此处阅读有关TIME() 的更多信息。
</EDIT>
In MySQL the equivalent would be the STR_TO_DATE()
function:在 MySQL 中,等效的是
STR_TO_DATE()
函数:
SELECT STR_TO_DATE(yourColumn, '%H%i') FROM ...
Read about STR_TO_DATE() and its parameters under the DATE_FORMAT() function.阅读 DATE_FORMAT() 函数下的STR_TO_DATE()及其参数。
In SQL Server 2008, given data that looks like在 SQL Server 2008 中,给定的数据看起来像
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.其他 SQL 实现可能具有类似的功能。
In earlier versions of SQL Server that lack the time
datatype, just use datetime
, thus:在缺少
time
数据类型的 SQL Server 的早期版本中,只需使用datetime
,因此:
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.您将获得 1900 年 1 月 1 日的
datetime
值,其中包含所需的时间。
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 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.