Solved

selecting first digit from a two digit integer in sql server

Posted on 2014-03-10
2
2,098 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

710 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