简体   繁体   English

DateTime转换错误-Excel到SQL

[英]DateTime Conversion Error - Excel to SQL

I have two data sets on two different SQL servers. 我在两个不同的SQL服务器上有两个数据集。 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: 我有数据集1并将其放入Excel,然后将其放入临时表中,以便可以针对服务器2上的数据进行查询。这是我创建的SQL代码:

    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. 不确定您使用的是哪个数据库,但这是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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM