try this, I only coded to support decimals up to a precision of 5, but you can increase it if necessary:
CREATE FUNCTION [dbo].[ufn_ParseDecimal]
(
@Sequence VARCHAR(max),
@Delim CHAR(1),
@Prec INT,
@Scale INT
)
RETURNS sql_variant
AS
BEGIN
DECLARE @L VARCHAR(max)
DECLARE @R VARCHAR(max)
IF CHARINDEX(@Delim,@Sequence)>0
BEGIN
SET @L=LEFT(@Sequence,CHARINDEX(@Delim,@Sequence)-1)
SET @R=RIGHT(@Sequence,LEN(@Sequence)-CHARINDEX(@Delim,@Sequence))
END
ELSE
BEGIN
SET @L=@Sequence
SET @R=''
END
DECLARE @1_0 decimal(1,0)
DECLARE @1_1 decimal(1,1)
DECLARE @2_0 decimal(2,0)
DECLARE @2_1 decimal(2,1)
DECLARE @2_2 decimal(2,2)
DECLARE @3_0 decimal(3,0)
DECLARE @3_1 decimal(3,1)
DECLARE @3_2 decimal(3,2)
DECLARE @3_3 decimal(3,3)
DECLARE @4_0 decimal(4,0)
DECLARE @4_1 decimal(4,1)
DECLARE @4_2 decimal(4,2)
DECLARE @4_3 decimal(4,3)
DECLARE @4_4 decimal(4,4)
DECLARE @5_0 decimal(5,0)
DECLARE @5_1 decimal(5,1)
DECLARE @5_2 decimal(5,2)
DECLARE @5_3 decimal(5,3)
DECLARE @5_4 decimal(5,4)
DECLARE @5_5 decimal(5,5)
DECLARE @v sql_variant
IF @Prec=1
BEGIN
IF @Scale=0 BEGIN SET @1_0=RIGHT(@L,1) SET @v= @1_0 END
ELSE IF @Scale=1 BEGIN SET @1_1='0.'+LEFT(@R,1) SET @v= @1_1 END
END
ELSE IF @Prec=2
BEGIN
IF @Scale=0 BEGIN SET @2_0=RIGHT(@L,2) SET @v= @2_0 END
ELSE IF @Scale=1 BEGIN SET @2_1=RIGHT(@L,1)+'.'+LEFT(@R,1) SET @v= @2_1 END
ELSE IF @Scale=2 BEGIN SET @2_2= '0.'+LEFT(@R,2) SET @v= @2_2 END
END
ELSE IF @Prec=3
BEGIN
IF @Scale=0 BEGIN SET @3_0=RIGHT(@L,3) SET @v= @3_0 END
ELSE IF @Scale=1 BEGIN SET @3_1=RIGHT(@L,2)+'.'+LEFT(@R,1) SET @v= @3_1 END
ELSE IF @Scale=2 BEGIN SET @3_2=RIGHT(@L,1)+'.'+LEFT(@R,2) SET @v= @3_2 END
ELSE IF @Scale=3 BEGIN SET @3_3= '0.'+LEFT(@R,3) SET @v= @3_3 END
END
ELSE IF @Prec=4
BEGIN
IF @Scale=0 BEGIN SET @4_0=RIGHT(@L,4) SET @v= @4_0 END
ELSE IF @Scale=1 BEGIN SET @4_1=RIGHT(@L,3)+'.'+LEFT(@R,1) SET @v= @4_1 END
ELSE IF @Scale=2 BEGIN SET @4_2=RIGHT(@L,2)+'.'+LEFT(@R,2) SET @v= @4_2 END
ELSE IF @Scale=3 BEGIN SET @4_3=RIGHT(@L,1)+'.'+LEFT(@R,3) SET @v= @4_3 END
ELSE IF @Scale=4 BEGIN SET @4_4= '0.'+LEFT(@R,4) SET @v= @4_4 END
END
ELSE IF @Prec=5
BEGIN
IF @Scale=0 BEGIN SET @5_0=RIGHT(@L,5) SET @v= @5_0 END
ELSE IF @Scale=1 BEGIN SET @5_1=RIGHT(@L,4)+'.'+LEFT(@R,1) SET @v= @5_1 END
ELSE IF @Scale=2 BEGIN SET @5_2=RIGHT(@L,3)+'.'+LEFT(@R,2) SET @v= @5_2 END
ELSE IF @Scale=3 BEGIN SET @5_3=RIGHT(@L,2)+'.'+LEFT(@R,3) SET @v= @5_3 END
ELSE IF @Scale=4 BEGIN SET @5_4=RIGHT(@L,1)+'.'+LEFT(@R,4) SET @v= @5_4 END
ELSE IF @Scale=5 BEGIN SET @5_5= '0.'+LEFT(@R,5) SET @v= @5_5 END
END
RETURN @v
END
this sample code uses the function:
SELECT CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.4','.',4,1) , 'BaseType')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.4','.',4,1) , 'Precision')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.4','.',4,1) , 'Scale')) ,dbo.ufn_ParseDecimal('123.4','.',4,1)
UNION SELECT CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.45','.',5,2), 'BaseType')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.45','.',5,2), 'Precision')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('123.45','.',5,2), 'Scale')) ,dbo.ufn_ParseDecimal('123.45','.',5,2)
UNION SELECT CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('1.234','.',5,4) , 'BaseType')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('1.234','.',5,4) , 'Precision')),CONVERT(varchar(10),SQL_VARIANT_PROPERTY(dbo.ufn_ParseDecimal('1.234','.',5,4) , 'Scale')) ,dbo.ufn_ParseDecimal('1.234','.',5,4)
OUTPUT from sample code:
---------- ---------- ---------- ---------
decimal 4 1 123.4
decimal 5 2 123.45
decimal 5 4 1.2340
(3 row(s) affected)