需要帮助将 Sybase 中的字符串转换为日期格式

发布于 2024-09-06 12:53:18 字数 546 浏览 4 评论 0原文

我正在尝试将两种类型的字符串转换为日期格式,但是无法执行其中任何一个。

有问题的输入字符串和预期输出如下:


输入 1: 20100614191522

预期输出 1: 6/14/2010 7:15:22 PM


输入 2: 2010/12

预期输出 2: 12/1/2010 12:00:00 AM


我尝试过,

select convert(datetime,'20100614191522',109) 

我尝试使用“转换”功能使用不同的样式参数。但我总是收到以下错误。 将 VARCHAR 值“20100614191522”显式转换为 DATETIME 字段期间出现语法错误。 Msg: 249,Level: 16,State: 1

你能帮我一下,如何实现同样的效果吗?

提前致谢。

I am trying to convert two types of string into date format, however not able to do either of them.

The problematic input strings with expected output are as follows:


Input 1: 20100614191522

Expected output 1: 6/14/2010 7:15:22 PM


Input 2: 2010/12

Expected output 2: 12/1/2010 12:00:00 AM


I tried like ,

select convert(datetime,'20100614191522',109) 

I tried with different style parameters with "convert" function . But I always get following errors.
Syntax error during explicit conversion of VARCHAR value '20100614191522' to a DATETIME field.
Msg: 249, Level: 16, State: 1

Can you please help me out, how to achieve the same.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2024-09-13 12:53:18

您尝试过的样式参数用于将日期时间数据转换为字符类型。这里你要求相反的转换。

输入 1

declare @str varchar(20)
set @str = '20100614191522'
select convert(datetime,substring(@str, 5, 2 ) + '/' + substring(@str, 7, 2 ) + '/' + substring(@str, 1, 4 )  + ' ' + substring(@str, 9, 2 )  + ':' + substring(@str, 11, 2 ) + ':' + substring(@str, 13, 2 ) )

给出

Jun 14 2010  7:15PM

输入 2

declare @str varchar(20)
set @str = '2010/12'
select convert(datetime, @str + '/01')

给出

Dec  1 2010 12:00AM

The style parameters that you have tried is for converting datetime data to a character type. Here you are asking for the opposite convertion.

Input 1

declare @str varchar(20)
set @str = '20100614191522'
select convert(datetime,substring(@str, 5, 2 ) + '/' + substring(@str, 7, 2 ) + '/' + substring(@str, 1, 4 )  + ' ' + substring(@str, 9, 2 )  + ':' + substring(@str, 11, 2 ) + ':' + substring(@str, 13, 2 ) )

gives

Jun 14 2010  7:15PM

Input 2

declare @str varchar(20)
set @str = '2010/12'
select convert(datetime, @str + '/01')

gives

Dec  1 2010 12:00AM
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文