Postgresql, Delete characters within strings - sql

I am new to PostgreSQL and am attempting to create a query that will clean up certain columns within a table. By "clean up" I mean delete all characters before or after a certain symbol.
I have one column called "Campaign" within a table called "Adwords".The column has this string/structure within all rows, DE-SRC-Brand. The only place that I would like to keep in this column would be where Brand currently is. I had previously done this manually through excel by doing a simple search and replace with "*-" as the searching criteria.
How would this formula translate to PostgreSQL? How would it then also change if I would like to delete characters in front of a certain designated symbol. Please let me know if anything is unclear as I am still new to this program.

If you want to extract the brand, you can use substring() with a pattern:
select substring(campaign from '%-%-#"%#"' for '#') as brand
from adwords;
You can incorporate this into an update:
update adwords
set campaign = substring(campaign from '%-%-#"%#"' for '#')
where campaign like '%-%-%';

Related

how to get query criteria in access vba

Now I am trying to create customize query design view form with Access VBA. I want to show fields name and criteria of saved query.
For example, SQL statement that executed by QueryDef.sql is
SELECT AllCars.ID, AllCars.CarName
FROM AllCars
WHERE (((AllCars.ID) Between 15 And 25) AND ((AllCars.MinPay)<1000)) ;
I would like to get criteria expressions( ID = Between 15 And 25, MinPay = <1000) and field names(ID , CarName) from it.
I can get field name but I am not sure how to get query criteria expression in Access VBA.
I tried to split sql statement but I think this is not a best way for multiple criteria expressions.
If getting criteria in access vba may be possible, could someone help me?
Thanks in advance.
Change the code that creates the saved query: instead of saving all the SQL together, save the field names separately and the expressions separately.
To keep all the fields together but in a way that you can later separate them, put the character ";" between the field names, and to separate them you will use the split function.
You can do the same for the expressions, and when you read the saved data you associate the position in the array of the expression with the position of the field in the array and thus you connect them together to show the user which expression applies to which field.
In the same way you build the query

Want to find fields in a column that start with certain letters

I'm looking to find a way fields that only start with certain letters
Example: Within the "Postcode" column on the report, I want to show only postcodes that start with "CF"
My best guess would be to create an object that marks these down as a number, but I don't know how to identify them.
Thanks for any advice.
In the case of Webi.
You can create variable, wich helds only symbols you are interested in. In your example it would be:
f=left([Postcode];2)
Then restrict table in the report by filter. And add values you want to filter list.

Remove a string inside a cell in MS Access

I want to delete a certain string in a cell in MS Access, whether in a column or in the whole table. I know that I can do this using plain old Find and Replace but in this case it is not economical for my thousand-row table.
For example,
remove all Unknown values from all columns.
remove the string "dollars" from the values in column price ie. if the cell contains "34 dollars", it will just be "34".
Can this be done in SQL and how?
Assuming your query will run within an Access session, the second goal is easy. You can Replace dollars with a zero-length string.
UPDATE YourTable
SET price = Replace(price, 'dollars', '');
You could use the same strategy for the first goal, but may decide it makes more sense to examine the datatypes of the table's fields and only UPDATE those which are text or memo.

Find or Strip Invalid characters from Database

We are using a database where the front end software has allowed the input of invalid characters. (I have no control or re-writing of the software.)
The types of characters are carriage returns, line breaks, �, ¶, basically anything that is not 0-9, a-z or standard punctuation causes us issues with the database and how we use the data.
I'm looking for a way to scan the entire database to identify these invalid codes and either display them as results or strip them out?
I had been looking at This site wondering if there was a way of searching for a certain range? But I might be barking up the wrong tree.
I'm fairly new to SQL so be gentle with me, thanks.
The only way I could think to do this would be to write a stored procedure which uses system tables to get a list of all fields in the database/schema in question. Have it exclude system tables (or only include those that are user defined) then dynamically write out SQL update statements based on the columns/tables found in the system table inquiries. Using regular expressions or character removal like in this article
The system tables in question are:
SELECT
table_name,column_name
FROM
information_schema.columns
Psudo code would be:
Get list of tables we want to do this for
For each table in list
get list of columns for table that have string data.
For each column in table
generate update statement to strip unwanted characters
--Consider writing out table, column key, before after values to history table. incase this
has to be undone.
--Consider counter so I have an idea of what was updated
execute updatestatement
next column
next table
write out counter
Since you say
the data then moves to a second program that cannot handle these
characters and this causes the process to fail.
I'm wondering if you can leave the unreadable data where it is and create a new column for changed data that's only populated if/when the 2nd process fails. You'll still have to test every character of the data in the failed cell, but you wouldn't have to test every character of every row. After you determine the updated text to process, you can call the 2nd process again with the updated value.

Searching Sqlite

I was wondering if there was a way to search an entire SQLite database for one specific word. I do not know the column that it is in or even the table that it is in.
The table and row/column that contains this specific word also contains the other entries that i need to edit.
In-short:
Need to find a specific word
Can't query (i don't think i can atleast) since i don't know the table or column name that its located in.
I need to know where this specific word is referenced. In what table and row so I can access the others that are along side it.
Basically, is there a CTRL+F functionality of SQlite that searches the entirety of the SQLite file?
I have mac/windows/linux machines. I am not limited by software if that is a solution.
Any such functionality would essentially be running queries that check every column of every table. You can do that via a script that runs the following SQL:
1) Get a list of all the tables:
select name from sqlite_master where type = 'table'
2) For each table, get all of its columns (column name is available in the name field)
pragma table_info(cows)
3) Then for each table, generate a query that checks every field and run it:
select
*
from cows
where name like '%Daisy%'
or owner like '%Daisy%'
or farm like '%Daisy%'