Skip to content

Instantly share code, notes, and snippets.

@mohclips
Created August 20, 2020 22:49
Show Gist options
  • Save mohclips/c888f67cb8e0589e3812d69d2dc6c2fa to your computer and use it in GitHub Desktop.
Save mohclips/c888f67cb8e0589e3812d69d2dc6c2fa to your computer and use it in GitHub Desktop.
sql frunction to convert degrees to N E S W compass points
DROP FUNCTION IF EXISTS deg_to_nesw;
DELIMITER //
CREATE FUNCTION deg_to_nesw(deg float) RETURNS char(3) DETERMINISTIC
-- based on: https://www.campbellsci.eu/blog/convert-wind-directions
BEGIN
SET @directions = 'N,NNE,NE,ENE,E,ESE,SE,SSE,S,SSW,SW,WSW,W,WNW,NW,NNW,N';
RETURN substring_index(substring_index(@directions,',',Round((deg MOD 360)/ 22.5,0)+1),',', -1);
END
//
DELIMITER ;
select deg_to_nesw(72.8);
@mohclips
Copy link
Author

this is quite clever too.

SELECT deg_to_nesw(seq*22.5) FROM seq_0_to_15 ;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment