Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last remaining characters if the field lenght is greater than 5

Tags:

sql

sql-server

I was wondering if there is a way to select the remaining characters from the varchar field if the field length is greater than 5.

For example:

ID_Num

99984   
99923GF
99100EFK
99341LM
99145RL4C

What I am trying to get:

ID_Num

GF
EFK
LM
RL4C
like image 242
Milan Pk Avatar asked Dec 22 '22 23:12

Milan Pk


1 Answers

STUFF is great for things like this:

SELECT STUFF(ID_Num,1,5,'')
FROM YourTable
WHERE LEN(ID_Num) > 5;

STUFF is used to "replace" characters within a string. In this case, it replaces the next 5 characters from position 1 (which is the start of the string), with the string ''; thus removing them.

like image 129
Larnu Avatar answered Dec 28 '22 22:12

Larnu