I do something like
UPDATE OR REPLACE someTable SET a=1, b=2 WHERE c=3
I expect if it doesnt exist it will be inserted into the DBs. But nothing happens and i get no errors. How can i insert data, replace it if it already exist and use a where for the condition (instead of replacing BC of a unique ID)
Careful, INSERT OR REPLACE doesn't have the expected behaviour of an "UPDATE OR REPLACE".
If you don't set the values for all fieds, INSERT OR REPLACE is going to replace them with default values, whereas with an UPDATE you expect to keep the old values.
See my answer here for an example: SQLite - UPSERT *not* INSERT or REPLACE
Try
INSERT OR REPLACE INTO [someTable] (a,b) VALUES(1,2) WHERE c = '3'
Related
I have a column C of type REAL in table F in SQLite. I want to join this everywhere where in another table the negative value of F exists (along with some other fields).
However -C or 0-C etc.. all return the rounded value of C e.g. when C contains "123,456" then -C returns "-123".
Should I cast this via a string first or is the syntax differently?
Looks like the , in 123,456 is meant to be a decimal separator but SQLite treats the whole thing as a string (i.e. '123,456' rather than 123.456). Keep in mind that SQLite's type system is a little different than SQL's as values have types but columns don't:
[...] In SQLite, the datatype of a value is associated with the value itself, not with its container. [...]
So you can quietly put a string (that looks like a real number in some locales) into a real column and nothing bad happens until later.
You could fix the import process to interpret the decimal separator as desired before the data gets into SQLite or you could use replace to fix them up as needed:
sqlite> select -'123,45';
-123
sqlite> select -replace('123,45', ',', '.');
-123.45
I made a stupid mistake and created a column like this:
CREATE TABLE mytable (mycol INTEGER, ...)
As you can see, I forgot to define a default value like "DEFAULT 0".
In my code, I need to raise the value in "mycol" by 1.
I was baffled when I found out that this code didn't have any effect.
UPDATE mytable SET mycol=(mycol+1)
The column value stays as it is. In my case "EMPTY" (=no value at all).
I would like to avoid re-creating the table if possible.
I would like to ask if there is any easy way to fix this in the SQL statement so that an EMPTY value is seen as 0 so that
UPDATE mytable SET mycol=(mycol+1)
on a column value of EMPTY would finally produce the new column value of 1.
You can use such as below if your column has null value:
UPDATE mytable SET mycol= ifnull(mycol,0)+1
With NVARCHAR data type, I store my local language text in a column. I face a problem how to query that value from the database.
ዜናገብርኤልስ is stored value.
I wrote SQL like this
select DivisionName
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
select unicode (DivisionName)
from t_Et_Divisions
where DivisionName = 'ዜናገብርኤልስ'
The above didn't work. Does anyone have any ideas how to fix it?
Thanks!
You need to prefix your Unicode string literals with a N:
select DivisionName
from t_Et_Divisions
where DivisionName = N'ዜናገብርኤልስ'
This N prefix tells SQL Server to treat this string literal as a Unicode string and not convert it to a non-Unicode string (as it will if you omit the N prefix).
Update:
I still fail to understand what is not working according to you....
I tried setting up a table with an NVARCHAR column, and if I select, I get back that one, exact row match - as expected:
DECLARE #test TABLE (DivisionName NVARCHAR(100))
INSERT INTO #test (DivisionName)
VALUES (N'ዜናገብርኤልስ'), (N'ዜናገብርኤልስ,ኔትዎርክ,ከስተመር ስርቪስ'), (N'ኔትዎርክ,ከስተመር ስርቪስ')
SELECT *
FROM #test
WHERE DivisionName = N'ዜናገብርኤልስ'
This returns exactly one row - what else are you seeing, or what else are you expecting??
Update #2:
Ah - I see - the columns contains multiple, comma-separated values - which is a horrible design mistake to begin with..... (violates first normal form of database design - don't do it!!)
And then you want to select all rows that contain that search term - but only display the search term itself, not the whole DivisionName column? Seems rather pointless..... try this:
select N'ዜናገብርኤልስ'
from t_Et_Divisions
where DivisionName LIKE N'%ዜናገብርኤልስ%'
The LIKE searches for rows that contain that value, and since you already know what you want to display, just put that value into the SELECT list ....
I have read the docs for MariaDB's REGEX_REPLACE but cannot get my query to work. I am storing links in a column, link and want to change the end of the link:
From www.example.com/<code> to www.example.com/#/results/<code> where <code> is some hexidecimal hash, e.g. 55770abb384c06ee00e0c579. What I am trying is:
SELECT REGEX_REPLACE("link", "www\\.example\\.com\\/(.*)", "www\\.example\\.com\\/#\\/results\\/\\1");
The result is:
Showing rows 0 - 0.
I wasn't able to figure out what the first argument was--the documentation says "subject". Turns out it's just the column name. So this works:
UPDATE my_table
SET my_link = REGEXP_REPLACE(
my_link,
"http:\\/\\/www\\.example\\.com\\/(.*)",
"http:\\/\\/www\\.example\\.com\\/#\\/results\\/\\1")
WHERE my_link IS NOT NULL
Let's say I have a table called "table"
So
Create Table "Table" (a int not null, b int default value 1)
If I do a "INSERT INTO "Table" (a) values (1)". I will get back 1 for column a and 1 for column b as the default value for column b is 1.
BUT if I do "INSERT INTO "Table" (a, b) values (1, null)". I will bet back 1 for column a and an empty value for column b. Is there a way to set a column's default value if a null was given?
No, if you are doing:
INSERT INTO my_table (a, b) values (1, null)
You are explicitely asking for a null value on b column.
In a RDBMS you could technically use a trigger to override that behavior. But in SQLite you can't.
If you don't want nulls for column b then you should set it as a non null-able field as you have done with a
This solution for MySQL should mostly work for SQLite. The main, and albeit major, difference is that there doesn't seem to be a Default() function in SQLite like there is in MySQL. I was able to replicate this functionality by keeping track of my database schema in PHP and then manually inserting the default value as the second argument to Coalesce(). See this Gist for example code.
If the column with the default value can have the NOT NULL constraint, then you can:
create the table using this syntax:
CREATE TABLE "Table" (a INT NOT NULL, b INT NOT NULL ON CONFLICT REPLACE DEFAULT 1);
and insert as usual
create the table as in your question
and insert using this syntax:
INSERT OR REPLACE INTO "Table" (a, b) VALUES(1, null);
https://database.guide/convert-null-values-to-the-columns-default-value-when-inserting-data-in-sqlite/
If the column with the default value can not have the NOT NULL constraint (allowing NULL to be inserted), as in your question:
you will have to omit the column with the default value from the insert query so that it gets its default.
Ideal would be:
INSERT INTO "Table" (a, b) VALUES(1, COALESCE(NULL, DEFAULT))
, as is in other sql dialects, which might be supported in future release:
https://sqlite.org/forum/info/d7384e085b808b05