Changing integer to floating point and adding deci

2019-06-05 01:58发布

问题:

I'm working with SQL on Microsoft SQL Server Management Studio 2008.

In a nut shell, I'm receiving data on coordinates as 115949833 and I need it to output as 115.949833 because I need to be able to calculate the mileage between the latitude and longitude coordinates. Both the longitude and latitude values are saved as integers with no decimal places. I was trying to change it to a string so I could use a substring to concatenate and convert it into a floating point. I've already figured out the formula for the mileage between lat and long values but its not accurate because of the issue with the decimal point.

Here's the code I used for the longitude:

Cast(substring(str(longitude,1,not null),1,3)+'.'+substring(str(longitude,4,not null),4,9) as float) as longitude

It keeps telling me I have an incorrect syntax near CAST.

I've been doing a lot of research and still couldn't find an exact way of performing this task. If anyone could provide any feedback, tips, help, etc. It would be greatly appreciated. Thanks.

***EDIT: I need the entire column named "Longitude" to be converted into a floating point or to somehow display a decimal after the 3rd character and the entire column named "Latitude" to display a decimal after the 2nd character.

So for example right now my columns show:

Latitude Longitude
36158500 115949833
36340000 115914667
36153488 115944875

and I need it to look like this:

Latitude Longitude
36.158500 115.949833
36.340000 115.914667
36.153488 115.944875

*EDIT* So using Tim Lehner's Answer, I've used my code and implemented his answer, but now I need to figure out how to get it to show the mileage using the new Latitude and Longitude columns... I created a few temp table's to store the information I wanted in based on specific radio_name's but I can't pull the info from those temp tables using Tim's Answer.

Here's the code I'm using:
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 AVL

The actual code itself does work and function properly but the mileage calculation is using the original whole latitude/longitude numbers and not the updated numbers that have the decimal point.

I tried plugging in CTE2 in the last statement instead of AVL and it says it's an invalid object name. Any pointers or tips?...

回答1:

I like to use datatypes to my advantage:

select 115949833 / 1000000.0
-- returns 115.949833000

You can then round/truncate to your spec.

Adding the decimal point to the divisor will promote the output of this operation to numeric per the rules of datatype precedence.

UPDATE

Per your test data, you might use a query like this:

select cast(Latitude / 1000000.0 as decimal(10, 6)) as Latitude
  , cast(Longitude / 1000000.0 as decimal(10, 6)) as Longitude
from MyTable

/*
Returns:
Latitude    Longitude
36.158500   115.949833
36.340000   115.914667
36.153488   115.944875
*/


回答2:

you always want to add the dot after the 3rd character? use the STUFF function

DECLARE @v VARCHAR(100)
SELECT @v = '115949833'

SELECT CONVERT(FLOAT,(STUFF(@v,4,0,'.')))

Output

115.949833

Another way is to multiply by 0.000001

DECLARE @v VARCHAR(100)
SELECT @v = '115949833'


SELECT CONVERT(INT,@v)  * 0.000001


回答3:

I'd use STUFF:

select stuff(longitude,4,0,'.') from table

Here is the Fiddle.

And if you need it to be float, then just add cast:

select cast(stuff(longitude,4,0,'.') as float)  from table

Good luck.



回答4:

SELECT round(substring(longitude,1,3)+'.'+substring(longitude,4,6),6)
FROM TableName