Solved

selecting first digit from a two digit integer in sql server

Posted on 2014-03-10
2
1,973 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 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 25

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
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 backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

708 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

13 Experts available now in Live!

Get 1:1 Help Now