{"id":102,"date":"2012-01-02T11:40:00","date_gmt":"2012-01-02T11:40:00","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2012\/01\/02\/sql-server-peculiar-issue-error-converting-varchar-to-bigint\/"},"modified":"2012-01-02T11:40:00","modified_gmt":"2012-01-02T11:40:00","slug":"sql-server-peculiar-issue-error-converting-varchar-to-bigint","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2012\/01\/02\/sql-server-peculiar-issue-error-converting-varchar-to-bigint\/","title":{"rendered":"SQL Server Peculiar Issue – Error Converting Varchar to Bigint"},"content":{"rendered":"

Yesterday I had a chat with one of my friend who was having problem with his SQL Query.
\nIll just list down his Peculiar problem and the way we solved it in this blog post. It would be a good learning to all.<\/p>\n

The problem he had:<\/strong>
\n– There was an insert script for a table(say it tablex).
\n– All the columns in this table were of type varchar(100).
\n– This insert script was running fine for most of the insert values, but was failing when one of the column has value ‘123E99’.<\/strong>
\n – The error the script was giving was:
\n
\n Msg 8114, Level 16, State 5, Line 1
\n Error converting data type varchar to bigint.
\n <\/font><\/p>\n

All the columns being varchar, why was this error occuring?
\nWhy is this datatype conversion happening?
\nIs this conversion implicitly happening or it explicitly done?
\nWhy was this error occuring only for value ‘123E99’ but not for value ‘128M99’?<\/p>\n

Following are the steps we followed to find the reason for this error:<\/strong>
\nChecked the table definition to see if there were any triggers on the table.
\n There were no triggers on this table<\/p>\n

Analysed the input value which was giving the error.
\n‘123E99’ – This value has E as part of it.
\nWhen the below query is executed it gives 1 as output.
\n[sql]
\nSELECT isnumeric(‘123E99’);
\n[\/sql]<\/p>\n

But the output will be 0 for ‘128M99’.
\nThis means sql server understands ‘123E99’ as a numeric value but not ‘128M99’.<\/p>\n

So there is some part of code somewhere in the database which is causing this issue by validating weather the value is numeric.<\/p>\n

So we went ahead and searched for all the indexed views in the database.
\nWe found one that is using this table.<\/p>\n

Below is the piece of code in this indexed view
\n[sql]
\nCASE WHEN ISNUMERIC(tablex.column2) = 1
\nTHEN CONVERT(BIGINT,tablex.column2)
\n[\/sql]<\/p>\n

So this is the code that is giving the above error.
\nThe ISNUMERIC is giving true but conversion to big int is failing as the value is larger that that of bigint.
\nModifying this piece of code solved the issue. \ud83d\ude42<\/p>\n

Why was this indexed view code running when he was executing the insert script on tablex?<\/p>\n

Just Because tablex is being used in the indexed view, indexed view has to be indexed with new data in tablex<\/dd>\n

Learnings form this post:
\nWhen you face such peculiar issues of conversion etc.. <\/p>\n

    \n
  • \ncheck for the design of the table and see if the conversion is implicitly happening.\n<\/li>\n
  • \nCheck for the triggers on the table, and see if there is any implicit or explicit datatype conversion happening there.\n<\/li>\n
  • \nCheck for the indexed views in the database, See if there is any implicit or explicit datatype conversion in the indexed views.\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"

    Yesterday I had a chat with one of my friend who was having problem with his SQL Query. Ill just list down his Peculiar problem and the way we solved it in this blog post. It would be a good learning to all. The problem he had: – There was an insert script for a…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[128,186,3],"tags":[623,624,625,626,627,628],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}