How do I remove unknown characters in a string?

Arif

I would like to delete parts of an string.

We have a Table: Locations

mk-MK=New York; sq-AL=Nej York; en-US=New York

mk-MK=London; sq-AL=London; en-US=London

mk-MK=Paris; sq-AL=Paris; en-US=Paris

I Want to remove everything and keep only sq-AL=LocationName.

I want the result to be:

sq-AL=Nej York;
sq-AL=London;
Zohar Peled

This is yet another example of the importance of normalized databases.
In a normalized database you would have a table with 2 columns, one for the culture (sq-Al, en-US etc`) and one for the value. I would go a step further and have the cultures in a lookup table.

However, since this is not the case you have to use string manipulations to get the value of a specific culture. you can use SUBSTRING and CHARINDEX to find the specific pattern you want.
This will work in any of the cases represented by the sample data I've listed.

-- Create the table and insert sample data
CREATE TABLE Location ([Name] varchar(100))
INSERT INTO Location ([Name]) VALUES 
('en-US=Huston; mk-MK=Huston; sq-AL=Huston;'), -- end of the row, with the ending ';'.
('en-US=New York; mk-MK=New York; sq-AL=Nej York'), -- end of the row, without the ending ';'.
('mk-MK=London; sq-AL=London; en-US=London'),  -- middle of the row
('sq-AL=Paris; en-US=Paris; mk-MK=Paris') -- begining of the row



SELECT  SUBSTRING(Name, 
        CHARINDEX('sq-AL=', Name), -- index of 'sq-AL='
        CASE WHEN CHARINDEX(';', Name, CHARINDEX('sq-AL=', Name)) > 0 THEN -- If there is a ';' after 'sq-AL='.
            CHARINDEX(';', Name, CHARINDEX('sq-AL=', Name)) -- index of the first ';' after 'sq-AL=' 
            - CHARINDEX('sq-AL=', Name)  -- index of the first ';' - the index of 'sq-AL=' will give you the length for `Nej York`
        ELSE 
            LEN(Name) 
        END
    ) + ';'
FROM Location

-- Cleanup
DROP Table Location 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I remove unknown characters in a string?

From Dev

How do I remove multiple characters in a string

From Dev

How do I remove multiple offending characters from my string?

From Dev

How do I remove new line characters from a string?

From Dev

how do i remove the first characters of a string [python]

From Dev

How do I remove the first n characters from a string in c?

From Dev

How do I remove the last characters from a string?

From Dev

For strings, how do I remove repeated characters?

From Dev

For strings, how do I remove repeated characters?

From Dev

In an expect script, how do I remove a set of special characters from a string variable?

From Java

How do I remove all non alphanumeric characters from a string except dash?

From Dev

How do I remove the last line of a string that has visible characters in ruby?

From Dev

How do I convert a SQLCHAR[200] type to std::string without remove characters?

From Dev

How do I remove copyright and other non-ASCII characters from my Java string?

From Dev

How do I remove 1 instance of x characters in a string and find the word it makes in Python3?

From Dev

How do I check for string breaking characters?

From Dev

How do I split a String by Characters in Java?

From Dev

How do I check for string breaking characters?

From Dev

How do I remove CSS from a string?

From Dev

How Do I Remove A Vowel From String

From Dev

How do i remove chars from a string?

From Dev

How do I remove punctuaution from a string

From Dev

How do I remove specific input in string

From Dev

how do i remove a letter from string

From Dev

How can I remove escape characters from string? UTF issue?

From Dev

How can I remove the escape characters from this string?

From Dev

How can I format a string to remove certain characters (.NET Razor)?

From Dev

How can I remove certain characters from a string?

From Dev

How can I remove or replace all punctuation characters from a String?

Related Related

  1. 1

    How do I remove unknown characters in a string?

  2. 2

    How do I remove multiple characters in a string

  3. 3

    How do I remove multiple offending characters from my string?

  4. 4

    How do I remove new line characters from a string?

  5. 5

    how do i remove the first characters of a string [python]

  6. 6

    How do I remove the first n characters from a string in c?

  7. 7

    How do I remove the last characters from a string?

  8. 8

    For strings, how do I remove repeated characters?

  9. 9

    For strings, how do I remove repeated characters?

  10. 10

    In an expect script, how do I remove a set of special characters from a string variable?

  11. 11

    How do I remove all non alphanumeric characters from a string except dash?

  12. 12

    How do I remove the last line of a string that has visible characters in ruby?

  13. 13

    How do I convert a SQLCHAR[200] type to std::string without remove characters?

  14. 14

    How do I remove copyright and other non-ASCII characters from my Java string?

  15. 15

    How do I remove 1 instance of x characters in a string and find the word it makes in Python3?

  16. 16

    How do I check for string breaking characters?

  17. 17

    How do I split a String by Characters in Java?

  18. 18

    How do I check for string breaking characters?

  19. 19

    How do I remove CSS from a string?

  20. 20

    How Do I Remove A Vowel From String

  21. 21

    How do i remove chars from a string?

  22. 22

    How do I remove punctuaution from a string

  23. 23

    How do I remove specific input in string

  24. 24

    how do i remove a letter from string

  25. 25

    How can I remove escape characters from string? UTF issue?

  26. 26

    How can I remove the escape characters from this string?

  27. 27

    How can I format a string to remove certain characters (.NET Razor)?

  28. 28

    How can I remove certain characters from a string?

  29. 29

    How can I remove or replace all punctuation characters from a String?

HotTag

Archive