[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# SQL Query : extracting last 3 digits

Posted on 2009-05-12
Medium Priority
1,014 Views
Hi SQL experts,

here's a simple one.  I need to extract the last 3 digits of a column, but the numbers of digits in each column varies.

Column:
111101
2202
333333303

Results to equal:
101
202
303

How???
0
Question by:jetli87
[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
• 5
• 3
• 2

LVL 75

Expert Comment

ID: 24369418
SELECT RIGHT (urColumn, 3)
FROM urTable
0

LVL 1

Author Comment

ID: 24369435
I've tried that, but it doesn't display consistently.

I tried left() and right() and it doesn't get the ideal output.
0

LVL 75

Expert Comment

ID: 24369441
>I've tried that, but it doesn't display consistently.
Can u post some sample
0

LVL 1

Author Comment

ID: 24369471
Here's the first query with a filter for a specific set of records
``````***Normal Return***
select sunitcode from tenant where hproperty = 170

sunitcode
6401101
6401102
6402211
6402213
6402214
6402215
6402216

***With Right()***
select sunitcode=right(sunitcode,4) from tenant where hproperty = 170

101
102
211
213
214
215
216
``````
0

LVL 1

Author Comment

ID: 24369497
***Here's the same query but with a different set of records***

``````***Normal Return***
select sunitcode from tenant where hproperty = 172

65101
65102
65127
65128
65129
65130

***With Right()***
select sunitcode=right(sunitcode,4) from tenant where hproperty = 172

1
2
7
8
9
0
``````
0

LVL 75

Expert Comment

ID: 24369571
SELECT RIGHT( 65101, 4 )  --- is that giving you 1 ??
0

LVL 1

Author Comment

ID: 24369580
hhmmm...no, it gave me:

5101
0

LVL 43

Accepted Solution

Eugene Z earned 2000 total points
ID: 24369585
is sunitcode not numeric?

---try
select sunitcode=right(rtrim(sunitcode),3) from tenant where hproperty = 172

0

LVL 1

Author Comment

ID: 24369600
worked like a charm.

I'm still learning Sql so i'm considerly novice, so can you explain briefly the conext of the statement you supplied and how it resolved the issue?
0

LVL 43

Expert Comment

ID: 24369696
so it was numeric..

can be :
1. it is char datatype ->
2. it is char(varchar,etc) datatype and data was pumped with  trailing blanks.
3. etc

more:

RTRIM
http://msdn.microsoft.com/en-us/library/aa238471(SQL.80).aspx

datatypes
http://msdn.microsoft.com/en-us/library/aa258271(SQL.80).aspx
0

## Featured Post

Question has a verified solution.

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

Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insiâ€¦
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In thisâ€¦
###### Suggested Courses
Course of the Month12 days, 20 hours left to enroll