Solved

Convert Data in Table from float to Integer

Posted on 2013-01-17
3
432 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 143

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 143

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

821 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