having (Conversion failed ) error in SQL Server - sql

When I create a table using bellow SQL query:
CREATE TABLE student(studentID INTEGER PRIMARY KEY,studnetName VARCHAR (30) NOT NULL,birthday Date)
Bellow operations(SQL commands) show me an error:
INSERT INTO student VALUES('226745','Ahmed','24-06-1997')
INSERT INTO student VALUES('226745','sara','28-03-2000')
INSERT INTO student VALUES('226745','Ali','12-02-2007')
the exact error is:
( Conversion failed when converting date and/or time from character string).
Can anybody help to explain why it happens?

It's because sql engine can't figure out the date format automatically (probably confused between m-d-y and d-m-y)
so you can specify the dateformat before you insert, ex in sql server:
set dateformat dmy;
and the same situation in other dbms

You don't specify your database. Most databases support strings in the format 'YYYY-MM-DD' (an ISO 8601-compatible format) for date constants:
INSERT INTO student VALUES('226745', 'Ahmed', '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', '2007-02-12');
In Standard SQL, date constants are supported by using the date keyword:
INSERT INTO student VALUES('226745', 'Ahmed', DATE '1997-06-24');
INSERT INTO student VALUES('226745', 'sara', DATE '2000-03-28');
INSERT INTO student VALUES('226745', 'Ali', DATE '2007-02-12');
I would also advise you to list the columns explicitly when using insert.

Related

ORA-01858 Error in an Oracle SQL Table

I've been fighting with this problem for a few days.
I created the next table in Oracle's SQL Developer:
CREATE TABLE EMPLEADOSMM (
ID_EMPLEADO VARCHAR2 (10) PRIMARY KEY,
NOMBREEM VARCHAR2 (15) NOT NULL,
AP_PATEM VARCHAR2 (20) NOT NULL,
AP_MATEM VARCHAR2 (20) NOT NULL,
FECHA_NAC DATE NOT NULL,
FECHA_ING DATE NOT NULL,
ID_CARGO VARCHAR2 (10));
Then, I proceeded to add some values to the table, the thing is that it added some like this:
INSERT INTO EMPLEADOSMM VALUES ('100006','OSCAR','MARIN','PEREZ','12-jul-85','15-nov-17','C0002');
But this others send me the 'ORA-01858: a non-numeric character was found where a numeric was expected' error.
INSERT INTO EMPLEADOSMM VALUES ('100004','FABIAN','RODRIGUEZ','VELEZ','31-aug-87','13-jul-17','C0003');
INSERT INTO EMPLEADOSMM VALUES ('100005','LUZ MARIA','TORINO','YAÑEZ','11-dec-90','13-jul-17','C0003');
I tried changing the year format by "yyyy", rewriting all the zeros, but nothings seems to work. Some ideas?
First, I want to emphasize that the code does work. Here is a SQL Fiddle demonstrating the working code.
Second, that means that something about your system causes it to break. Barbaras Ozhan seems to have the right explanation -- internationalization settings recognize some month abbreviations as being the same as English, but not all of them.
You should be writing the insert as:
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100006', 'OSCAR', 'MARIN', 'PEREZ', DATE '1985-07-12', DATE '2017-11-15', 'C0002');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100004', 'FABIAN', 'RODRIGUEZ', 'VELEZ', DATE '1987-08-31', DATE '2017-07-13', 'C0003');
INSERT INTO EMPLEADOSMM (ID_EMPLEADO, NOMBREEM, AP_PATEM, AP_MATEM, FECHA_NAC, FECHA_ING, ID_CARGO)
VALUES ('100005', 'LUZ MARIA', 'TORINO', 'YAÑEZ', DATE '1990-12-11', DATE '2017-07-13', 'C0003');
Oracle supports the ANSI standard keyword DATE for introducing date constants in the ISO/ANSI standard format, YYYY-MM-DD. I strongly recommend that you use this format in all your code. Use TIMESTAMP when there is a time component.
Including the column names is a best practice.
I would question why the employee id is a string, if you are only going to include numbers in it.

Updating timestamp with custom format (dd/MM/yyyy HH:MM:ss.FFFFFFF)

How to write a statement that will update the date field in database with dd/MM/yyyy HH:MM:ss.FFFFFFF format.
I have select query which return the require string
SELECT FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
I tried with
update ORDER
set timedate=FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
WHERE ID='288'
But returning error :
SQL Error [8152] [22001]: String or binary data would be truncated.
com.microsoft.sqlserver.jdbc.SQLServerException: String or binary data would
be truncated.
My field datatype is varchar 27
CREATE TABLE AYAM.dbo.ORDER (
ID int NOT NULL IDENTITY(1,1),
TIMEDATE varchar(27) NOT NULL,
CONSTRAINT PK_ORDER_DATA PRIMARY KEY (ID,TIMEDATE)
) GO;
I am using MSSQL 2016
Why do you ask for FFFFFFF when CURRENT_TIMESTAMP function returns only 3 decimal numbers? Use SYSDATETIME() function to get better precision.
I was unable to re-produce the error. What SQL Server version do you use?
create table #test (timedate varchar(27))
insert into #test VALUES ('test');
update #test
set timedate=FORMAT(SYSDATETIME(), 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
select * from #test
drop table #test
The output is: 18/09/2017 12:09:44.6914345
UPDATED:
the same test was done using your table structure. No errors ...
INSERT INTO dbo.[ORDER] (TIMEDATE) VALUES ('test')
GO 300
update [ORDER]
set timedate=FORMAT(CURRENT_TIMESTAMP, 'dd/MM/yyyy HH:MM:ss.FFFFFFF')
WHERE ID='288'

Issues with TIMESTAMP and CURRENT_TIMESTAMP in SQL commands

I have some issue creating tables that use CURRENT_TIMESTAMP to specify a date. I need this because I'm using java jpa entities to retrieve them by date. If I run to a local h2 database I have no issues.
In this example:
INSERT INTO Post (id, title, slug, teaser, body, author_id, posted_on)
VALUES (1, 'Spring Boot Rocks!', 'spring-boot-rocks', #TEASER, #BODY, 1, CURRENT_TIMESTAMP);
Everything gets created and works perfectly, but when I try the same query in an Azure SQL database that I'm connecting into I get the error
Failed to execute query. Error: Cannot insert an explicit value into a
timestamp column. Use INSERT with a column list to exclude the
timestamp column, or insert a DEFAULT into the timestamp column.
If I try to change CURRENT_TIMESTAMP to TIMESTAMP I get;
Failed to execute query. Error: Invalid column name 'TIMESTAMP'.
If I change it to DEFAULT as the previous error suggests the tables get created but I can't retrieve them by date of creation since DEFAULT is not a time value.
full query
SET IDENTITY_INSERT author ON
insert into author(id,first_name,last_name,email) values (1,'Dan','Vega','danvega#gmail.com');
insert into author(id,first_name,last_name,email) values (2,'John','Smith','johnsmith#gmail.com');
SET IDENTITY_INSERT author OFF
SET IDENTITY_INSERT post ON
DECLARE #TEASER varchar(4000) = 'text...'
DECLARE #BODY varchar(4000) = 'text...'
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (1,'Spring Boot Rocks!','spring-boot-rocks',#TEASER,#BODY,1,CURRENT_TIMESTAMP);
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (2,'Spring Data Rocks!','spring-data-rocks',#TEASER,#BODY,1,CURRENT_TIMESTAMP);
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (3,'John Blog Post 1','john-blog-post-1',#TEASER,#BODY,2,CURRENT_TIMESTAMP);
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (4,'John Blog Post 2','john-blog-post-2',#TEASER,#BODY,2,CURRENT_TIMESTAMP);
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (5,'John Blog Post 3','john-blog-post-3',#TEASER,#BODY,2,CURRENT_TIMESTAMP);
insert into Post(id,title,slug,teaser,body,author_id,posted_on) values (6,'Refactoring our Spring Data Project','refactoring-spring-data-project',#TEASER,#BODY,1,CURRENT_TIMESTAMP);
SET IDENTITY_INSERT post OFF
The TIMESTAMP datatype in SQL Server (and SQL Azure) is NOT what the ISO/ANSI Standard defines (this is a leftover of the original Sybase heritage of SQL Server).
It's really just a binary counter for optimistic concurrency checking - it has NOTHING to do with a date and/or time at all!
To store and handle dates and times, use the datatype DATE (for just dates - no time) or DATETIME2(n) for date&time instead
Fixed by recreating the database using application.properties spring.jpa.hibernate.ddl-auto=create-drop and replacing
#CreatedDate #Column(columnDefinition = "TIMESTAMP")
private Date postedOn;
to
#CreatedDate #Column(columnDefinition = "DATETIME2(0)")
private Date postedOn;

Bulk insert issue with date from excel into SQL Table

I am trying to bulk insert these two columns from excel into a temp table ##NBP_Table. However, when I do that I get the following error:
'Operand type clash: int is incompatible with date'
Does that mean the date aren't in the format it should be to be inserted into a table?
create table ##NBP_Table
(
Applicable_Date date,
NBP_Value numeric(4,4)
)
insert into ##NBP_Table
values (01/04/2014,1.7107),
(02/04/2014,1.6482),
(03/04/2014,1.686),
(04/04/2014,1.6681)
To get the date insert working, please try this
create table ##NBP_Table
(
Applicable_Date date
NBP_Value numeric(5,4)
)
insert into ##NBP_Table
values ('01/04/2014',1.7107)
The date needs to be in quotation marks
I have also corrected the numeric data type for you
this date in expression is considered as int so it will be performed / operations,
so please use 'before starting date and ' after ending date.
'01-04-2014'
Create table #NBP_Table
(
Applicable_Date date,
NBP_Value numeric(5,4)
)
insert into #NBP_Table
values ('01-04-2014',1.7107),
('02-04-2014',1.6482),
('03-04-2014',1.686),
('04-04-2014',1.6681)

How to insert date values into table

How can I insert into table with different input using / ,with date datatype?
insert into run(id,name,dob)values(&id,'&name',[what should I write here?]);
I'm using oracle 10g.
Since dob is DATE data type, you need to convert the literal to DATE using TO_DATE and the proper format model. The syntax is:
TO_DATE('<date_literal>', '<format_model>')
For example,
SQL> CREATE TABLE t(dob DATE);
Table created.
SQL> INSERT INTO t(dob) VALUES(TO_DATE('17/12/2015', 'DD/MM/YYYY'));
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM t;
DOB
----------
17/12/2015
A DATE data type contains both date and time elements. If you are not concerned about the time portion, then you could also use the ANSI Date literal which uses a fixed format 'YYYY-MM-DD' and is NLS independent.
For example,
SQL> INSERT INTO t(dob) VALUES(DATE '2015-12-17');
1 row created.
date must be insert with two apostrophes'
As example if the date is 2018/10/20. It can insert from these query
Query -
insert into run(id,name,dob)values(&id,'&name','2018-10-20')
let suppose we create a table Transactions using SQl server management studio
txn_id int,
txn_type_id varchar(200),
Account_id int,
Amount int,
tDate date
);
with date datatype we can insert values in simple format: 'yyyy-mm-dd'
INSERT INTO transactions (txn_id,txn_type_id,Account_id,Amount,tDate)
VALUES (978, 'DBT', 103, 100, '2004-01-22');
Moreover we can have differet time formats like
DATE - format YYYY-MM-DD
DATETIME - format: YYYY-MM-DD HH:MI:SS
SMALLDATETIME - format: YYYY-MM-DD HH:MI:SS
insert into run(id,name,dob)values(&id,'&name',[what should I write
here?]);
insert into run(id,name,dob)values(&id,'&name',TO_DATE('&dob','YYYY-MM-DD'));
You can also use the "timestamp" data type where it just needs "dd-mm-yyyy"
Like:
insert into emp values('12-12-2012');
considering there is just one column in the table...
You can adjust the insertion values according to your table.
I simply wrote an embedded SQL program to write a new record with date fields.
It was by far best and shortest without any errors I was able to reach my requirement.
w_dob = %char(%date(*date));
exec sql insert into Tablename (ID_Number ,
AmendmentNo ,
OverrideDate ,
Operator ,
Text_ID ,
Policy_Company,
Policy_Number ,
Override ,
CREATE_USER )
values ( '801010',
1,
:w_dob,
'MYUSER',
' ',
'01',
'6535435023150',
'1',
'myuser');
To insert the current date you can just use this GETDATE() function.
insert into run(id,name,dob) values(&id,'&name',GETDATE());
you can also use CURRENT_TIMESTAMP() function to insert current date and time.