将文本转换为DateTime格式 [英] Convert Text to DateTime Format
问题描述
我的SQL Server 2008表中有一个字段,如下所示:
字段名称:Dt_Tm
格式:nvarchar(20)
范例:2012年10月10日@ 0715
我使用此字段在SELECT语句中创建了以下内容:
LEFT(Dt_Tm,11)+''''+ SUBSTRING(Dt_Tm,13,2)+'':''+ RIGHT(Dt_Tm,2)+'':00''
这有效,我得到``2012年10月10日07:15:00''.但是,当我尝试将此插入到格式为"smalldatetime"的新表字段中时,出现以下错误:
将字符串转换为smalldatetime数据类型时转换失败"
谁能帮我解决这个问题.
谢谢.
try:
转换(日期,左(Dt_Tm, 11 )+ ' ' + SUBSTRING(Dt_Tm, 13 , 2 )+ :' + 2 )+ :00')
现在,转换函数将返回日期值
I have a field in my SQL Server 2008 table as follows:
Field Name: Dt_Tm
Format: nvarchar(20)
Example: 10 Oct 2012@0715
I have created the following in a SELECT statement using this field:
LEFT(Dt_Tm, 11) + '' '' + SUBSTRING(Dt_Tm, 13, 2) + '':'' + RIGHT(Dt_Tm, 2) + '':00''
This works and I get ''10 Oct 2012 07:15:00''. But when I try to INSERT this into a new table field that is formatted ''smalldatetime'', I get the following error:
"Conversion failed when converting character string to smalldatetime data type"
Can anyone help me with this problem.
Thank you.解决方案try:
convert(date,LEFT(Dt_Tm, 11) + ' ' + SUBSTRING(Dt_Tm, 13, 2) + ':' + RIGHT(Dt_Tm, 2) + ':00')
Now convert function will return date value
这篇关于将文本转换为DateTime格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!