Solved

sql - convert number

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 46
What's wrong with this T-SQL Foreign Key? 7 46
Query group by data in SQL Server - cursor? 3 34
average of calculation (TSQL) 4 10
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

831 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