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
27 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:Scott Pletcher
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

832 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