sql-serversql-server-cesql-server-ce-3.5

Get Float value with Comma separtor


Am new to Sql server ce. my table have float value. I want select float with comma separated and decimal position. like this

table name table1

 val
 1220333
 222
 36535

I want result like this

val
12,20,333.00
222.00
36,535.00

like indian Rupees

Am using Sql server ce 3.5


Solution

  • DECLARE @Table1 TABLE 
        (val int)
    ;
    
    INSERT INTO @Table1
        (val)
    VALUES
        (1220333),
        (222),
        (36535)
    ;
    select convert(varchar(50), CAST(val as money), -1) amount from @Table1
    
    select FORMAT(CAST(val AS MONEY),'N','en-in') amount from @Table1
    

    OR

    Function

    create function to_indian_currency(@n decimal(25,5))
    returns varchar(100) as
    BEGIN
    declare @a varchar(100) = cast(@n as varchar(100))
    declare @dec_part varchar(100) = 
    (select substring(@a, charindex('.',@a), len(@a)-charindex('.',@a)+1))
    declare @int_part varchar(100) = (select left(@a, charindex('.',@a)-1))
    declare @f int = cast(@int_part as bigint)%1000
    declare @q int = cast(@int_part as bigint)/1000
    declare @final varchar(100) = ''
    while @q > 0
    begin
    set @final = cast(@q%100 as varchar) + ',' + @final
    set @q = @q/100
    end
    RETURN @final + cast(@f as varchar) + @dec_part
    END
    

    select dbo.to_indian_currency(val) from @Table1