Spatial SQL -> MS-SQL. Convert degrees to radians and radians to degrees


/*
  =================================================================
   this function converts Degrees To Radians
  =================================================================
*/


IF EXISTS (SELECT name FROM sys.objects
  WHERE name = 'sfn_DegreesToRadians' AND type IN (N'FN',N'IF', N'TF',N'FS', N'FT')
  DROP FUNCTION sfn_DegreesToRadians
GO

CREATE FUNCTION sfn_DegreesToRadians (@degrees FLOAT = 0) RETURNS FLOAT
AS
BEGIN


  DECLARE @radians FLOAT;

  IF (@degrees BETWEEN -360 AND 360)
     SET @radians = @degrees * ( PI() / 180);
  ELSE
     SET @radians = 0;

  RETURN @radians;

END
GO

/*
  =================================================================
   this function converts Radians to Degrees
  =================================================================
*/


IF EXISTS (SELECT name FROM sys.objects
  WHERE name = 'sfn_RadiansToDegrees' AND type IN (N'FN',N'IF', N'TF',N'FS', N'FT')
  DROP FUNCTION sfn_RadiansToDegrees
GO

CREATE FUNCTION sfn_RadiansToDegrees (@radians FLOAT = 0) RETURNS FLOAT
AS
BEGIN


  DECLARE @degrees FLOAT;

  IF (@radians BETWEEN (PI() * -2) AND (PI() * 2))
     SET @degrees = @radians * (180 / PI());
  ELSE
     SET @degrees = 0;

  RETURN @degrees;

END
GO


Test functions:
select dbo.sfn_DegreesToRadians (360);

select dbo.sfn_RadiansToDegrees (Pi() * 2);




sqlexamples.info