How to delete a common word from large number of datas in a Postgres table - sql

I have a table in Postgres. In that table more than 1000 names are there. Most of the names are start with SHRI or SMT. I want to delete this SHRT and SMT from the names and to save original name only. How can I do that with out any database function?

I'll step you through the logic:
Select left(name,3) from table
This select statement will bring back the first 3 chars of a column (the 'left' three). If we are looking for SMT in the first three chars, we can move it to the where statement
select * from table where left(name,3) = 'SMT'
Now from here you have a few choices that can be used. I'm going to keep to the left/right style, though replace could likely be used. We want the chars to the right of the SMT, but we don't know how long each string is to pick out those chars. So we use length() to determine that.
select right(name,length(name)-3) from table where left(name,3) = 'SMT'
I hope my syntax is right there, I'm lacking a postgres environment to test it. The logic is 'all the chars on the right of the string except the last 3 (the minus 3 excludes the 3 chars on the left. change this to 4 if you want all but the last 4 on the left)
You can then change this to an update statement (set name = right(name,length(name)-3) ) to update the table, or you can just use the select statement when you need the name without the SMT, but leave the SMT in the actual data.

Related

How can I delete rows that have 2 characters or less in a particular column?

I'm cleansing a database table so that I can build a data warehouse for my coursework, however first I need to make sure that the data is of a quality.
There are a lot of name entries have only a single letter. I want to delete those rows with a single script.
You can get the number of chars in the column name with the function length()
delete from tablename
where length(trim(name)) < 2
The function trim() could also be useful in this case.
Delete from #your_table
Where length(#your_name_column)=1
This will remove all the rows with a 1 character length name
This has not been tested but REGEXP_LIKE can be very usefull for things like this.
delete from your_table
where regexp_like (column, '[A-Z]|[a-z]')

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

insert made instead of update resulting in duplicate rows. how to fix?

I have a database where is supposed to have only one register with 2 value columns that can be filled by my web application. The values to one of the columns where given to me in an excel and we had to put it into the database. The person who did that, should had used an "update if exists else insert" but he didn't. Now, we have for some data, duplicate lines, one having just the column "valor_realizado_oficial" filled (with the column adt_login filled with 'talend' and with the key columns filled too), and another with the other column filled by the application.
So:
If exists two lines, I would like to copy the value of the column "valor_realizado_oficial" from the line with adt_login like 'talend' to the other line and delete this line.
If exists just one line, do nothing.
I tried to perform the copy part with:
update indicador_val iv
set valor_realizado_oficial=carga.valor_realizado_oficial
from (
select valor_realizado_oficial, ano, municipio_fk, indicador_fk, und_federativa_fk
from indicador_val
where adt_login like 'talend' and ano=2013 ) carga
where iv.ano=2013 and iv.municipio_fk=carga.municipio_fk
and iv.indicador_fk=carga.indicador_fk and iv.ano=carga.ano
and iv.und_federativa_fk = carga.und_federativa_fk;
But 0 rows where affected. Here's an example of a pair of lines:
id; adt_login; ano; valor_estimado, valor_realizado_oficial, indicador_fk, municipio_fk, und_federativa_fk
313885; "talend";2013;;888;2;2202;
291998;"suagenda";2013;900;;2;2202;
And I would like to have just the second, with values:
291998;"suagenda";2013;900;888;2;2202;
What I did wrong? Thanks.
In the sample data, there are empty columns. For example, the last one: und_federativa_fk.
Some of these columns are used in your joining conditions in the correlated UPDATE. If these empty values translate to the SQL NULL value, you should realize that NULL=NULL as a condition is going to be NULL, which means false in this context.
That alone might explain why no row get updated.
The solution is to use IS NOT DISTINCT FROM as the comparison operator for values that may be null.
Example:
where iv.ano=2013 and iv.municipio_fk IS NOT DISTINCT FROM carga.municipio_fk
and iv.indicador_fk IS NOT DISTINCT FROM carga.indicador_fk
etc...

Oracle SQL - Joining list of values to a field with those values concatenated

The title is a bit confusing, so I'll explain with an example what I'm trying to do.
I have a field called "modifier". This is a field with concatenated values for each individual. For example, the value in one row could be:
*26,50,4 *
and the value in the next row
*4 *
And the table (Table A) would look something like this:
Key Modifier
1 *26,50,4 *
2 *4 *
3 *1,2,3,4 *
The asterisks are always going to be in the same position (here, 1 and 26) with an uncertain number of numbers in between, separated by commas.
What I'd like to do is "join" this "modifier" field to another table (Table B) with a list of possible values for that modifier. e.g., that table could look like this:
ID MOD
1 26
2 3
3 50
4 78
If a value in A.modifier appears in B.mod, I want to keep that row in Table A. Otherwise, leave it out. (I use the term "join" loosely because I'm not sure that's what I need here.)
Is this possible? How would I do it?
Thanks in advance!
edit 1: I realize I can use regular expressions and do a bunch of or statements that search for the comma-separated values in the MOD list, but is there a better way?
One way to do it is using TRIM, string concatenations and LIKE.
SELECT *
FROM tableA a
WHERE EXISTS(
SELECT 1 FROM tableB b
WHERE
','|| trim( trim( BOTH '*' FROM a.Modifier )) ||','
LIKE '%,'|| b.mod || ',%'
);
Demo --> http://www.sqlfiddle.com/#!4/1caa8/10
This query migh be still slow for huge tables (it always performs full scans of tables or indexes), however it should be faster than using regular expressions or parsing comma separated lists into individual values.

SQL Query: Modify records based on a secondary table

I have two tables in a PostgreSQL database.
The first table contains an ID and a text field with up to 200 characters and the second table contains a data definition table which has a column that contains smileys or acronyms and a second column which converts them to plain readable English.
The number of records in table 1 is about 1200 and the number in table two is about 300.
I wish to write a SQL statement which will convert any text speak in column 1 in table one into normal readable language based on the definitions in Table 2.
So for example if the value in table 1 reads as: Finally Finished :)
The transformed SQL would be something like: Finally Finished Smiles or smiling,
where the definition is pulled from the second table.
Note the smiley could be anywhere in the text in column one and could one of three hundred characters.
Does anyone know if this is possible?
Yes. Do you want to do it entirely in SQL, or are you writing a brief bit of code to do this? I'm not entirely sure of how to do it all in SQL but I would consider something like what is below:
SELECT row.textToTranslate FROM Table_1
oldText = row.textToTranslate
Split row.textToTranslate by some delimeter
For each word in row.textToTranslate:
queryResult = SELECT FROM Table_2 WHERE pretranslate=word
if(queryResult!=Null)
modifiedText = textToTranslate.replace(word, queryResult)
UPDATE Table_1 SET translatedText=modifiedText WHERE textToTranslate=oldText