Replace new line from the end of string SQL and update columns - sqlite

I have a table called Players with two columns Name and PlayerID. I am using SQLite under DB Browser for SQLite.
Unfortunately, all my player's names have a something like a "\n" (a newline) at the end of their Name.
Ex:
"Mark
"
I tried to use Update & Replace for all the names with the following query (I have like 450 rows in the table):
UPDATE Players
SET Name = REPLACE(Name,CHAR(10),'')
WHERE PlayerID <= 500
When I execute something like:
SELECT * FROM Players
WHERE Players.Name LIKE 'Mark'
it'll return no rows because of the end line. Here 'Mark' has no "\n", so it won't be found.
If I execute:
SELECT * FROM Players
WHERE Players.Name LIKE 'Mark
'
it will return my player. (after Mark I pressed enter)
I want to change all my rows from this format
"Mark
"
to this
"Mark"
and save all the changes.
How can I solve my problem? What's wrong?

Solution
The problem was that I had /r at the end of each string, not \n. So I had to use CHAR(13) instead of CHAR(10).
UPDATE Players
SET Name = REPLACE(Name, CHAR(13), '')
Also to remove all line feed characters (\n) I used:
UPDATE Players
SET Name = REPLACE(Name, CHAR(10), '')
Moreover to remove all the spaces () I used:
UPDATE Players
SET Name = REPLACE(Name, ' ', '')

Related

How do safely I add a raw string to a query?

My SQLite query :
let data = db.getAllRows(sql"""
SELECT name, source, uploaded_at, canonical_url, size
FROM table
WHERE name like ?
ORDER BY ? DESC
LIMIT ?
OFFSET ?
""", &"%{query}%", order, limit, offset)
Nim adds single quotes to any string replacing ?. I can manually build the SQL string and then use sql(string), but the input then isn't escaped. Is there some other token apart from ? that does not add '?
To answer your title question: "How to safely add a raw string to a query", you can use dbQuote:
import db_sqlite
let
a = "unes'caped %' string"
b = "my prefix ->"
c = "<- my suffix"
d = b & dbQuote(a) & c
echo d
This will print my prefix ->'unes''caped %'' string'<- my suffix, adding quotes before/after and escaping any ones inside. This string is presumably safe to pass as an sql statement. You should get the same result as if you had used ? with extra parameters.

Remove a part of string containing another field in SQLite

I need to remove a part of string in one column corresponding to another column.
I know I can use the REPLACE function for that, but not sure how to use it.
So, in my case, I want to remove the first part of the "name" column that is the same as the winery column.
Example:
Name: Family Wines Vintage Special
Winery: Family Wines
I want to obtain:
Name: Vintage Special
Winery: Family Wines
Possible problems:
the function needs to do nothing if Name and Winery are the same.
don't keep the space at begin of Name field after removing
if possible, clean the Name string if starts with coma (') or semicolon (;) after the removing OR use another query for that
Something like that:
UPDATE usr_wines SET name=REPLACE(name, winery, '') WHERE name LIKE '%' || winery;
Thanks,
To remove a character from the beginning of a text value, use the substr() function:
UPDATE usr_wines
SET Name = substr(Name, 2)
WHERE Name LIKE ',%';
The same can be done with a dynamic string:
UPDATE usr_wines
SET Name = substr(Name, length(Winery) + 1)
WHERE Name LIKE Winery || '%'
AND Name != Winery;

select the union of several tables together in a single step

I am very new to Oracle 11g and am trying to generate a large string by appending text for each column in a select statement and using a cursor to store the results. However I want the last statement to not have a union all included. The final result I want to build large string of each row generated or simply execute the result if possible.
Note: column1 has a list of schemas that I am interested in.
select 'select * from ' || column1 || '.' || column2 || ' union all ' from mytable
This is where column1 is the schema, column2 is the table name.
What is the simplest way to generate the final string without using rtrim to remove the last string. And is there a simple way to append all these rows together in the string automatically?
The final goal is to actually just execute the union into a resulting cursor.
If you're querying in a loop anyway I wouldn't try to construct the string as part of the select at all; I'd do it all within the loop. Something like (untested):
declare
str varchar2(32768);
begin
for rec in (select column1, column2, rownum as rn from mytable)
loop
if rec.rn > 1 then
str := str || ' union all ';
end if;
str := str || 'select * from "' || rec.column[ || '"."' || rec.column2 ||'"';
end loop;
-- do something with str e.g. display to verify the syntax
-- before using in a cursor
dbms_output.put_line(str);
end;
Rather than adding union all to the end of every row except the last one,the rn check means it's added to the start of every row except the first one, which is easier to detect.
I've also wrapped the schema and table names in double quotes, just in case you have to deal with any quoted identifiers. But if your stored values don't match the case of the owners and table names in all_tables this will cause a problem rather than solve it.

SQLite: replace part of a string with another one

I have a column named "tel". This column has some records that start with (for example) "0511" and I want to change it "0513". What is proper query?
You can use replace function
UPDATE table SET tel= replace( tel, '0511', '0513' ) WHERE tel LIKE '0511%';
Check the SQLite core functions here
EDIT
In case you have a match after the first 4 digits of your tel number.
For example 051121505113
UPDATE table SET tel= replace(substr(tel,1,5), '0511', '0513' )||substr(tel,5)
WHERE tel LIKE '0511%';
UPDATE table
SET tel = REPLACE(tell, '0511', '0513')
WHERE tel like '%0511%'
In case there's a risk that '0511' exists within the rest if the string you can make sure to only replace 0511 at the start of the string like this:
UPDATE [table]
SET [tel] = CASE WHEN
instr([tel], '0511') = 1 THEN
'0513' || substr([tel], 4)
ELSE
[tel]
END
WHERE [tel] LIKE '0511%';

how to sanitize data in sqlite3 column

I have a sqlite3 table with a column by the name Title, which stores the names of the some movies.
Table name - table1
Column name - Title
Examples data: "Casablanca" (1983) {The Cashier and the Belly Dancer (#1.4)}
I have another sqlite3 table with a column that stores movie titles.
Table name - table2
Column name - Title
Examples data: casa blanca
Both these tables were created using different datasets, and as such although the movie name is the same (casa blanca vs "Casablanca" (1983) {The Cashier and the Belly Dancer (#1.4)}), both are stored with extra text.
What i would like to do is to SANITIZE the already stored data in both the columns. By sanitization, I would like to strip the cell content of:
1. spaces
2. spl chars like !, ', ", comma, etc..
3. convert all to lower case
I hope with that atleast some level of matching can be had between both the columns.
My question is, how do i perform these sanitizations on data that is already stored in sqlite tables. I do not have an option to sanitize before loading, as i only have access to the loaded database.
I am using sqlite 3.7.13, and i am using sqlite manager as the gui.
Thank You.
This task is too specialized to be done in SQL only.
You should write simple Perl or Python script which will scan your table, read data row by row, scrub it to meet your requirements and write it back.
This is example in Perl:
use DBI;
my $dbh = DBI->connect("dbi:mysql:database=my.db");
# replace rowid with your primary key, but it should work as is:
my $sth = $dbh->prepare(qq{
SELECT rowid,*
FROM table1
});
while (my $row = $sth->fetchrow_hashref()) {
my $rowid = $row->{rowid};
my $title = $row->{title};
# sanitize title:
$title = lc($title); # convert to lowercase
$title =~ s/,//g; # remove commas
# do more sanitization as you wish
# ...
# write it back to database:
$dbh->do(
qq{
UPDATE table1
SET title = ?
WHERE rowid = ?
}, undef,
$title,
$rowid,
);
}
$sth->finish();
$dbh->disconnect();

Resources