Remove the Last Character in a String in T-Sql

Remove last character from string column in SQL Server CE

Based off this example I was able to get it done using SUBSTRING:

UPDATE myTable
SET col = SUBSTRING(col, 0, LEN(col))
WHERE col LIKE '%,';

How to remove last character of the string using tSQL?

Well, just don't remove the last comma. Remove the first one instead:

  • using STUFF():

    SELECT
    STUFF(
    (CASE col1 & 1 WHEN 1 THEN ',Case 1' ELSE '' END)
    + (CASE col1 & 2 WHEN 2 THEN ',Case 2' ELSE '' END)
    + (CASE col1 & 4 WHEN 4 THEN ',Case 4' ELSE '' END),
    1,
    1,
    ''
    ) AS [Case]
    FROM table_01
  • using SUBSTRING():

    SELECT
    SUBSTRING(
    (CASE col1 & 1 WHEN 1 THEN ',Case 1' ELSE '' END)
    + (CASE col1 & 2 WHEN 2 THEN ',Case 2' ELSE '' END)
    + (CASE col1 & 4 WHEN 4 THEN ',Case 4' ELSE '' END),
    2,
    999999 /* not necessary to calculate LEN()-1, any value
    that is definitely greater than that will do */
    ) AS [Case]
    FROM table_01

SQL how to remove the last character of a string if it is not numeric?

With just a bit of string manipulation

Declare @YourTable Table ([PhoneNumber] varchar(50))
Insert Into @YourTable Values
('1111111')
,('1111111name')
,('1111111ext2222name')
,('1111111ex222name')

Select *
,NewValue = substring([PhoneNumber],1,len([PhoneNumber])-patindex('%[0-9]%',reverse([PhoneNumber])))
From @YourTable

Results

PhoneNumber         NewValue
1111111 111111
1111111name 111111
1111111ext2222name 1111111ext222
1111111ex222name 1111111ex22

How to remove last character from string?

Use trim():

select trim(trailing ';' from string)

SQL Remove Last Character if it is -

You can use trim() function as

mysql> select trim( trailing '-' from 'hello-');
+-----------------------------------+
| trim( trailing '-' from 'hello-') |
+-----------------------------------+
| hello |
+-----------------------------------+
1 row in set (0.00 sec)

mysql> select trim( trailing '-' from 'hello');
+----------------------------------+
| trim( trailing '-' from 'hello') |
+----------------------------------+
| hello |
+----------------------------------+
1 row in set (0.00 sec)

http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_trim

So in the query replace the hard coded hello- with your column name.

Remove first and last character if has specific character in SQL Server?

Just an another perspective by using a CASE expression and a combination of LEFT and RIGHT string functions.

Query

select case when left(@str, 1) = '#' and right(@str, 1) = '#'
then left((right(@str, len(@str) - 1)), len((right(@str, len(@str) - 1))) - 1)
when left(@str, 1) = '#' and right(@str, 1) <> '#'
then right(@str, len(@str) - 1)
when left(@str, 1) <> '#' and right(@str, 1) = '#'
then left(@str, len(@str) - 1)
else @str end as str;

Find a demo here

SQL Take off last character if a certain one exists in a string

(In case your are using SQL Server RDBMS)

You can try the following combination of right and left:

case when right(col, 1) = '/' then left(col, len(col)-1) else col end

SQLFiddle

(In case your are using MySQL RDBMS)

trim(trailing '/' from col);

SQLFiddle

How to remove first & last character of a text?

You can do:

select substr(col, 2, length(col) - 2)


Related Topics



Leave a reply



Submit