Solved

sql - convert number

Posted on 2014-09-19
4
155 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:
Scott Pletcher 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:Scott Pletcher
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

In this article I will describe the Copy Database Wizard 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…
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 the fundamental information of how to create a table.

765 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