views:

188

answers:

3
declare @a varchar(40)
set @a='1.23e-4'
declare @b decimal(27,12)

if isnumeric(@a) =1
begin

    select @b=cast(@a as decimal(27,12))

end
else
begin
    select @b=-1
end

select @b

when exeucting above sql code under SQL 2005 environment I am getting following error.

Error converting data type varchar to numeric

anyone knows why?

thanks.

A: 

nevermind I found my answer

at

http://stackoverflow.com/questions/312054/efficient-isnumeric-replacements-on-sql-server

N30
may i know the reason for down ranking ?
N30
+1  A: 
SELECT  @b = CONVERT(REAL, @a, 2)

Scientific notation only works on FLOAT and REAL.

Quassnoi
A: 

cast via float first.

SQL is quite strict about decimal

eg

SELECT CAST('' AS float), CAST('' AS int), CAST('' AS float) --0
SELECT CAST('' AS decimal) --error
gbn