Convert decimal to varchar or char

I need to make a database that returns a value type char or varchar being the first to enter a decimal by means of a function.
CREATE FUNCTION Descripcion (@precio decimal(8,2))
RETURNS decimal
AS BEGIN DECLARE @descripcion char(25)
SET @precio =
CASE
WHEN @precio <= 50 then 'Barato'
WHEN @precio > 50 and @precio <= 100 then 'Regular'
WHEN @precio > 100 and @precio <= 200 then 'Caro'
ELSE 'Muy Caro'
END
RETURN @descripcion
END

SELECT IdProducto, ProdNombre, PrecioVenta, dbo.Descripcion(PrecioVenta) FROM Productos

But I get the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Kimiko Sora UzumakiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:
I think you need to return something other than decimal and you should be setting the value of @descripcion not @precio.

This worked for me.

CREATE FUNCTION Descripcion (@precio decimal(8,2))
RETURNS char(25)
AS BEGIN DECLARE @descripcion char(25)
SET @descripcion =
CASE
WHEN @precio <= 50 then 'Barato'
WHEN @precio > 50 and @precio <= 100 then 'Regular'
WHEN @precio > 100 and @precio <= 200 then 'Caro'
ELSE 'Muy Caro'
END
RETURN @descripcion
END
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kimiko Sora UzumakiAuthor Commented:
Thanks!!! it worked
0
Mark WillsTopic AdvisorCommented:
From a performance perspective, I would be doing
CREATE FUNCTION udf_Descripcion (@precio decimal(8,2))
RETURNS varchar(20)
AS
BEGIN 
RETURN (SELECT CASE
        WHEN @precio <= 50 then 'Barato' 
        WHEN @precio > 50 and @precio <= 100 then 'Regular'
        WHEN @precio > 100 and @precio <= 200 then 'Caro'
        ELSE 'Muy Caro' END)
END
GO

-- and now to test

select dbo.udf_Descripcion(123.45) as Descripcion

Open in new window

Multi-statement functions can be painfully expensive on resources and time. So, if you can, always try to RETURN(<sql goes here>) as the goal of a function. Sure, sometimes you need to make it multi-step / multi-command but if you have the choice always strive for InLine Scaler|Table Valued Functions.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.