Solved

sql - convert number

Posted on 2014-09-19
4
153 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

920 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

17 Experts available now in Live!

Get 1:1 Help Now