Solved

Need to update several columns and tables in a database and convert scientific notation to a decimal(27,17).

Posted on 2014-01-22
4
24 Views
Last Modified: 2016-02-26
Need to update several columns and tables in a database and convert scientific notation to a decimal(27,17).

Using the following for COLUMN(S) for the scientific notation part:

CASE
      WHEN COLUMN like '%E-%' THEN LTRIM(RTRIM(CAST(CAST(COLUMN AS FLOAT) AS DECIMAL(18,18))))
      WHEN COLUMN like '%E+%' THEN NULL
      ELSE COLUMN
END


What is the best way to update existing columns? SS 2008 R2
0
Comment
Question by:JGH5
  • 2
4 Comments
 
LVL 24

Expert Comment

by:chaau
ID: 39802098
Instead of doing the cast like this, I would recommend to use STR function:
CASE
      WHEN COLUMN like '%E-%' THEN LTRIM(RTRIM(STR(CAST(COLUMN AS FLOAT), 18, 8)))
      WHEN COLUMN like '%E+%' THEN NULL
      ELSE COLUMN
END

Open in new window

0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 39803652
Why are you NULLing out "E+" values?

Why not just something like:

CASE
      WHEN COLUMN like '%E%' THEN CAST(CAST(COLUMN AS FLOAT) AS DECIMAL(18,18))
      ELSE COLUMN
END
0
 

Author Comment

by:JGH5
ID: 39804285
Also, the data is already in the table as a varchar and needs to be converted to a decimal data type.
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39804687
To convert data type from varchar to decimal you will have create a temporary column, like this:
ALTER TABLE Table1 ADD COLUMN tempColumn DECIMAL(18,8) NULL

Open in new window

Then, run your Update statement:
UPDATE Table1 SET tempColumn = 
CASE
      WHEN oldCOLUMN like '%E%' THEN CAST(CAST(oldCOLUMN AS FLOAT) AS DECIMAL(18,8))
      ELSE NULL -- actually, I think you do not need any case statements at all, see below
END 

Open in new window

Then run a statement to DROP old column and rename the column:
ALTER TABLE Table1 DROP COLUMN oldColumn;
exec sp_rename 'Table1.tempColumn', 'Table1.oldColumn'

Open in new window


Note: I think the conversion should take care for the cases where data is not numeric at all. In this case you could use ISNUMERIC() function to make sure that the data converts without any problems. Check for example this statement:
;with textData as(
  SELECT '123.5E+10' AS t UNION ALL
  SELECT '123.5E-10' AS t UNION ALL
  SELECT '123.5' AS t UNION ALL
  SELECT '-2312E+5' AS t UNION ALL
  SELECT 'abcd' AS t UNION ALL
  SELECT 'text' AS t )
select t, CASE WHEN ISNUMERIC(t)=1 
               THEN convert(decimal(27,8), convert(float, t))
               ELSE NULL END AS num
FROM textData

Open in new window

The correct Update statement should be:
UPDATE Table1 SET tempColumn = 
CASE
      WHEN WHEN ISNUMERIC(oldCOLUMN)=1 THEN convert(decimal(27,8), convert(float, oldCOLUMN))
      ELSE NULL
END 

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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

17 Experts available now in Live!

Get 1:1 Help Now