Solved

selecting first digit from a two digit integer in sql server

Posted on 2014-03-10
2
2,081 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
[X]
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 Comments
 
LVL 22

Assisted Solution

by:Steve Wales
Steve Wales earned 250 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 26

Accepted Solution

by:
Shaun Kline earned 250 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 Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

756 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