question

vasanth avatar image
vasanth asked

Try_cast conversion issue

Hello All, I'm facing a wired issue. It would be great if some one help me on this. This query worked like a charm until i add where clause with IS NULL . If i execute with Is null i got below error (With IS not Null query is working fine). When i execute the inner query i got the desired result set invalid records with null values and Valid records has been casts properly. But when i try to filter it in outer query it fails that too because of few records. Doubts: In inner query all invalid records has been converted to null i could see that. But why it fails when it comes to outer query that too only because of few records (i found this by executing invalid records one by one)? Is that issue with Try_cast? select Number,content from (select Name, Number,TRY_cast(TRY_CAST(TRY_CAST((Content) AS VARBINARY (MAX)) AS XML) as nvarchar (max)) as content from Documents where LTRIM(RTRIM(NAME)) in ('XYZ.xmll') ) A where Content is null Error: Warning: Fatal error 7102 occurred at Jun 7 2016 4:47PM. Note the error and time, and contact your system administrator. Thanks Rathna
query
2 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image Kev Riley ♦♦ commented ·
can you provide some example data that gives this error?
0 Likes 0 ·
vasanth avatar image vasanth commented ·
Kev, Thanks for your reply. It is xml records saved has BLOB. I could not share data since all have more than 20 mb. FYI: The conversion fails, If there is no matching end tag or of any junk character. Thanks Rathna
0 Likes 0 ·
Kev Riley avatar image
Kev Riley answered
Does the query work without TRY_CAST? select Number , content from ( select Name , Number , Content from Documents where ltrim(rtrim(NAME)) in ( 'XYZ.xmll' ) ) A where content is null;
4 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

vasanth avatar image vasanth commented ·
Yes. It works. Even for almost all invalid data it works. In inner query could see null values for all invalid records. Error comes in to picture only when I use the casted content column in where clause or in order by
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
OK. So what about moving the is null inside the inner query select Number , content from ( select Name , Number , try_cast(try_cast(try_cast(( Content ) as varbinary(max)) as xml) as nvarchar(max)) as content from Documents where ltrim(rtrim(NAME)) in ( 'XYZ.xmll' ) and content is null ) A ;
0 Likes 0 ·
vasanth avatar image vasanth commented ·
To use isnull in inner clause we have to try_cast it in where clause too. If i do that again I'm getting same error. Since there is no null in content column. They null value come when we do try_cast.
0 Likes 0 ·
Kev Riley avatar image Kev Riley ♦♦ commented ·
OK so now we know it is the try_cast (don't forget I'm doing this blind here :) ) but I still think it's to do with your data. The limited testing I've done and your query works fine, the only difference is I don't have your data :(
0 Likes 0 ·
vasanth avatar image
vasanth answered
I Appreciate your effort Kev Riley. TRY_CAST has converted the invalid to null. I could see that in result set. My doubt is like is there any chance that Try_cast may returning null value without EOF. I got this doubt when i do the Name with order by. i took two records NAME AA - Valid record BB- Invalid record When i do order by ASC it works fine. But if i do order by DESC it through error. I have three record. NAME AA- Valid record BB- Invalid record CC- Valid record I not able do Order by in ASC or DESC. Hope This will narrow down my problem. Below is my inner query result for invalid record NAME, Try_Cast_Fun AM, (null) Thanks Rathna
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.