Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combining LEFT and RIGHT functions.

Author  Topic 

revdnrdy
Posting Yak Master

220 Posts

Posted - 2008-12-10 : 13:00:42

Hello;

We have a table where each field from a column is 5 chars. Recently I was asked to write a simple query to extract the 2 rightmost characters.

so before extraction the values in employeeID would be represented as:
10000
10001
20000
20001
etc..

However this query returns nothing (blank fields)

select left(employeeID,2) from pjemploy

While this query returns what I expect

select right(left(employeeID,5),2) from pjemploy

output:
00
01
00
01

Why is it that I needed to combine the right and left functions together? Shouldn't I have been able to use left by itsef as in the first query? Again these are just chars with a guaranteed length of 5.
Nothing significant about the datatype in my view..

Do I miss something obvious?

r&r

hanbingl
Aged Yak Warrior

652 Posts

Posted - 2008-12-10 : 13:45:13
unless you specify column length 5 you will need to RTrim the column.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-10 : 21:05:25
try like below

RIGHT(LTRIM(RTRIM(yourfield)),2)
Go to Top of Page
   

- Advertisement -