Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

selecting first digit from a two digit integer in sql server

Posted on 2014-03-10
2
Medium Priority
?
2,223 Views
Last Modified: 2014-03-10
I have a sql server 2008.
One of my columns is an integer column.
This column contains the numbers 1 through 30.
So i have 30 rows in my table.

in my select statement how do I only select the first digit of any value that is in this column?

So I would want all the rows containing 10-19 to only show 1
So I would want all the rows containing 20-29 to only show 2
So I would want the row containing 30 to only show 3
0
Comment
Question by:maqskywalker
2 Comments
 
LVL 23

Assisted Solution

by:Steve Wales
Steve Wales earned 1000 total points
ID: 39918484
If the datatype is integer, just do an integer divide:

select column_name / 10
from table

If it's a decimal you can CAST it

select cast (column_name / 10 as INT)

With this, 0-9 shows 0, 10-19 shows 1 etc
0
 
LVL 27

Accepted Solution

by:
Shaun Kline earned 1000 total points
ID: 39918508
If you want 1-9 to appear as 1-9, 10-19 to appear as 1, 20-29 as 2 and 30 to appear as 3, cast the number into a varchar and use left to get the first "character".
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

Question has a verified solution.

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

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…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

927 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