Hello,

This is my first post.. so.. here goes!

I have the following formula for calculating bearing in Excel:

=IF(ISERR(MOD(ATAN2(COS(RADIANS([@OriginLatitude]))*SIN(RADIANS([@DestinationLatitude]))-SIN(RADIANS([@OriginLatitude]))*COS(RADIANS([@DestinationLatitude]))*COS(RADIANS([@DestinationLongitude]-[@OriginLongitude])),COS(RADIANS([@DestinationLatitude]))*SIN(RADIANS([@DestinationLongitude]-[@OriginLongitude])))*180/PI(),360)),0,MOD(ATAN2(COS(RADIANS([@OriginLatitude]))*SIN(RADIANS([@DestinationLatitude]))-SIN(RADIANS([@OriginLatitude]))*COS(RADIANS([@DestinationLatitude]))*COS(RADIANS([@DestinationLongitude]-[@OriginLongitude])),COS(RADIANS([@DestinationLatitude]))*SIN(RADIANS([@DestinationLongitude]-[@OriginLongitude])))*180/PI(),360))

I have OriginLongiutde, OriginLatitude, DestinationLongitude and DestinationLatitude columns within a table in SSMS.

Can anyone help with converting this formula to T-SQL?