Link to home
Start Free TrialLog in
Avatar of MohitPandit
MohitPanditFlag for India

asked on

SQL Server: How to validate Latitude & Longitude value is valid

Hello Folks,

I've one table which contains Latitude & Longitude for each record.

Is there any ways to validate Latitude & Longitude value valid or not?

Best Regards
Avatar of Walter Ritzel
Walter Ritzel
Flag of Brazil image

The latitude must be a number between -90 and 90 and the longitude between -180 and 180.
So, you can create check constraints on your fields.
create table x (
id int not null,
latitude float CHECK (latitude > -90 and latitude < 90),
longitude float CHECK (longitude > -90 and longitude < 90))

Open in new window

Avatar of MohitPandit

ASKER

Should it be >= -90 and <=90 i.e. greater than equal to 90 and less than equal to 90?

Apart, do you have any URL for this rule?
No, I don't have. But you can get many if you simply type valid latitude longitude values on google.
Here is the fixed check constraints for that.

create table x (
id int not null,
latitude float CHECK (latitude >= -90 and latitude <= 90),
longitude float CHECK (longitude >= -180 and longitude <= 180))

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you
Walter Ritzel,

Did not intend to steal the points from your very valid answer.

I am sorry about that.

Anthony
Anthony Perkins, no problem at all, man.