Spatial SQL -> MS-SQL 2008. Function that creates Geography object (LL WGS 84)

In the following example we publish function that creates Geography object from input Longitude/Latitude values.

/*
sfn_CreateGeog creates Geography object (LL WGS-84) from input Longitude/Latitude values. Returns NULL if one of the arguments is NULL.
*/


IF EXISTS (SELECT * FROM sys.objects
  WHERE object_id = OBJECT_ID(N'[dbo].[sfn_CreateGeog]')
  AND type IN (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[sfn_CreateGeog]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION dbo.sfn_CreateGeog (@Longitude FLOAT, @Latitude FLOAT)
RETURNS GEOGRAPHY
WITH EXECUTE AS CALLER, RETURNS
NULL ON NULL INPUT
AS
BEGIN
  DECLARE
@g GEOGRAPHY;
  SET @g = GEOGRAPHY::STGeomFromText(
  'POINT(' + CONVERT(Varchar(30),@Longitude, 2)
  + ' ' + CONVERT(Varchar(30),@Latitude, 2) + ')',4326);

  RETURN @g;
END
GO


Test function

SELECT dbo.sfn_CreateGeog(9.182223, 45.468214).STBuffer(1) AS Geog





sqlexamples.info