删除 CQL 中时间数据类型的尾随零

Remove trailing zeroes of time datatype in CQL

正在尝试删除 arrival_time 的尾随零,列数据类型为 Time

SELECT * FROM TABLE

我得到了这个:

station_name | arrival_time --------------+-------------------- Wellington | 06:05:00.000000000

我需要这样的结果:

station_name | arrival_time --------------+-------------------- Wellington | 06:05:00

我是 CQL 的新手,在此先感谢。

select station_name, SUBSTRING( Convert(Varchar(20),arrival_time), 0, 9)  As arrival_time
from [Table]

按照table和数据格式

使用
CREATE TABLE [dbo].[ArrivalStation](
    [station_name] [varchar](500) NULL,
    [arrival_time] [Time](7) NULL
) ON [PRIMARY]


INSERT [dbo].[ArrivalStation] ([station_name], [arrival_time]) VALUES (N'Wellington ', N'06:05:00.0000000')
INSERT [dbo].[ArrivalStation] ([station_name], [arrival_time]) VALUES (N'Singapore', N'12:35:29.1234567')

所以你实际上不能在 Cassandra 中使用 time 类型来做到这一点。但是,您可以使用 timestamp.

cassdba@cqlsh:Whosebug> CREATE TABLE arrival_time2 (station_name TEXT PRIMARY KEY, 
    arrival_time time, arrival_timestamp timestamp);
cassdba@cqlsh:Whosebug> INSERT INTO arrival_time2 (station_name , arrival_time , arrival_timestamp)
    VALUES ('Wellington','06:05:00','2018-03-22 06:05:00');
cassdba@cqlsh:Whosebug> SELECT * FROM arrival_time2;

 station_name | arrival_time       | arrival_timestamp
--------------+--------------------+---------------------------------
   Wellington | 06:05:00.000000000 | 2018-03-22 11:05:00.000000+0000

(1 rows)

当然,这也不是您想要的,真的。所以接下来你需要在 ~/.cassandra/cqlshrc.

[ui] 部分设置一个 time_format
[ui]
time_format = %Y-%m-%d %H:%M:%S

重新启动 cqlsh,这应该可以工作:

Connected to Test Cluster at 127.0.0.1:9042.
[cqlsh 5.0.1 | Cassandra 3.11.2 | CQL spec 3.4.4 | Native protocol v4]
Use HELP for help.
cassdba@cqlsh> SELECT station_name,arrival_timestamp
    FROm Whosebug.arrival_time2 ;

 station_name | arrival_timestamp
--------------+---------------------
 Wellington | 2018-03-22 11:05:00

(1 rows)