简体   繁体   English

是否使用SQL将datetime列可移植地转换为字符串?

[英]Is there a portable conversion of a datetime column to string using SQL

I have a column defined as a datetime in sybase and have converted it to a string using 我有一个列定义为sybase中的日期时间,并已使用转换为字符串

ISNULL(CAST(CAST(most_recent_trades.asof_date AS DATE) AS VARCHAR(20))

It turns out that the representation of this as a string in sybase differs from my unit test db which is H2. 事实证明,这在sybase中以字符串表示与我的单元测试数据库H2不同。

In sybase
Sep  1 2016

In H2
2016-09-01

The column is defined as datetime in H2 as well. 该列也定义为H2中的日期时间。

Is there a way to convert the datetime column to a string that is the same string representation in both Sybase and H2 (I don't need the time portion of this if that helps any)? 有没有一种方法可以将datetime列转换为在Sybase和H2中都是相同的字符串表示形式的字符串(如果有帮助的话,我不需要它的时间部分)?

EDIT : I don't think there is any way to do this in a portable way with one function, so what I decided to do was use the YEAR, MONTH and DAY functions (they are all available in Sybase and H2), extract the parts and concatenate them to make a date that will look the same when represented as a string in both Sybase and H2. 编辑:我认为没有任何方法可以通过一个函数以可移植的方式执行此操作,因此我决定要做的是使用YEAR,MONTH和DAY函数(它们在Sybase和H2中都可用),提取零件并将它们连接起来以形成一个日期,当在Sybase和H2中用字符串表示时,日期将相同。 This works for me at this point. 这一点对我来说很有效。

我将使用CONVERT()函数的格式121 ,然后获取它的日期部分,如下所示:

substring(convert(varchar(30), most_recent_trades.asof_date, 121), 1, 10)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM