This is producing an sql syntax error, don't know why:
INSERT INTO dnc_temp (number, release) VALUES ('07938347', '2014-07-10 23:50:12')
The fields I'm inserting into are INT(15) and DATETIME
Thanks
RELEASE is MySQL Reserved keyword
To use a reserved keyword as a column name use ` around the keyword
Try this :-
INSERT INTO dnc_temp (`number`, `release`) VALUES ('07938347', '2014-07-10 23:50:12')
Check this LIVE SQLFIDDLE
column name release is Reserve character that's why producing syntax error. check Live SQLFiddle Link.
MySQL
CREATE TABLE dnc_temp(
num INT(8),
rel DATETIME
);
INSERT INTO dnc_temp (num, rel) VALUES ('07938347', '2014-07-10 23:50:12');
SELECT * FROM dnc_temp;
Result
NUM REL
----------- ----------------------------
7938347 July, 10 2014 23:50:12+0000
use the ` character to escape the reserved word "release".
INSERT INTO dnc_temp (number, `release`) VALUES ('07938347', '2014-07-10 23:50:12')
Related
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}$')
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');
I am trying to further a question I asked yesterday where I wanted to know how to query a date in a different format. But now I am trying to do an insert using this method (see below) however I can't get it to work. I have checked the manual but it is not beginner friendly!
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Put the date in single quotes and move the parenthesis (after the 'yes') to the end:
INSERT INTO custorder
VALUES ('Kevin', 'yes' , STR_TO_DATE('1-01-2012', '%d-%m-%Y') ) ;
^ ^
---parenthesis removed--| and added here ------|
But you can always use dates without STR_TO_DATE() function, just use the (Y-m-d) '20120101' or '2012-01-01' format. Check the MySQL docs: Date and Time Literals
INSERT INTO custorder
VALUES ('Kevin', 'yes', '2012-01-01') ;
Looks like you've not encapsulated your string properly. Try this:
INSERT INTO custorder VALUES ('Kevin','yes'), STR_TO_DATE('1-01-2012', '%d-%m-%Y');
Alternatively, you can do the following but it is not recommended. Make sure that you use STR_TO-DATE it is because when you are developing web applications you have to explicitly convert String to Date which is annoying. Use first One.
INSERT INTO custorder VALUES ('Kevin','yes'), '2012-01-01';
I'm not confident that the above SQL is valid, however, and you may want to move the date part into the brackets. If you can provide the exact error you're getting, I might be able to more directly help with the issue.
The date format for mysql insert query is YYYY-MM-DD
example:
INSERT INTO table_name (date_column) VALUE ('YYYY-MM-DD');
An add-on to the previous answers since I came across this concern:
If you really want to insert something like 24-May-2005 to your DATE column, you could do something like this:
INSERT INTO someTable(Empid,Date_Joined)
VALUES
('S710',STR_TO_DATE('24-May-2005', '%d-%M-%Y'));
In the above query please note that if it's May(ie: the month in letters) the format should be %M.
NOTE: I tried this with the latest MySQL version 8.0 and it works!
When using a string-typed variable in PHP containing a date, the variable must be enclosed in single quotes:
$NEW_DATE = '1997-07-15';
$sql = "INSERT INTO tbl (NEW_DATE, ...) VALUES ('$NEW_DATE', ...)";
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
)
I need to insert form data from my VB.NET application to a Microsoft Access database.
I am getting the error "Syntax error in INSERT INTO statement" when using the following syntax:
INSERT INTO bs1 (teacher, subject, date, period)
VALUES ('test', 'test', 'test', 'test')
I'll admit I'm used to the MySQL type syntax, any help on this matter would be greatly appreciated, thanks.
I believe date is a reserved word. You need to encapsulate the reserved field names in square brackets:
INSERT INTO bs1 (teacher, subject, [date], period) VALUES ('test', 'test', 'test', 'test')
EDIT: See the following article for a complete list of reserved words in Access 2002 and greater:
http://support.microsoft.com/kb/286335
~md5sum~
In Access the delimiter for literal values inserted into date fields is #, for text fields is ' or " and numeric field values do not have a delimiter, which suggests:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', #2009-12-31#, 0)
In Access Database Engine SQL code, when you need to specify that a literal value is of type DATETIME, you can either explicitly cast the value to DATETIME or use # characters to delimit the value.
Using an explicit cast using the CDATE() function:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', CDATE('2009-12-31 00:00:00'), 0);
Using a DATETIME literal value:
INSERT INTO bs1 (teacher, subject, [date], period)
VALUES ('test', 'test', #2009-12-31 00:00:00#, 0);
When INSERTing a value into a column of type DATETIME, if you do not specify an explicit DATETIME value, the engine will implicitly attempt to coerce a value to DATETIME. The literal value 'test' cannot be coerced to type DATETIME and this would appear to be the source of your syntax error.
Note: none of the above applies to the NULL value. In Access Database Engine SQL there is no way to cast the NULL value to an explicit type e.g.
SELECT CDATE(NULL)
generates an error, "Invalid use of NULL". Therefore, to specify a NULL DATETIME literal, simply use the NULL keyword.
It pays to remember that the Access Database Engine has but one temporal data type, being DATETIME (its synonyms are DATE, TIME, DATETIME, and TIMESTAMP). Even if you don't explicitly specify a time element, the resulting value will still have a time element, albeit an implicit one. Therefore, it is best to always be explicit and always include the time element when using DATETIME literal values.