Solved

sql - convert number

Posted on 2014-09-19
4
152 Views
Last Modified: 2014-09-29
How can I convert below to a single degit?

From
1.00000000000
3.00000000000
1.00000000000

to
1
3
1
0
Comment
Question by:VBdotnet2005
  • 2
4 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40333235
SELECT CAST(3.0000000 as int) as column_name_here

Keep in mind that if the above isn't numeric it will throw a type conversion error, so to make sure that isn't the case, see if the below returns any rows

SELECT CAST(your_number_column as int) as column_name
FROM your_table
WHERE ISNUMERIC(your_number_column) = 0

Open in new window

Then to select what can be converted without error...
SELECT CAST(your_number_column as int) as column_name
FROM your_table
WHERE ISNUMERIC(your_number_column) = 1

Open in new window

0
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40333274
SELECT (LEFT(value, 1)
or:
SELECT LEFT(value, CHARINDEX('.', value) - 1)
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40333316
If there might be an error, use TRY_CAST instead of CAST. That way, you will have a NULL instead of an error.

an alternative is CONVERT and TRY_CONVERT.
0
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40333333
CAST is just inviting errors, so why do it?  For example:

SELECT CAST('3.0000000' AS int) AS column_name_here --error!

If you just want to pull out a single character, there's no need for the hassle.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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.
In this article I will describe the Backup & Restore 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.
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…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

708 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