Solved

Convert Data in Table from float to Integer

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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…
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
Viewers will learn how the fundamental information of how to create a table.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now