Error converting data type varchar

前端 未结 8 1498
既然无缘
既然无缘 2020-12-20 15:07

I currently have a table with a column as varchar. This column can hold numbers or text. During certain queries I treat it as a bigint column (I do

8条回答
  •  再見小時候
    2020-12-20 15:32

    OK. I finally created a view that works:

    SELECT TOP (100) PERCENT id, CAST(CASE WHEN IsNumeric(MyCol) = 1 THEN MyCol ELSE NULL END AS bigint) AS MyCol
    FROM         dbo.MyTable
    WHERE     (MyCol NOT LIKE '%[^0-9]%')
    

    Thanks to AdaTheDev and CodeByMoonlight. I used your two answers to get to this. (Thanks to the other repliers too of course)

    Now when I do joins with other bigint cols or do something like 'SELECT * FROM MyView where mycol=1' it returns the correct result with no errors. My guess is that the CAST in the query itself causes the query optimizer to not look at the original table as Christian Hayter said may be going on with the other views

提交回复
热议问题