Search code examples
sqlsql-serverspatialgeography

How to set a point as default value for a geography column?


I have this SQL command that creates a table with a GEOGRAPHY-type column for spatial data that is meant to store a latitude/longitude pair (a point). How can I set a default value for that column? For example, a point at (0,0)?

CREATE TABLE [dbo].[StationLocations] (
    [Id] [int] NOT NULL,
    [StationId] [int] NOT NULL,
    [Position] [Geography],
CONSTRAINT [PKStationLocations] PRIMARY KEY CLUSTERED ([Id]))

Solution

  • try this:

    CREATE TABLE #TEMP(ID INT,COL1 GEOGRAPHY DEFAULT(CONVERT(GEOGRAPHY,'POINT (0 0)')))
    INSERT INTO #TEMP (ID) VALUES(1)
    SELECT * FROM #TEMP
    ID  COL1
    1   0xE6100000010C00000000000000000000000000000000
    
    SELECT ID,CONVERT(VARCHAR,COL1) AS DEF_VALUE FROM #TEMP
    ID  DEF_VALUE
    1   POINT (0 0)
    

    In your case

    CREATE TABLE [dbo].[StationLocations] (
        [Id] [int] NOT NULL,
        [StationId] [int] NOT NULL,
        [Position] [Geography] DEFAULT(CONVERT(GEOGRAPHY,'POINT (0 0)')),
    CONSTRAINT [PKStationLocations] PRIMARY KEY CLUSTERED ([Id]))