CONVERT issue in sqlserver with Msg 529 error

余生长醉 提交于 2019-12-08 05:43:30

问题


select CONVERT(datetime,value,103) from results

This statement is giving me a error

"Msg 529, Level 16, State 2, Line 1
Explicit conversion from data type text to datetime is not allowed."

VALUE column Datatype is TEXT in the RESULTS table.

How can i overcome the above problem. please help !


回答1:


You can't convert from text to datetime

You must apply two convert operations.

The first: From text to varchar

The second: From varchar to datetime

So your query will become:

SELECT CONVERT(datetime,
       CONVERT(varchar(30),value), 103)
FROM results

Plus: text datatype is deprecated for new versions of Sql Server, so I strongly advice you to change (if you can) your datatype text into varchar(max).

The DDL code to apply your change is:

ALTER TABLE results ALTER COLUMN value varchar(max)



回答2:


You need first to convert text to nvarchar. Try:

select convert(datetime, convert(varchar(30),value), 103) from results

Remarks: See How to convert text column to datetime in SQL



来源:https://stackoverflow.com/questions/41831971/convert-issue-in-sqlserver-with-msg-529-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!