转换varchar值时,转换失败' - > '到数据类型int。我该如何解决这个问题? [英] Getting Conversion failed when converting the varchar value ' --> ' to data type int. How do I solve this?
本文介绍了转换varchar值时,转换失败' - > '到数据类型int。我该如何解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嘿伙计们,我在表格更新时触发了。这是它的一些代码:
Hey guys, I made a trigger for when a table is updated. This is a bit of its code:
INSERT INTO [dbo].Logs([date],[name],[changes],[eventId])
SELECT getdate(), 'john',
CAST(D.eventStart AS nvarchar(30)) + ' --> ' + CAST(I.eventStart AS nvarchar(30)),
I.id
FROM Inserted I
INNER JOIN Deleted D ON I.id = D.id
我正在使用CAST因为eventStart的类型是dateTime2
问题是当我运行我得到的应用程序时:
转换varchar值时转换失败' - >'到数据类型int。
知道为什么以及如何解决这个问题?
I'm using the CAST because eventStart is of the type dateTime2
The problem is when I run the application I get:
"Conversion failed when converting the varchar value ' --> ' to data type int."
Any idea why and how to fix this?
推荐答案
检查您的字段类型:最可能的原因是更改
不是NVARCHAR列,因此用螺栓连接在一起的两个日期将被转换为任何数据类型。
< br / >
本地快速检查:
Check your field types: the most likely reason is thatchanges
is not an NVARCHAR column, so your two dates bolted together are being converted to whatever datatype that is.
A quick check locally:
SELECT CAST(CAST(GETDATE() AS DATETIME2) AS NVARCHAR(30)) + '-->' + CAST(CAST(GETDATE() AS DATETIME2) AS NVARCHAR(30))
显示完全有效的字符串:
Shows a perfectly valid string:
2015-03-10 11:42:52.2230000-->2015-03-10 11:42:52.2230000
所以它有成为INSERT操作的一部分。
So it has to be part of the INSERT operation.
这篇关于转换varchar值时,转换失败' - > '到数据类型int。我该如何解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文