Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

In SQL, how can I remove the first 4 characters of values of a specific column in a table? The column name is Student Code and an example value is ABCD123Stu1231. I want to remove first 4 chars from my table for all records

Please guide me

1 Answer

0 votes
by (40.7k points)

Try using the below code:

SELECT RIGHT(MyColumn, LEN(MyColumn) - 4) AS MyTrimmedColumn

In the above code, RIGHT takes 2 arguments - the string (or column) to operate on, and the number of characters to return (starting at the "right" side of the string). 

LEN syntax is used to return the length of the column data, and we have subtracted four so that our RIGHT function leaves the leftmost 4 characters "behind".

Hope this helps you.

Note: If you want to UPDATE the table rather than just return doctored results, then you can do as follows:

UPDATE MyTable

SET MyColumn = RIGHT(MyColumn, LEN(MyColumn) - 4)

The above solution will keep the 4 characters at the start of the string, rather than discarding said 4 characters.

Related questions

0 votes
1 answer
0 votes
3 answers
0 votes
1 answer
asked Jul 13, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...