[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
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
Medium Priority
?
34 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 25

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 70

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 25

Accepted Solution

by:
chaau earned 2000 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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.
Suggested Courses

873 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