Monday, 9 October 2017

Convert Data in currency format using sql server all version

// Query

create function Currency_Format(@ValToConver float,@CommaInt int)
returns varchar(200)
as
begin
  declare @RepsVal varchar(50)
  declare @p1 varchar(40)
  declare @p2 varchar(10)

  set @RepsVal = replace(convert (varchar(20), convert(money, @ValToConver), 3 ) , ',','')
  set @p1 = left(@RepsVal, charindex('.', @RepsVal)-1)
  set @p2 = substring(@RepsVal, charindex('.', @RepsVal), 10)

  set @RepsVal = right(@p1, 3) + @p2
  set @p1 = left(@p1, len(@p1)-3)

  while (@p1<>'')
  begin
    set @RepsVal = right(@p1, @CommaInt) + ',' + @RepsVal
    if (len(@p1) > @CommaInt)
      set @p1 = left(@p1, len(@p1)-@CommaInt)
    else
      set @p1= ''
  end

  return(@RepsVal)

end


/// Result



No comments:

Post a Comment

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...