HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Remove the last character from Variable String

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
lastthecharacterremovefromvariablestring

Problem

I have a few variable data in the column "email".

For example:

roy2087@gmail.com123
sg123@yahoo.compori


How do I get rid of the characters after .com?

I used Replace(email,'.com%',gmail.com), however, we cannot use wildcard characters in Replace.

Any Suggestions?

Solution

SELECT
    SUBSTRING(col, 1, CHAR_LENGTH(col) - CHAR_LENGTH( SUBSTRING_INDEX(col, '.com', -1) ) ) ;


If you want update the strings from a table, chopping off what is on the right of .com:

UPDATE 
    tableX
SET
    col = SUBSTRING(col, 1, CHAR_LENGTH(col) - CHAR_LENGTH( SUBSTRING_INDEX(col, '.com', -1) ) ) 
WHERE
    SUBSTRING_INDEX(col, '.com', -1) <> col ;

Code Snippets

SELECT
    SUBSTRING(col, 1, CHAR_LENGTH(col) - CHAR_LENGTH( SUBSTRING_INDEX(col, '.com', -1) ) ) ;
UPDATE 
    tableX
SET
    col = SUBSTRING(col, 1, CHAR_LENGTH(col) - CHAR_LENGTH( SUBSTRING_INDEX(col, '.com', -1) ) ) 
WHERE
    SUBSTRING_INDEX(col, '.com', -1) <> col ;

Context

StackExchange Database Administrators Q#22153, answer score: 3

Revisions (0)

No revisions yet.