Select objects within radius using longitude and latitude [duplicate] - sql

This question already has answers here:
SQL Query for Performing Radius Search based on Latitude Longitude
(4 answers)
Distance Calculation with huge SQL Server database
(1 answer)
Closed 9 years ago.
I wonder how I can create a query and select objects that are located within a radius of 50 km.
The information I can use is Longitude and latitude for every object. I would like to set my own location in the query and calculate the objects distance to my position based on its coordinates.

Assuming you're using a geography type, which you should be, use STDistance - see http://technet.microsoft.com/en-us/library/bb933808.aspx
select *
from yourtable
where place.STDistance(#myposition)<50000

Related

SQL difference between dividing by float and by int [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
SQL Server, division returns zero
(6 answers)
Closed 8 months ago.
so I'm currently learning SQL and one of the exercies was to:
Show patient_id, weight, height, isObese from the patients table. Display isObese as a boolean 0 or 1. Obese is defined as weight(kg)/(height(m)2) >= 30. Weight is in units kg and height in units cm.
I managed to do it this way:
SELECT patient_id, weight, height,
CASE
WHEN weight/POWER(height/100,2) >= 30 then '1'
ELSE '0' END AS isObese
FROM patients
and it was incorrect. The correct solution was to write: weight/POWER(height/100.0,2).
So my question is why I have to divide by 100.0 instead of 100 to change from centimeters to meters? What is the difference? In this table every height value was an int type, without decimal numbers.

Rounding hour numbers doing daylight saving time [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Can`t divide two INTEGERS in SQL Server [duplicate]
(2 answers)
SQL Server, division returns zero
(6 answers)
Closed 11 months ago.
Can anyone show how I do something as simple as round to the nearest integer?
select round((24+24+24)/24,0) result: 3
select round((24+24+23)/24,0) result: 2
select round((24+24+25)/24,0) result: 3
Is there something I can do so all three examples will give me the result 3?
Thank you in advance
The problem is integer division. All of the values are integers, so the 2.9whatever is truncated to just 2 before you even start rounding.
This works just fine by including a floating point value with the division operation:
select round((24+24+24)/24.0,0)
select round((24+24+23)/24.0,0)
select round((24+24+25)/24.0,0)
The other option is always just throw in a bonus hour. Then you don't even need to round:
select (24+24+24+1)/24
select (24+24+23+1)/24
select (24+24+25+1)/24

Can`t divide two INTEGERS in SQL Server [duplicate]

This question already has answers here:
Integer division in sql server
(8 answers)
Closed 1 year ago.
Why do I get 3 when I execute this T-SQL script:
SELECT 10/3;
I have tried :
SELECT CAST((10/3) AS decimal(5,2));
But now I get 3.00 - still not what I expected
If you are using two integers in a division it will do integer division. You need to cast at least one of the integers to float or decimal before the division to get what you are looking for.
{int}/{int}={int}. SQL Server, or any good language fior that matter, won't implicitly change the data type of a expressions results when only 1 data type is involved (in this case int). Thus 10/3 = 3 is correct.
As for the latter, you still have {int}/{int}={int}, but you then convert the result to a decimal. 3 as a decimal is 3.00 so that too is correct.
You need to convert one of values before you divide; changing the data type of either the dividend or the divisor works. For example:
SELECT (10 * 1.) / 3,
10 / CONVERT(decimal(2,0),3);

i would like to display only non rounded decimal value in sql access [duplicate]

This question already has answers here:
Difference between numeric, float and decimal in SQL Server
(8 answers)
Closed 5 years ago.
I wanted to to display only the non rounded decimal values using sql access.
I have tried the the query;
select * from table1 where oc_amount!=0;
But its not returning the value.
enter image description here
You would need to compare the raw value to the rounded value
select * from table1 where oc_amount <> Round(oc_amount, 0)
If that isn't what you really wanted, try:
select * from table1 where (Round(oc_amount, 2) - Round(oc_amount, 0)) > 0
(*adjust the number of decimals of rounding to suit your need)

How to calculate age on a Microsoft Access form? [duplicate]

This question already has answers here:
calculating age in years and months in Microsoft Access (2010)
(2 answers)
Closed 7 years ago.
On the Microsoft Access form I have created for patient data at a hospital I want to use the field DATE OF BIRTH and the field DATE OF FORM (current date) in order to calculate the three fields
age(yr)
age(mo), and
age(days).
Is it possible to do this after just being given two dates?
Yes, those two dates are enough to calculate age. So you can use the DateDiff function in the Expression builder of the Age textboxes.
Below, I assume Age(yr) means age using years, Age(mo) means age using months, and Age(days) means age using days.
In the control source of Age(yr) textbox use:
=DateDiff("yyyy",[DateOfBirth],Date())
For Age(mo):
=DateDiff("m",[DateOfBirth],Date())
For Age(days):
=DateDiff("d",[DateOfBirth],Date())