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') , '\.(.*)', '');
Related
I have a column as varchar2 datatype, the data in it is in format:
100323.3819823.222
100.323123.443422
1001010100.233888
LOL12333.DDD33.44
I need to remove the whole part after the first occurrence of '.'
In the end it should look like this:
100323
100
1001010100
LOL12333
I cant seem to find the exact substring expression due to the fact that there is not any fix length of the first part.
One way is to use REGEXP_SUBSTR:
SELECT REGEXP_SUBSTR(column_name,'^[^.]*') FROM table
The other way is to combine SUBSTR with INSTR, which is a bit faster, but will result in NULL if the data doesn't contain a dot, so you'll have to add a switch if needed:
SELECT SUBSTR(column_name, 1, INSTR(column_name,'.') - 1) FROM table
For oracle you can try this:
select substr (i,1,Instr(i,'.',i)-1) from Table name.
I am trying to automate the data input of a adress data base however some of the housenumber have a letter added to the end of them. Because my database keeps the letter and the number separate i need to somehow split these 2 up. I know that i need a substring for this. My question is however how do i get the location of the first letter.
for example 17b
would give me 3
where 259a
would give me 4
You should consider using REGEXP_INSTR or REGEXP_SUBSTR as the standard INSTR and SUBSTR functions won't have the kind of flexibility that you need.
In your case, you would use something like REGEXP_INSTR(column_name, '[^0-9 ]').
Use regexp_replace for both numbers or letters
select regexp_replace('259a', '[A-Za-z]') from dual;
select regexp_replace('259a', '[0-9]') from dual;
FIDDLE
I am using this query to replace one character in a cell
select replace(id,',','')id from table
But I want to replace two characters in a cell.
If the cell is having this data (1,3.1), and I want it to look like this (131).
How can I replace two different characters in one cell?
Use TRANSLATE instead of REPLACE(). It replaces each occurrence of a character in the first pattern with its matched character in the second. To remove characters, simply leave cut short the replacement string:
select translate(id, '1,.', '1') id from table
Note that the second string cannot be null. Hence the need to include 1 (or some other character) in both strings.
Find out more.
Obviously the more characters you need to convert/remove the more attractive TRANSLATE() becomes. The main use for REPLACE is changing patterns (such as words) rather than individual characters.
Can use
select replace(translate(id,',.',' '),' ','') from table;
or
select regexp_replace('1,3.1','[,.]','') from dual;
or
select replace(replace(id,',',''),'.','') from table;
Call the replace again.
select replace(replace(id,',',''), '.','') id from table
Do this:
select REPLACE(REPLACE(id,',',''),'.','')
Or use a regular expression:
select regexp_replace(id, '[.,]', '') id from table
Find out more
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.
In my DB, I store various version numbers, like the following :
OBJNAME
Fix_6.0.0a.1
Fix_6.0.0a.2
I would like to sort them not according to last version element (the number behind the last . character. How do I write such SQL statement ?
I guess it's something like:
SELECT SUBSTR(INSTR(OBJNAME, ".", -1)) as LAST_VERSION, OBJNAME
FROM MY_TABLE
ORDER BY LAST_VERSION
But what is the exact syntax?
The correct version is
select TO_NUMBER(SUBSTR(OBJNAME,INSTR(OBJNAME,'.',-1)+1,LENGTH(OBJNAME))) as LAST_VERSION, OBJNAME from MY_TABLE order by LAST_VERSION
i dont know which sql-software you are using, but you should have a look at substr and instr parameters. in you case you are passing 3 parameters to instr and 1 parameter in substr. but substr normally requires more. insert some blanks to get an overview of your statement, especially for the substr.... as LAST_VERSION. then you will see. wrong param count.