Solved

Convert Data in Table from float to Integer

Posted on 2013-01-17
3
444 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Do you have a plan for Continuity?

It's inevitable. People leave organizations creating a gap in your service. That's where Percona comes in.

See how Pepper.com relies on Percona to:
-Manage their database
-Guarantee data safety and protection
-Provide database expertise that is available for any situation

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

617 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