Solved

Convert Data in Table from float to Integer

Posted on 2013-01-17
3
427 Views
Last Modified: 2013-01-18
RecordNumber	TLInstance	Timestamp	Hundredths	Type	Data	Flags
139543	101	2013-01-17 09:15:00.000	4	0	1.91663e+006	0
139544	101	2013-01-17 09:30:01.000	52	0	1.91673e+006	0
139545	101	2013-01-17 09:45:00.000	11	0	1.91682e+006	0
139546	101	2013-01-17 10:00:01.000	49	0	1.91692e+006	0
139547	101	2013-01-17 10:15:00.000	20	0	1.91701e+006	0
139548	101	2013-01-17 10:30:01.000	69	0	1.91709e+006	0
139549	101	2013-01-17 10:45:00.000	12	0	1.91718e+006	0
139550	101	2013-01-17 11:00:01.000	53	0	1.91727e+006	0
139551	101	2013-01-17 11:00:01.000	53	1	259::42	0
139552	101	2013-01-17 11:06:01.000	70	3	11	0
139553	101	2013-01-17 11:06:01.000	70	3	00	0
139554	101	2013-01-17 11:15:00.000	0	0	1917363	0
139555	101	2013-01-17 11:30:00.000	0	0	1917455	0
139556	101	2013-01-17 11:45:00.000	0	0	1917548	0
139557	101	2013-01-17 12:00:00.000	0	0	1917641	0
139558	101	2013-01-17 12:15:00.000	0	0	1917735	0
139559	101	2013-01-17 12:30:00.000	0	0	1917831	0

Open in new window

Hi,

I have a application archiving data every 15mins into a database. I've recently changed the datatype of the data going into the table (the table column is still a Varchar(32)), but  now need to convert the Historic data to an integer.

to better display my requirement, I've attached a sample of the old and new data. I could really use an update query to go through all of the rows, and convert from the old to new datatype, so that is displays correctly in the application. Currently, the application on displays a 1 when the Data is recorded as 1.91663e+006. I need to convert this 1.91663e+006 to its actual value of 1916630.

Any help is appreciated.
0
Comment
Question by:wint100
  • 2
3 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 38787023
this small code shall help:
declare @table table ( field varchar(20)) 
insert into @table values ( '1.91663e+006' )

select t.field
  , left(t.field, charindex('e+', t.field)-1) base
  , right(t.field, len(t.field) - charindex('e+', t.field) -1 )
  , cast(left(t.field, charindex('e+', t.field)-1) as float) * power( 10, cast(right(t.field, len(t.field) - charindex('e+', t.field) -1 ) as int ))
from @table t

Open in new window

0
 
LVL 1

Author Comment

by:wint100
ID: 38787068
Can this be used to update all historic data (2 years of samples recorded @ 15min intervals) on  table called Data? I need it to update all rows in the table.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38787211
this would do, then:
 
UPDATE  t
   SET Data	= cast(left(t.Data	, charindex('e+', t.Data	)-1) as float) * power( 10, cast(right(t.Data	, len(t.Data	) - charindex('e+', t.Data	) -1 ) as int ))
FROM yourtable t
WHERE Data	like '%e+%' 

Open in new window

0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question