0

I'm used to working in MS SQL Server, so MySQL is always a little bit foreign to me. I wrote the following for MS SQL Server, and I'm trying to port it to MySQL:

CREATE FUNCTION ToRadians
(@Degrees float)
RETURNS float
AS
BEGIN
  DECLARE @Radians float
  SELECT @Radians  = @Degrees * 3.1415927 / 180
  RETURN @Radians
END
GO

I've got

CREATE FUNCTION ToRadians
(@Degrees float)
RETURNS float
BEGIN
  DECLARE @Radians float;
  SELECT @Radians  = @Degrees * 3.1415927 / 180;
  RETURN @Radians;
END

but in PHPMyAdmin, that gives me the error:

Error

SQL query:

CREATE FUNCTION ToRadians(
@Degrees float
) RETURNS float AS BEGIN DECLARE@Radians float;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@Degrees float)
RETURNS float
AS
BEGIN
DECLARE @Radians float' at line 2 

The searching I've done indicates that the above MySQL UDF should be correct, but it obviously isn't. SELECT version() returns 5.0.77

2 Answers 2

1
CREATE FUNCTION ToRadians (in_degrees FLOAT) RETURNS FLOAT
DETERMINISTIC
BEGIN
    RETURN in_degrees * 3.1415927 / 180;    
END;    

DETERMINISTIC because the return for a given value of degrees should always be the same in radians.

Also DEGREES() and RADIANS() functions already exist in mysql. RADIANS()

Sign up to request clarification or add additional context in comments.

1 Comment

I did not know that function existed. Fancy.
0
  1. Don't use @ in MySQL variable names, it creates a local session variable.
  2. You probably need to change standart delimiter which is semicolon to another character, for instance // : DELIMITER // before your function definition, then // just after function, then switch delimiter back to semicolon (DELIMITER ;)

2 Comments

Also, it turns out I can't use SELECT to do math within a function. I should have been using SET Radians = Degrees * 3.1415927 / 180;
You could also do SELECT ... INTO [variable]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.