sql - convert number

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


Question by:VBdotnet2005
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
  • 2
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

LVL 69

Accepted Solution

Scott Pletcher earned 500 total points
ID: 40333274
SELECT (LEFT(value, 1)
SELECT LEFT(value, CHARINDEX('.', value) - 1)
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.
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.

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
point in time restore in SQL server 26 46
SQL Throw Error 7 35
SQL query 7 20
mysql vs miscrosoft sql server 6 20
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

730 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