How to return first few characters in a string by trimming last 8? (SQL Server 2008)
35,301
Solution 1
See SUBSTRING
select substring(column_name, 1, len(column_name)-8)
from table_name
Solution 2
declare @str varchar(50)
set @str = 'ABCD99991000'
select LEFT(@str, len(@str) - 8)
(No error checking for Len(@str) < 8)
so,
select LEFT(colname, LEN(colname) - 8)
from table_name
Solution 3
BEGIN
declare @s varchar(16)
select @s = 'abcdefghij'
--
SELECT LEFT(@s, LEN(@s) - 8)
END
Solution 4
LEFT(E2499991039,LEN(E2499991039)-8)
Solution 5
Left(column, length(column - 8))
Author by
eek
Updated on March 28, 2020Comments
-
eek about 4 years
I have a bunch of strings like this:
'ABCD99991000' 'XYZ79991000' 'E2493991039'
What I really care about is everything before the last 8 characters. The problem is that the characters I care about at the start of the string are of variable length. In the above examples, what I'd want to return is:
'ABCD' 'XYZ' 'E24'
The RIGHT() function would be perfect if I could get it to return everything BEFORE the results. For example, RIGHT(E2499991039,8) returns the numbers I don't want! How can I return the variable length strength before the last 8 characters in my query?
SQL Server 2008