Solved

How to convert number 123.8 to 1238, removing decimal, in SQL query?

Posted on 2016-11-14
5
47 Views
Last Modified: 2016-11-16
How do I remove the decimal point in a string using a SQL statement?

Also, the result must result in a Char field with 7 spaces.

So 123.8 would be 1238xxx with x's being blank spaces in a fixed width file.

or D02.3 as D023xxx with x's being blank spaces always to the right of the string.

I am using SQL 2008.
0
Comment
Question by:Becky Edwards
[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
5 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 41887046
GT/LT just included to illustrate the spaces...

DECLARE @Test	VARCHAR(30) = '123.8'

SELECT '>' + LEFT(CAST(REPLACE(@Test, '.', '') AS CHAR(7)) + '       ', 7) + '<'

Open in new window

0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41887137
Try, Replace . with '' and use RTRIM to remove spaces from the right hand side and the add SPACE(7).

Note - RTRIM is used because if we have value like '128.8 ', then we cannot add 7 spaces directly. If we add then we will get 8 spaces. So first we need trim the spaces from the right hand side and then add the 7 spaces we need.

Note - | (Appended pipe) so that we can check that there are no spaces after the string.

DECLARE @ AS VARCHAR(100) = '123.8    '

SELECT '|' + RTRIM(REPLACE(@,'.','')) + SPACE(7) + '|'

GO
DECLARE @ AS VARCHAR(100) = 'D02.3    '

SELECT '|' + RTRIM(REPLACE(@,'.','')) + SPACE(7) + '|'

Open in new window


Output

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|1238       |

(1 row(s) affected)

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|D023       |

(1 row(s) affected)


Code for you with out pipes


DECLARE @ AS VARCHAR(100) = '123.8    '

SELECT RTRIM(REPLACE(@,'.','')) + SPACE(7) 

GO
DECLARE @ AS VARCHAR(100) = 'D02.3    '

SELECT RTRIM(REPLACE(@,'.','')) + SPACE(7)

Open in new window


Hope it helps !!
0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 41887950
Hello,

You can try this

SELECT LEFT(REPLACE(COLUMN,'.','') + '       ',7) FROM TABLE

Open in new window

0
 

Author Closing Comment

by:Becky Edwards
ID: 41888409
These all worked!
  I gave the points to the first person who answered.

Thank you.
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41889376
No problem :) , Thanks Becky !!

Regards,
Pawan
0

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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