Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
sql_server 217
t-sql 211
tsql 116
sqlserver 96
BRH 78
#SQLServer 66
#TSQL 56
SQL Server 34
function 11
SSMS 9

Archive · View All
August 2007 17
August 2010 8
June 2012 7
June 2011 7
November 2007 7
August 2012 6
May 2012 6
November 2011 6
August 2011 6
October 2011 6

Madhivanan's TSQL Blog

Datatype convertion error - Findout problamatic data

Jun 20 2012 12:00AM by Madhivanan   

There is a blog post about #TSQL CAST Conversion Error… give me the row please! where the posted showed some methods to identify the data where convertion error happens. Here are other approaches to identify the problamatic data.

Consider the following set of data.

create table #t (data varchar(100))
insert into #t 
select '798234' union all
select '798.95' union all
select 'r84' union all
select '0.784' union all
select '$129' union all
select '8734.9823.6673' union all
select '000' union all
select '1,200' union all
select '44$129' union all
select '#890.99' union all
select null 
When you try to convert the datatype into money datatype
select cast(data as money) from #t
You will get this error
Msg 235, Level 16, State 0, Line 3
Cannot convert a char value to money. The char value has incorrect syntax.

But it will not show you the row where the convertion falis. The difficult part is identify bad data and convert good data only.

Here are two ways to identify bad data

Method 1 : Use regular expression
select data from #t
where data like '%[^0-9.]%' or len(data)-len(replace(data,'.',''))>1
The above query finds out data where there is atleast a character not listed in [0-9] and a dot or have multiple dots. It returns the following data
data
---------------------
r84
$129
8734.9823.6673
1,200
44$129
#890.99

Note that it will also return numerals which are formatted. However this will be a good way to start identifying the bad data that cannot be converted to specific datatype.

Method 2 : Use Try_parse() function (SQL Server 2012 onwards) 

select data from #t
where try_convert(money,data) is null
The new try_convert() function converts the good data successfully and returns NULL for bad data. See the result below. try_convert() take cares of formatted data too
data
-----------------------
r84
8734.9823.6673
44$129
#890.99
NULL

Note : Method 1 will consider formatted data as bad data. If you want to simulate what try_convert() does, you can add extra condtions in the WHERE clause based on how you define the formation.

You may also be interested to read this post Finding out problematic data in Bulk Insert data truncation error

Tags: 


Madhivanan
3 · 39% · 12430
10
 
0
Lifesaver
 
 
 
0
Incorrect



Submit

3  Comments  

  • Try this:

    SELECT data
    FROM #t
    WHERE IsNumeric(data) = 0
    

    Or simply this:

    SELECT
       data,
       Convert(money, CASE WHEN IsNumeric(data) = 1 THEN data END) WillConvertTo
    FROM #t
    
    commented on Jun 20 2012 12:16PM
    ErikEckhardt
    65 · 3% · 887
  • Isnumeric will also work for numbers expressed in scientific notation which can not be converted to any numeric datatype. Add 12d2 in the datasource and see that your code gives convertion error.

    commented on Jun 20 2012 11:59PM
    Madhivanan
    3 · 39% · 12430
  • @Madhivanan, I used the data you provided. Scientific numbers like 12d2 can be excluded like so:

    SELECT
       data,
       Convert(money, CASE WHEN data NOT LIKE '%[^-+,.$0-9]%' AND IsNumeric(data) = 1 THEN data END) WillConvertTo
    FROM #t
    

    While TRY_PARSE is superior, many people have SQL 2008, 2005, and even 2000 databases, so this may be of some use.

    commented on Jun 21 2012 1:34PM
    ErikEckhardt
    65 · 3% · 887

Your Comment


Sign Up or Login to post a comment.

"Datatype convertion error - Findout problamatic data " rated 5 out of 5 by 10 readers
Datatype convertion error - Findout problamatic data , 5.0 out of 5 based on 10 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]