Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Calculating distance using Long/Latitude & Converting Int. to Decimal
    primarykey
    data
    text
    <p>Original Post: <a href="https://stackoverflow.com/questions/14653875/changing-integer-to-floating-point-and-adding-decimal-point">Changing integer to floating point and adding decimal point</a></p> <p>So here's the back story: I'm working with a database that receives GPS coordinates from vehicles and I need to figure out the mileage of those vehicles based on the Longitude and Latitude values. However, in the database both columns Longitude/Latitude are "Int/Not null" and have no decimal places. So they look like this:</p> <pre><code>Latitude Longitude 36158500 115949833 36340000 115914667 36153488 115944875 </code></pre> <p>and I need it to look like this: </p> <pre><code>Latitude Longitude 36.158500 115.949833 36.340000 115.914667 36.153488 115.944875 </code></pre> <p>With Tim Lehner's help, we figured out how to get the decimal point, but I can't use the formula I already created to figure out the mileage. I have to be able to search for the long/lat values based on the radio_name so that I can look at each vehicle's mileage individually, which is why I created a temp table called CTE and CTE2. However, the code will not work if I call the temp table because it says it's an invalid object. Now the code works fine if I plug in "AVL" (the original table's name) versus using CTE2 on my last statement below. However, the mileage isn't calculated using the new lat/long values. </p> <p>So just as a recap: the columns are Latitude, Longitude. We need to convert these columns to place a decimal point after 2 characters in latitude, and after 3 characters in longitude. Then we need a column named Miles which calculates the mileage between the latitude and longitude values given. </p> <p>Any tips, help, pointers, etc. are all greatly appreciated! Thank you! </p> <pre><code>USE [system] GO With CTE as (SELECT * FROM AVL WHERE (DATE_TIME between '01/30/2013 00:00:00' AND '01/30/2013 23:59:59') AND radio_name = 'MAS7'), CTE2 as (select *,row_number() over(partition by Vehicle_ID order by Date_Time) as RN FROM CTE) SELECT *, sqrt((69.1*(previous.Latitude - next.Latitude))* (69.1*(previous.Latitude-next.Latitude)) + (69.1*(previous.Longitude-next.Longitude)) * cos(next.Latitude/57.3) * (69.1*(previous.longitude-next.Longitude)) * cos(next.Latitude/57.3)) as Miles From CTE2 as Previous Join CTE2 as Next On previous.Vehicle_ID = Next.Vehicle_ID AND Previous.RN = Next.RN - 1 select cast(Latitude / 1000000.0 as decimal(10, 6)) as Latitude , cast(Longitude / 1000000.0 as decimal(10, 6)) as Longitude from cte2 </code></pre> <p><b>****EDIT****</b> <br> Desired outcome would look something similar to this: <br></p> <pre><code>Latitude Longitude Mileage Radio_Name 36.158500 115.949833 3.444 MAS7 36.340000 115.914667 3.443 MAS7 36.153488 115.944875 4.544 MAS7 </code></pre> <p>(That's not the correct mileage but it's just an example of what I'm looking for as the outcome.) </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload