DateTime Conversion Error - Excel to SQL - sql

I have two data sets on two different SQL servers. I've got dataset 1 and put it into Excel and am then going to put this into a temp table so I can query it against the data on server 2. Here is the SQL code that I have created:
Create table #JWTemp1 (AutoID varchar(10), IDNumber varchar(20), AltIDNumber varchar(20), AdmitDTTM datetime, AdmitDay varchar(15), AdmitWeekNo int)
Insert into #JWTemp1 Values('ID001','BCC445567','ABC445567','42510.7326388889','Friday','21')
Each time I try and run the code, I get the following error:
Conversion failed when converting date and/or time from character string.
I know this is a common error but I've tried all manner of soutions and got nowhere. Any suggestions?

You have to format the string. Not sure what DB are you using but here is the syntax for mySql.
DATE_FORMAT(colName, '%Y-%m-%d') DATEONLY
DATE_FORMAT(colName,'%H:%i:%s') TIMEONLY

Unfortunately, non of the answers provided seemed to work. However, I solved the issue using the following logic:
Create table #JWTemp1 (AutoID varchar(10), IDNumber varchar(20), AltIDNumber varchar(20), AdmitDTTM datetime, AdmitDay varchar(15), AdmitWeekNo int)
Insert into #JWTemp1 Values('ID001','BCC445567','ABC445567','42510.7326388889','Friday','21')
Select
convert(datetime, Convert(float,AdmitDTTM))

Related

How to call a DB2 AS400 table valued function which take Date parameters

I have a table valued function as below
CREATE FUNCTION ABELIBLE.TVFBOEGETSHIPMENTS (STARTDATE DATE, ENDDATE DATE, ADDRESSCODE CHAR(9))
RETURNS TABLE (ID INT, JOBNUMBER CHAR(9), CUSTOMERREFERENCE CHAR(18),
CONSIGNEENAME CHAR(30), CREATEDDATE DATE, AIRPORTOFORIGIN CHAR(3), AIRPORTOFARRIVAL CHAR(3),
AIRPORTOFDESTINATION CHAR(3), COUNTRYOFDESTINATION CHAR(3), ADDRESSCODE CHAR(9), CONSIGNMENTNUMBER CHAR(25))
LANGUAGE SQL
NOT DETERMINISTIC
READS SQL DATA
RETURN
SELECT
ROW_NUMBER() OVER(ORDER BY EMJOBN DESC),
A.EMJOBN,
A.EMCREF,
A.EMOSNM,
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')),
A.EMAOFO,
A.EMAOFA,
A.EMAOFD,
A.EMCOFD,
A.EMUKCD,
A.EMRPRT
FROM DTALIBLE.EMASTER A WHERE A.EMPSFT = 'Y' AND A.EMUKCD = ADDRESSCODE AND
DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) >= DATE(STARTDATE) AND DATE(TIMESTAMP_FORMAT(DIGITS(A.EMCRTD), 'DDMMYY')) <= DATE(ENDDATE)
However I am not getting any results back when I run the following query
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
I am using DBeaver to run the query on AS400.
For my own sanity I tired the select statement with literals and i got the following result
Then again when I tried with below dates it does not fetch any results and i get this error.
any help is much appreciated. Thank you in advance.
The only thing that jumps out at me is that in your invocation,
SELECT * FROM TABLE(ABELIBLE.TVFBOEGETSHIPMENTS(DATE('06/06/2019'), DATE('06/07/2020'),'MUL0044')) AS XXX
'MUL0044' as a literal is treated as varchar, and your UDTF has the parm defined as char.
That mismatch usually gives a Function not found error however. You can cast the literal to char or change the parm type to varchar. Which to chose depends on how it will normally be invoked. If you're just testing with a literal and the function would normally be passed a char then just cast the literal to char.
If you use the Run SQL Scripts component of IBM ACS, you'll have the ability to debug the function.

SQL Server 2008 varchar and char not working

I get an error saying varchar or char is not a recognized in SQL Server. However I use the same software, SQL Server 2008 in college, so I do not want to go for a different version of SQL Server. Anyway I can fix this?
Major Error 0x80040E14, Minor Error 26302
create table abc
(
id int,
name varchar(15)
)
Error is:
The specified data type is not valid. [ Data type (if known) = varchar
]
varchar(n) is not supported in SQL Server Compact. Here is the full list of types supported.
Following are the types that you can use.
nvarchar(n)
nchar(n)
ntext
So you might need to change to nvarchar(10), nvarchar(5) and nchar(1) etc..
Really ? , i tried it, also i tried in creating temp table, it seems both are ok.
create table abc (id int, name varchar(15))
create table #tmp (id int, name varchar(15))
http://sqlfiddle.com/#!3/66b44

Bulk inserting data gives error

Attempting to bulk insert into a table and I am getting the error:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 31, column 4 (Birthday).
Below is the code I am trying to use to insert the data:
Bulk Insert Dzt.dbo.Player
From 'A:\New Folder\Seed_Files\Player.csv'
With
(
FieldTerminator=',',
RowTerminator='\n',
FirstRow=2
)
Here is the code I used when making the table:
Use Dzt
Create Table Player
(
Player_ID int,
FirstName varchar(255),
LastName varchar(255),
Birthday date,
Email varchar(255),
L_Flag varchar(255)
);
This is my first attempt at making a table and inserting data so I am thinking it is likely a datatype error for the Birthday field but I have been unable to find anything online that I am able to grasp my head on at this time. I have also tried use the datatype datetime instead of date but I received the same error.
I am using SSMS 2012 to create and insert the data onto a 2012 SQL Server.
Let me know if there is anything else I can provide that might help.
As you suspect it could be a date format error, I would suggest importing the csv into a table with Birthday column set to varchar type. Then use this query to filter the erroneous records.
select birthday from temptable where isdate(birthday) = 0
You could then correct those records and then insert them into your old table.

Conversion failed when converting date and/or time from character string SQL

CREATE TABLE TICKET
(
TicketID int primary key,
StartLoc varchar(20),
FinalLoc varchar(20),
price int,
DateOfPurchase date,
SeatNumber int,
TrainID int,
DepartureDate date ,
);
insert into TICKET
values (1,'HaydarPasa','Sirkeci',20,'20,18-06-12 10:34:09 AM',10,1,'20-06-12 10:34:09 AM')
I'm using SQL Server Management 2008 R2 version.When execute the query it says
Conversion failed when converting date and/or time from character string SQL
#juergenD is right, the string 20,18-06-12 10:34:09 AM is not a date string. Is the 20 part of the insert? if so your table definition is off. I expect a copy-paste error here...
Additionally: When i try your query on my machine, it still gives an error converting to date. If I change the dates to the format yyyy-MM-dd hh:mm:ss AM it does work. Might be to do with local settings though.

mysql syntax error for timestamp

I have this piece of SQL that is being fed to Mysql.
CREATE TABLE pn_history(
member INT,
action INT,
with INT,
timestamp DATETIME,
details VARCHAR(256)
)
But is comes back as an error about the syntax.
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'with INT,
timestamp DATETIME,
details VARCHAR(256)
)' at line 4
Why is this failing?
Both 'with' and 'timestamp' are reserved words in MySQL. So to get this to work, you'd need to escape each one:
CREATE TABLE pn_history(
member INT,
action INT,
`with` INT,
`timestamp` DATETIME,
details VARCHAR(256)
)
Really though, you need to consider changing the names of your columns identifiers.
Read more about MySQL Reserved Words.
EDIT: Actually, TIMESTAMP is not a reserved word. The documentation says:
MySQL allows some keywords to be used
as unquoted identifiers because many
people previously used them. Examples
are those in the following list:
ACTION
BIT
DATE
ENUM
NO
TEXT
`TIME
TIMESTAMP
So I guess that means peer pressure took TIMESTAMP off the reserved word list. Hah!
The problem is the name of the with column. Change the name into something like withValue.
CREATE TABLE pn_history(
member INT,
action INT,
withValue INT,
timestamp DATETIME,
details VARCHAR(256)
)
timestamp is a keyword (it is a data type in mysql) which may be causing you problems.
I would suggest using a different name, but if it must be named timestamp, try using backticks to quote it.