Solved

selecting first digit from a two digit integer in sql server

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBScript Write Column Headers 3 38
create an aggregate function 9 34
Find SQL query used by application 3 20
How to construct an if else statement from existing code 6 21
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

778 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