Query in mariadb returns error results.
To reproduce the problem:
Create the following table:
CREATE TABLE test_table (
ID int(4) NOT NULL AUTO_INCREMENT,
test_field double(20,2) DEFAULT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
Insert some values:
INSERT INTO test_table VALUES ('1', '1.12');
INSERT INTO test_table VALUES ('2', '1.13');
INSERT INTO test_table VALUES ('3', '1.14');
INSERT INTO test_table VALUES ('4', '1.15');
INSERT INTO test_table VALUES ('5', '2.24');
Run this query:
SELECT test_field FROM test_table WHERE test_field = '1.14'
The Maria db returns nothing.
If I use '1.13' instead of '1.14', mariadb returns correctly one record.
It is understood that I should not use single quotes around the value but I would expect that the result would be correct, since the single quotes are not forbitten. It seems as a the type casting problem which I believe it has to addressed.
Do not use (m,n) on DOUBLE or FLOAT. It causes an extra rounding.
Because DOUBLE and FLOAT are not exact decimal numbers it is folly to compare a DOUBLE to a DECIMAL.
1.14 is converted to binary. But when converted back to decimal you get something close to 1.1400000000000001.
Bottom line: Don't use = for testing DOUBLE or FLOAT values. And don't use (m,n) with them.
Related
If it is a string column, I can insert null or empty value with '', but what about the decimal column, Null or -99999? Thanks so much for any advice.
You can insert null values into the table with a column type integer. Empty_string and null is the same in oracle.
Eg:
create table t(x int)
insert into t values('')
insert into t values(null)
select count(*),count(x)
from t
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=e7653cf554a9e19a8ae47f1c101218ff
NULL is nothing but the value that is missing or not available.
You can insert null in any null-allowed column in database regardless of its data type. For string '' (empty value) is considered as null. There is no such stuff for number.
Create table test
(id number,
Name varchar2(10),
Address clob,
Zip integer);
Insert into test
Values(NULL, NULL, NULL, NULL)
Cheers!!
You cannot insert empty value ('') which will throw the error following error, instead you can insert NULL values.
converting data type varchar to numeric.
INSERT INTO TBL (DeciClm) VALUES(NULL)
I'm having a hard time creating a simple table:
CREATE TABLE `csat` (
`csat_id` INT NOT NULL AUTO_INCREMENT,
`value` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`csat_id`)
);
CREATE TABLE `migrated` (
`migrated_id` INT NOT NULL AUTO_INCREMENT,
`title` INT,
`description` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`migrated_id`)
);
INSERT INTO csat
VALUES (1, 1, 2017-06-15);
INSERT INTO migrated
VALUES (1, 2, 2018-06-15);
I get the error:
Data truncation: Incorrect date value: '1996' for column 'month' at row 1
It seems like my date is in the right format:
https://www.w3schools.com/sql/func_mysql_date.asp
I'm also wondering why I need to specify a value on the csat_id, because I thought SQL would just put that in for me since its the primary key.
You have to wrap your date values in single quotation marks: '2017-06-15', not 2017-06-15. Right now, MySQL is evaluating this as 2017 minus 6 minus 15, which comes to 1996.
Also, when inserting, it's best to specify the columns you're inserting into. And if your column is set to AUTO_INCREMENT, you don't need to specify it:
INSERT INTO csat
(`value`, `month`)
VALUES
(1, '2017-06-15');
I would also consider changing your column names. Perhaps make "value" more descriptive (value of what?) And month is misleading, since it's actually a date-type column.
You haven't said which database server you're using, but generally speaking dates are inputted as strings.
You should try the following inserts;
INSERT INTO csat (`csat_id`, `value`, `month`)
VALUES (1, 1, '2017-06-15');
INSERT INTO migrated (`migrated_id`, `title`, `description`, `month`)
VALUES (1, 2, 2, '2018-06-15');
Also, you should specify which columns you're inserting into. This prevents data from being entered into the wrong fields, especially when schema changes occur.
SQL does auto increment primary key fields (if defined that way). However, you had to define it in your insert statements because you didn't specify the columns you were inserting to.
Try this instead;
INSERT INTO csat (`value`, `month`)
VALUES (1, '2017-06-15');
INSERT INTO migrated (`title`, `description`, `month`)
VALUES (2, 2, '2018-06-15');
I guess you missed the single qoutes (as per Sql standards) at first in your date and then while inserting even if the column is autoincrement you need to specify columns other than the autoincrement column so as to make sure the data you are inserting belongs to that specific column or not
Try this
INSERT INTO
csat(value,month) values
(1,'2017-06-15')
I have been working on my SQL database for school project and I have a problem with my idea. I am doing a car service database (all made up) and I have a column deadline of datatype date for a table order (names for both in my language - 'rok' and 'narocilo') and I am trying to insert null into some of rows where deadline isn't specified by the customer.
create table narocilo
(
id_narocila integer NOT null,
opis varchar(50),
cena integer,
status varchar(20),
datum_narocila date,
rok date
);
This is some ways i tried to insert null but none of them worked
to_date('dd-mm-yyyy', null)
to_date(null)
null
to_date('null')
So, my question is, can I insert null into a date column and if yes, how?
Thank you in advance!
You have to insert an entire row's worth of data, but as long as you have not defined that field as NOT NULL like you have above, it should be fine to insert NULL values into the date fields:
INSERT INTO narocilo(id_narocila, opis, cena, status, datm_narocila, rok)
VALUES (1001, 'something', 6, 'Open', NULL, NULL)
A very simple way to insert NULL is to leave them out of the insert altogether:
INSERT INTO narocilo (id_narocila, opis, cena, status)
VALUES (?, ?, ?, ?);
Technically, this inserts the default value. But you haven't specified one, so the default is NULL.
I'm trying to insert some test data into a table to check the functionality of a web servlet, however, using pgAdmin4 to do the insert, I am running into an issue I'm not sure how to rectify. What I want to see is the last value (an image byte stream) is null for this test info. Here is my insert statement:
INSERT INTO schema.tablename("Test Title", "Test Content", "OldWhovian", "2016-07-29 09:13:00", "1469808871694", "null");
I get back:
ERROR: syntax error at or near ";"
LINE 1: ...ldWhovian", "2016-07-29 09:13:00", "1469808871694", "null");
^
********** Error **********
ERROR: syntax error at or near ";"
SQL state: 42601
Character: 122
I've tried removing the semi-colon just for kicks, and it instead errors on the close parenthesis. Is it an issue related to the null? I tried doing this without putting quotations around the null and I get back the same error but on the null instead of the semi-colon. Any help is appreciated, I am new to DBA/DBD related activities.
Related: Using PostgreSql 9.6
The insert statement usually has first part where you specify into which columns you want to insert and second part where you specify what values you want to insert.
INSERT INTO table_name (column1, column2) VALUES (value1, value2);
You do not need to specify into which columns part only if you supply all values in the second part. If you have a table with seven columns you can omit the first part if in the second part you supply seven values.
INSERT INTO table_name VALUES (value1, value2, value3, ...);
Example:
drop table if exists my_table;
create table my_table (
id int not null,
username varchar(10) not null,
nockname varchar(10),
created timestamptz
);
INSERT INTO my_table (id, username) VALUES (1, 'user01');
You insert into columns id and username. The column created has default value specified so when you do not supply value in insert the default is used instead. Nickname and identification_number can accept null values. When no value is supplied NULL is used.
INSERT INTO my_table VALUES (2, 'user02', NULL, NULL, current_timestamp);
That is the same as the previous but here is omitted the fist part so you must supply values for all columns. If you did not you would get an error.
If you want insert multiple values you can use several statements.
INSERT INTO my_table (id, username, identification_number) VALUES (3, 'user03', 'BD5678');
INSERT INTO my_table (id, username, created) VALUES (4, 'user04', '2016-07-30 09:26:57');
Or you can use the postgres simplification for such inserts.
INSERT INTO my_table (id, username, nickname, identification_number) VALUES
(5, 'user05', 'fifth', 'SX59445'),
(6, 'user06', NULL, NULL),
(7, 'user07', NULL, 'AG1123');
At the beginning I have written that you can omit the first part (where you specify columns) only if you supply values for all columns in the second part. It is not completely true. In special cases when you have table that has nullable columns (columns that can contain NULL value) or you have specified DEFAUL values you can also omit the first part.
create sequence my_seq start 101;
create table my_table2 (
id int not null default nextval('my_seq'),
username varchar(10) not null default 'default',
nickname varchar(10),
identification_number varchar(10),
created timestamptz default current_timestamp
);
INSERT INTO my_table2 DEFAULT VALUES;
INSERT INTO my_table2 DEFAULT VALUES;
INSERT INTO my_table2 DEFAULT VALUES;
Result:
101 default NULL NULL 2016-07-30 10:28:27.797+02
102 default NULL NULL 2016-07-30 10:28:27.797+02
103 default NULL NULL 2016-07-30 10:28:27.797+02
When you do not specify values defaults are used or null. In the example above the id column has default value from sequence, username has default string "default", nickname and identification_number are null if not specified and created has default value current timestamp.
More information:
PostgreSQL INSERT
Can anyone tell me why the following SQL refuses to insert more than 1 result (using sqlite3)? And how to make it insert all distinct values from the select port_shipment?
The cargo table in this database contains more than 2000 distinct values for port_shipment, but I tried several versions of the insert-command which resulted always in only 1 value inserted.
create table port (
port_ID integer not null primary key,
port_description text unique collate nocase,
port_name text,
country text,
lat text,
lon text
)
insert or ignore into port (port_ID, port_description)
values (null, (SELECT port_shipment FROM cargo))
Thanks!
The values statement is not needed with select. Try this:
insert or ignore into port (port_ID, port_description)
SELECT NULL, port_shipment
FROM cargo