Is there a keyword I can use to grab the last n characters in a field? I can do it using substring and octet length and such, but figured there has to be a better way.
How about this to get the last 3 characters from a column
SELECT SUBSTRING(TRIM(column) FROM CHARACTER_LENGTH(TRIM(column)) -2 FOR 3) from Table
If column is CHAR(10) and contains "WEST "
TRIM removes trailing spaces
CHARACTER_LENGTH returns 4
subtract 2 from CHARACTER_LENGTH = 2
SUBSTRING FROM 2 FOR 3 gives "EST"
Thanks jgerstb. I knew it could be done with lengths and such, just figured there'd be some keyword. In programming, all language I've used have some kind of left, right, mid functionality. But, I've never had to try it in SQL. Just curious if one existed.
I tried that and it just gives me the whole field.
Here's what I'm trying to do. I have a field that I need to get codes from. The source data comes from multiple sources. The field is defined as char(5), but can be 3-5 digits. All I need are the last 4 digits (if there are 4 there). If it's 3, I want all 3. If it's 4, I want all 4. If it's 5, then I only want the 4 on the right.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.