I am trying to pass names with accents, for example: sŕodigër to names without accents: srodiger.
I'm sure there has to be a way to do it, but I didn't find it.
I'm trying this, but it's crazy to do it with consonants too:
concat(CASE WHEN m.first_names IS NOT NULL THEN
replace(name, 'âãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮ',
'aaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuu')
ELSE '' END
How could I do it?
Pls use translate().
concat(CASE WHEN m.first_names IS NOT NULL THEN
translate(name, 'âãäåāăąÁÂÃÄÅĀĂĄèééêëēĕėęěĒĔĖĘĚìíîïìĩīĭÌÍÎÏÌĨĪĬóôõöōŏőÒÓÔÕÖŌŎŐùúûüũūŭůÙÚÛÜŨŪŬŮ',
'aaaaaaaaaaaaaaaeeeeeeeeeeeeeeeiiiiiiiiiiiiiiiiooooooooooooooouuuuuuuuuuuuuuuu')
ELSE '' END
In below example, it successfully replaced ascents with whatever you want to replace them with.
Example
Related
I am attempting to generate a new field based on an existing field where some of the entries contain different special characters. (ie. *, ') The special characters are at the end of the string, which is either second or third position.
I am NEW to SQL but not new to data. I am using a CASE WHEN statement. I have tried several approaches and several other commands within the CASE statement.
what I want is:
SELECT *
CASE WHEN grde_code_mid LIKE '[*]' THEN 'Remedial'
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble
I keep getting the same error: "FROM keyword not found where expected". I expect to have all 3 returns in the new field.
If you're looking for ' character you should escape it.
Change
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
by
WHEN grde_code_mid LIKE '['']' THEN 'Continuing'
Have a look at this question: How do I escape a single quote in SQL Server?
There are several issues with your query:
you are missing a comma in the SELECT clause between * and the CASE expression (this is causing the error that you are currently getting)
the bracket notations is only supported in SQL-Server; if you want to match on strings that end with * in a portable manner, you need expression ... LIKE '%*', not LIKE '[*]'
single quotes embedded in a string need to be escaped
SELECT *, other_field FROM ... is not supported on all RDBMS (and, as commented by jarhl, actually isn't standard ANSI SQL notation); you usually need to prefix the * with the table name or alias
Consider:
SELECT
g.*,
CASE
WHEN grde_code_mid LIKE '%*' THEN 'Remedial'
WHEN grde_code_mid LIKE '%''' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble g
Thank you all. I am still getting the hang of the language and proper terms. I am using Oracle DB. Also, yes, I did need to reference the table in the select statement (SELECT tablename.*). Found a work around:
CASE WHEN regexp_like(grde_code_mid, '[*]') THEN 'Remedial'
WHEN regexp_like(grde_code_mid, '['']') THEN 'Continuing'
ELSE NULL END AS special_class
I am working with a PostgreSQL database. I have a column that contains rows that look like this:
I am to replace the 'PC' text with '00000' and then remove the period and all text after.
So for example, row 5 should look like 0000055000 after the transformation.
I was able to the 'PC' with the overlay function. So my current query looks like this:
select set_name, overlay(set_name placing '00000' from 1 for 2 ) FROM
src.sap_setnode
WHERE set_name LIKE'PC%'
From there how can I remove the period and everything after it?
Thanks
I actually found the answer - if anyone has a similar question. The split_part function can be used to split the field by using '.' as a delimiter and then grabbing the first part.
SELECT
split_part( overlay(set_name placing '00000' from 1 for 2 ),'.',1) FROM
src.sap_setnode
SELECT SPLIT_PART(REPLACE(set_name ,SUBSTR(set_name ,1,2),'00000'),'.',1) FROM t WHERE set_name LIKE 'PC%' OR set_name NOT LIKE 'PC-%' OR set_name NOT LIKE 'PCA%'
Try to use regular expression, for example:
UPDATE TABLE SET set_name = regexp_replace(set_name, '^PC(.+)\..+$', '00000\1');
I'm not entirely sure that this covers all the corner cases you might encounter, so you might need to tweak the regex a bit :)
Also documentation for regex_replace:
http://www.postgresql.org/docs/9.1/static/functions-string.html
using regexp_replace:
select regexp_replace (regexp_replace(set_name,'^PC','00000') , '\.(.*)', '');
I am retreiving information from a BES (Blackberry Enterprise Server). I need to change the Display Name field from the BES to a name.name format.
The Display name comes across as First Last and possible First Last-EXT for example. I need to drop the dash after any last name with it, and also add a . between first and last.
I have tried using REPLACE to add the .
And I have tried CASE to remove the - and everthing after it. But how can I combine into one statement to do both?
,CASE WHEN CHARINDEX('-',[DisplayName])>0 THEN LEFT([DisplayName], CHARINDEX('-',[DisplayName])-1) ELSE [DisplayName] END
,REPLACE([DisplayName], ' ','.') AS 'Display Name'
edit : added code block
Try below code.
you have done all the work.
in your case statement you return the DisplayName that you want (removing '-' from lastname and every character following '-') so use that output and apply replace to replace your blank space to '.'
CASE WHEN CHARINDEX('-',[DisplayName])>0
THEN REPLACE(LEFT([DisplayName], CHARINDEX('-',[DisplayName])-1), ' ','.')
ELSE REPLACE([DisplayName], ' ','.')
END
Not sure if it is possible to do with trim in a select statement:
Basically want to trim anything characters in a string BEFORE a character is found:
say there are values (names) Oliver, Dave and I want to trim all characters before 'v' is found so those values would be 'ver' and 've' afterwards
Again it kind of sounds too complicated for a trim statement unless there is an easy way to combine it with a LIKE to search for such a thing.
May be getting ahead of myself here but would be a useful thing if anyone can help enlighten it :)
Maybe something like
SELECT REGEXP_SUBSTR('David', 'v.*') from dual
WHERE REGEXP_LIKE('David', 'v.*')
Description about REGEXP_SUBSTR: http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions131.htm
REGEXP_LIKE: http://docs.oracle.com/cd/B14117_01/server.101/b10759/conditions018.htm
You could do it with REGEXP, or with SUBSTR/INSTR:
SELECT CASE WHEN INSTR(mycolumn,'v') > 0
THEN SUBSTR(mycolumn,INSTR(mycolumn,'v'))
ELSE mycolumn
END
FROM ....
I'm probably missing something really obvious here, but this has been a bear to search for on Google (Maybe I don't have the right terminology).
I want to replace an unknown value with another value from a temp table. I know the length of the value so my thought was to use underscores as you would in a LIKE statement. The following DOES NOT work however:
UPDATE MyTable
SET Name =
Replace(Name, '__SomeString', TempTable.value + ' SomeString')
FROM MyTable INNER JOIN TempTable
ON Name LIKE TempTable.Name
This is MS SQL 2000 FWIW.
EDIT: To try and clarify it looks like the underscore '_' wildcard that is used in a LIKE statement is taken literally inside of the replace function. Is there another way?
Any thoughts?
UPDATE MyTable
SET Name =
CASE WHEN (Name like '_SomeString')
THEN TempTable.value + SUBSTRING(Name,2,LEN(Name)-1)
ELSE Name END
FROM MyTable INNER JOIN TempTable
ON MyTable.Name = TempTable.Name
WHERE MyTable.Name = 'TheNameToReplace' -- I don't know if it will be for a specific name hence the where...
This will then replace 'SomeString' in the Name field, with the value from TempTable.value
Is this what you were looking for or something else?
Perhaps you can use stuff instead of replace. You need to know the start position in the string where you want to replace the characters and you need to know the length of the expression that is to be replaced. If you don't know that perhaps you can use charindex or patindex to figure that out.
select stuff('A123', 1, 1, 'B ')
Result:
(No column name)
B 123
Would somethi8ng like this work?
UPDATE mytable
SET field1 = 'A' + SUBSTRING(field1,2,LEN(field1))
WHERE LEFT(field1) IN (0,1,2,3,4,5,6,7,8,9)
Apparently it is not possible to use wild cards in the REPLACE function. This is the closest match on SO that I could find: MySQL Search & Replace with WILDCARDS - Query While the link is for MySQL I believe it is true for MS SQL as well.
The other answers here are all creative solutions to the problem, but I ended up going the brute force route.