I have tried to Insert a value into a table in MySQL but I can't make it work. I am using the following queries:
INSERT into articulo values (32,'Sala',CAST('$10,000.45999' AS DECIMAL(10,5)),40.2399,200.2399,3,'kid 3');
MySQL shows the following error:
1 row(s) affected, 1 warning(s): 1292 Truncated incorrect DECIMAL value: '$10,000.45999'
And it shows the following into the table:
Of course I created the table 'articulo' before:
CREATE Table articulo
(
id_art int NOT NULL,
nom_art varchar (25) DEFAULT 'XXXXXXXXXXXXX',
prec_art decimal (10,5) DEFAULT 0.000,
peso_art decimal (10,5),
existencia float,
color_art int, CONSTRAINT chk_color1 CHECK (color_art between 0 and 20),
um_art varchar (10) DEFAULT 'DEF_PZA',
primary key (id_art)
);
I have seen many examples for Casting but all of them use the cast function under a select
statement.
Any idea how I can do in order to perform what I want?
I want to store $10,000.45999 into the table as a decimal value.
This would be 10000.45999
Thanks for your support!
You can insert the value by fixing up the number. For your case, this should work:
INSERT into articulo
SELECT 32, 'Sala',
CAST(REPLACE(REPLACE('$10,000.45999', ',', ''), '$', '') AS DECIMAL(10,5)),
40.2399, 200.2399, 3, 'kid 3';
Strictly speaking, the cast() is not necessary, but I like to avoid implicit conversions -- these can lead to hard-to-detect problems.
As a note: it is a good idea to include the column list in the insert statement.
You can't use commas or the dollar symbol in your value in that query.
You could rewrite your query as:
INSERT into articulo values (32,'Sala',CAST('10000.45999' AS DECIMAL(10,5)),40.2399,200.2399,3,'kid 3');
However you don't need to cast your value as a decimal if your column is already well defined as DECIMAL(10,5).
Simply write:
INSERT into articulo values (32,'Sala',10000.45999,40.2399,200.2399,3,'kid 3');
Related
Because I don't use Oracle 21. I can't use the JSON type in the definition of a table.
CREATE TABLE TABLE_TEST_QUERY_2
(
TTQ_NR INTEGER GENERATED BY DEFAULT AS IDENTITY,
TTQ_QUERY_TO_BE_TESTED VARCHAR2 (4000 BYTE),
TTQ_RESULT CLOB,
--RESULT JSON, UPGRADE oracle 21
TTQ_TTQ_CREATION_DATE DATE DEFAULT SYSDATE,
TTQ_ALREADY_TESTED INTEGER DEFAULT 0,
TTQ_TEST_PASSED INTEGER,
PRIMARY KEY (TTQ_NR),
CONSTRAINT RESULT CHECK (TTQ_RESULT IS JSON)
)
I want to add a json object in ttq_result. Not a string representing a json.
I've a way to transform a json into a clob.
select to_clob(utl_raw.cast_to_raw (json_object('a' value 2))) from dual;
But it's not working, if I try to insert the clob created from a json in the table
INSERT INTO BV_OWN.TABLE_TEST_QUERY_2 TTQ_RESULT
VALUES to_clob(utl_raw.cast_to_raw (json_object(a value '2')));
[Error] Execution (3: 13): ORA-03001: unimplemented feature
code(oracle 18)
update:
I've tried to add a json on dbfiddle with oracle 21. I'm using the json type to define a column.
CREATE TABLE TABLE_TEST_QUERY_2
(
TTQ_NR INTEGER GENERATED BY DEFAULT AS IDENTITY,
TTQ_QUERY_TO_BE_TESTED VARCHAR2 (4000 BYTE),
TTQ_RESULT JSON,
TTQ_TTQ_CREATION_DATE DATE DEFAULT SYSDATE,
TTQ_ALREADY_TESTED INTEGER DEFAULT 0,
TTQ_TEST_PASSED INTEGER,
PRIMARY KEY (TTQ_NR)
)
INSERT INTO TABLE_TEST_QUERY_2 TTQ_RESULT
VALUES json_object('a' value 2);
I have the same error.
ORA-03001: unimplemented feature
Maybe are these 2 problems related.
code oracle 21
Your first problem is because you are using the wrong syntax as you have omitted the brackets from around column identifiers or the column value:
INSERT INTO BV_OWN.TABLE_TEST_QUERY_2 (TTQ_RESULT)
VALUES ( to_clob(utl_raw.cast_to_raw (json_object(a value '2'))));
Which fixes the unimplemented feature exception but now you get:
ORA-00984: column not allowed here
Which is because you are using a different query to the SELECT as you have changed json_object('a' value 2) to json_object(a value '2') and the query cannot find a column a.
If you fix that by using the original code from the SELECT with 'a' as a string literal and not a a column identifier:
INSERT INTO BV_OWN.TABLE_TEST_QUERY_2 (TTQ_RESULT)
VALUES ( to_clob(utl_raw.cast_to_raw (json_object('a' value 2))));
You will then get the error:
ORA-02290: check constraint (FIDDLE_FCJHJVMCPHKXUCUPDUSV.RESULT) violated
Because converting to a RAW and then to a CLOB will mangle the value.
You need something much simpler:
INSERT INTO BV_OWN.TABLE_TEST_QUERY_2 (TTQ_RESULT)
VALUES (json_object('a' value 2));
or:
INSERT INTO BV_OWN.TABLE_TEST_QUERY_2 (TTQ_RESULT)
VALUES (EMPTY_CLOB() || json_object('a' value 2));
Which both work.
db<>fiddle here
I have a table
CREATE TABLE table_name
(
EmpId VARCHAR(50),
Name VARCHAR(100)
)
How can I restrict the EmpId column to consist of two letters followed by 3-5 digits? The following are all examples of valid values:
ac236, ak2356, av23695, ak365
I tried using the following check constraint:
ALTER TABLE table_name
ADD CONSTRAINT ck_table_name CHECK (EmpId NOT LIKE'%[^a-zA-Z0-9 ]%')
However, it allows all combinations of letters & digits, such as "23" and "fads":
INSERT INTO table_name
VALUES
('23', 'Test 2'),
('fabs', 'Test 2');
If a value violates the format, I'd like the query to fail and print error message. For example, if 'na23' were inserted as the EmpID, MySQL could say:
Empid should be ab123/ab1234/a12345 format
Initially, I was using MySQL 5.7.11-0ubuntu6-log (which, it turns out, doesn't support CHECK constraints), but have upgraded to MySQL 8.0.17.
Assuming it's MySQL >=8.0.16
check(EmpId regexp '^[a-z]{2}[0-9]{3,5}$')
First lets see how my table structure is set:
CREATE TABLE IF NOT EXISTS `RFVPOS`.`Station` (
`id` INT NOT NULL AUTO_INCREMENT,
`code` VARCHAR(20) NOT NULL,
`name` VARCHAR(45) NOT NULL,
`safeDropAmount` DECIMAL(4,4) NOT NULL,
`deadStockVolume` DECIMAL(4,4) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
And here is what MySQL forward engineering does:
`INSERT INTO `rfvpos`.`station` (`code`, `name`, `safeDropAmount`, `deadStockVolume`) VALUES ('test', 'test',` '100', '300');
Im aware that by enclosing my safeDropAmount and deadStockVolume with single qoutes would mean they are in a varchar datatype, so im removing them like this:
`INSERT INTO `rfvpos`.`station` (`code`, `name`, `safeDropAmount`, `deadStockVolume`) VALUES ('test', 'test', 100, 300);
But either ways, when i execute these scripts they say the same thing. this:
ERROR 1264: 1264: Out of range value for column 'safeDropAmount' at row 1
Kindly help me. I was using MySQL before, and after 3 years i'm back again at it, so i'm kinda refreshing.
Best regards,
The column datatype DECIMAL(4,4) won't allow for a value larger than 0.9999 to be stored. Attempting to store value greater than that (for example, 300) would give the "out of range" error.
DECIMAL(4,4) specifies a total of four digits, with four of them (all of them) after the decimal point, leaving zero digits before the decimal point.
If we want to allow values up to 9999.9999, we'd want datatype DECIMAL(8,4).
That's a total of eight digits, with four of those digits after the decimal point, the remainder (8-4) before the decimal point.
Also, MySQL will evaluate the values in single quotes as numeric, in a numeric context, such as inserting into DECIMAL column. The addition or removal of the single quotes around the numeric values, e.g. 300 or '300', has no effect, and they will be evaluated the same. (The difference is what error is returned, if we have a value that isn't numeric (invalid column vs .re is a difference in the error returned what we specify as a literal is not a numeric... foo would raise "Unknown column" error, 'foo' would raise "Incorrect value" error.
If i have my SQL create statement as follows;
CREATE TABLE TABLENAME12
(
TAB_ID INT NOT NULL AUTO_INCREMENT,
NAME_FIRST NVARCHAR(200),
TYPE NVARCHAR(200),
PRIMARY KEY( TAB_ID )
);
and if i want to Insert values to it, should i enter TAB_ID too ? Since it's auto increment.
When i INSERT INTO SWM_SALES_FEEDBACK VALUES (1,'Jerry','ty'); It gets inserted if i don't specify the primary key INSERT INTO SWM_SALES_FEEDBACK VALUES ('Jerry','ty'); i get the following error :
ERROR 1136 (21S01): Column count doesn't match value count at row 1
1.) What is the point of having AUTO_INCREMENT if it doesn't get auto incremented.
2.) If i have a field called:
BIRTH_TIME DATE,
How should i INSERT value to this field since it's DATE type
1) You have to respect an order of columns in your table. You can do either:
INSERT INTO SWM_SALES_FEEDBACK VALUES (null, 'Jerry','ty');
or
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST, TYPE) VALUES ('Jerry','ty');
2) You can use '2012-07-24' format for date column.
With an auto increment, you need to specify your columns
INSERT INTO tablename12 ('name_first','type') values ('Jerry','ty')
If you aren't inserting into every column, you need to tell the query which columns you want to insert into. try INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Not sure if mysql has a to_date() function in the new releases:
But, you can use this : Just enclose the date time field in single quotes. You can specify the date time formats
INSERT INTO table_name (birth_date) VALUES ('2008-07-04')
For other date format questions refer this.
For your auto increment, specify the columns
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
Point 1
If you don't want to store all fields in DB then you have to specify those column names before VALUES clause as shown below.
INSERT INTO myTable (field2,field3) VALUES ('field2','field3')
In your case, statement should be
INSERT INTO SWM_SALES_FEEDBACK (name_first, type) VALUES ('Jerry','ty')
^^^^^^^^^^^^^^^^^^
Point 2
If you have field as DATETIME, TIMESTAMP as datatype, you can enter date as
INSERT INTO myTable (myDate) VALUES ('2012-12-28 12:12:12')
Format is yyyy-mm-dd hh:mm:ss
If you want to store the current time of the system in DB, you could use NOW() as shown below.
INSERT INTO myTable (myDate) VALUES (NOW())
Try the following code
INSERT INTO SWM_SALES_FEEDBACK (NAME_FIRST,TYPE) VALUES ('Jerry','ty');
I am using MySQL database.
I have one table having column with datatype binary(16).
I need help with the insert statement for this table.
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9e-efdf-b449');
Error : Lookup Error - MySQL Database Error: Data too long for column 'distid' at row 1
How to resolve this issue?
You should remove the hyphens to make the value match the length of the field...
Example:
CREATE TABLE `assignedresource` (
`distid` binary(16) NOT NULL
)
insert into assignedresource values ('9fad5e9eefdfb449');
Also, MySQL standard is to use this notation to denote the string as binary... X'9fad5e9eefdfb449', i.e.
insert into assignedresource values (X'9fad5e9eefdfb449');
Well, assuming that you want to strictly insert a hexadecimal string, first you need to remove the dashes and then "unhex" your string before inserting it into a binary(16) data type column, the code would go like this:
INSERT INTO `assignedresource` VALUES(UNHEX(REPLACE('9fad5e9e-efdf-b449','-','')));
Also... the "usable" data you are inserting is actually 8 bytes after undashing it, so binary(8) would do fine if you plan on not storing the dashes.
You can strip the hyphens and perpend 0x to the value unquoted, like this:
insert into assignedresource values (0x9fad5e9eefdfb449);
As well as, as this (mentioned in other answers):
insert into assignedresource values (X'9fad5e9eefdfb449');
Both are valid notation for a hexadecimal literal.
Your string is 18 char long, change the database
CREATE TABLE `assignedresource` (
`distid` binary(18) NOT NULL
)