Sybase SQL: 将字符串转换为日期和时间
Sybase SQL: Convert String to Date and Time
Sybase SQL 将字符串转换为 DateTime 帮助:
我将日期和时间以字符串格式 ('yyyymmddhhnnss') 存储在 table 中。我需要 select 它们的日期和时间格式。
例如:
字符串值:'20141228092818'
预计 return:'2014 年 12 月 28 日 09:28:18 上午'
字符串值:'20141121132810'
预计 return:'2014 年 11 月 21 日 01:28:10 下午'
感谢帮助...
您指定的输出格式与任何 Sybase 标准样式都不完全相同(请参阅 Sybase documentation for the convert function)。
虽然和109风格很接近,只是日月颠倒了,时间没有前导零。要进行此转换,您需要将字符串转换为 Sybase 可以理解的格式,并且可以转换为日期时间(例如 yyyymmdd hh:mm:ss),将其转换为日期时间,然后使用109风格。
示例:
create table #test (test varchar(255))
insert #test select '20141228092818'
insert #test select '20141121132810'
select
convert(varchar(255),
convert(datetime,
left(test, 8) + ' ' +
substring(test, 9, 2) + ':' +
substring(test, 11, 2) + ':' +
substring(test, 13, 2)
),
109)
from #test
输出:
Dec 28 2014 9:28:18:000AM
Nov 21 2014 1:28:10:000PM
谢谢托比。成功了。
SELECT CONVERT(datetime,SUBSTRING(MyField, 5, 2 ) + '/' + SUBSTRING(MyField, 7, 2) + '/' + SUBSTRING(MyField, 1, 4 ) + ' ' + SUBSTRING(MyField, 9, 2 ) + ':' + SUBSTRING(MyField, 11, 2 ) + ':' + SUBSTRING(MyField, 13, 2 )) TransactDateTime
2014 年 12 月 28 日 09:28:18 上午
2014 年 11 月 21 日 01:28:10 下午
Sybase SQL 将字符串转换为 DateTime 帮助:
我将日期和时间以字符串格式 ('yyyymmddhhnnss') 存储在 table 中。我需要 select 它们的日期和时间格式。
例如: 字符串值:'20141228092818' 预计 return:'2014 年 12 月 28 日 09:28:18 上午'
字符串值:'20141121132810' 预计 return:'2014 年 11 月 21 日 01:28:10 下午'
感谢帮助...
您指定的输出格式与任何 Sybase 标准样式都不完全相同(请参阅 Sybase documentation for the convert function)。
虽然和109风格很接近,只是日月颠倒了,时间没有前导零。要进行此转换,您需要将字符串转换为 Sybase 可以理解的格式,并且可以转换为日期时间(例如 yyyymmdd hh:mm:ss),将其转换为日期时间,然后使用109风格。
示例:
create table #test (test varchar(255))
insert #test select '20141228092818'
insert #test select '20141121132810'
select
convert(varchar(255),
convert(datetime,
left(test, 8) + ' ' +
substring(test, 9, 2) + ':' +
substring(test, 11, 2) + ':' +
substring(test, 13, 2)
),
109)
from #test
输出:
Dec 28 2014 9:28:18:000AM
Nov 21 2014 1:28:10:000PM
谢谢托比。成功了。
SELECT CONVERT(datetime,SUBSTRING(MyField, 5, 2 ) + '/' + SUBSTRING(MyField, 7, 2) + '/' + SUBSTRING(MyField, 1, 4 ) + ' ' + SUBSTRING(MyField, 9, 2 ) + ':' + SUBSTRING(MyField, 11, 2 ) + ':' + SUBSTRING(MyField, 13, 2 )) TransactDateTime
2014 年 12 月 28 日 09:28:18 上午
2014 年 11 月 21 日 01:28:10 下午