'Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
来源:互联网 发布:iphone7plus软件铃声 编辑:程序博客网 时间:2024/05/23 07:23
用pymssql 连接SqlServer数据库后,向数据库插入一条包含时间字段的数据时,报错如下:
‘Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
处理过程:
1、直接在数据库里执行insert语句
INSERT INTO [dbo].[ClustedResult]([outputScript],[classId],[startTime],[endTime]) VALUES(0x76A914E67550CD61C6D89DF67F4D5F8E0B4AD30013C65888AC,'493886f0-7387-11e7-ab02-047d7ba2a507','2017-07-28 19:24:14.431000','2017-07-28 19:24:14.431000')
发现也报错,显示字符串与日期转换失败。
2、查看表中各字段的数据类型
结果:[startTime],[endTime]两个字段的数据类型是datetime
网上查找sqlserver 2008表示时间的数据类型有6种。
time、date、smalldatetime、datetime、datetime2、datetimeoffset;
datetime
日期和时间部分,可以表示的日期范围从公元1753年1月1日00:00:00.000 到9999年12月31日23:59:59.997 ,精确到3.33毫秒,它需要8个字节的存储空间。DateTime字段类型对应的时间格式是 yyyy-MM-dd HH:mm:ss.fff ,3个f,精确到1毫秒(ms),示例 2014-12-03 17:06:15.433
smalldatetime
从1900年1月1日到2079年6月6日的日期和时间数据精确到分钟。29.998秒或更低的 smalldatetime值向下舍入为最接近的分钟,29.999秒或更高的smalldatetime值向上舍入为最接近的分钟。需要4字节的存储空间。
date
SQL Server 2008新引进的数据类型。它表示一个日子,不包含时间部分,可以表示的日期范围从公元元年1月1日到9999年12月31日。只需要3个字节的存储空间。
DateTime2
DateTime2字段类型对应的时间格式是 yyyy-MM-dd HH:mm:ss.fffffff ,7个f,精确到0.1微秒(μs),示例 2014-12-03 17:23:19.2880929 。
在读取该数据时不管你的字段是datetime或smalldatetime读出来的格式都一样(如:1900-01-01)。datetime不会显示出他的毫秒,但是在进行时间比较时他要毫秒就会出现,从而让两个时间段不能相等。
如果是SQL Server 2005,那么请你使用smalldatetime吧,数据能节约一半,虽然查询的时候看起来没什么改变;
如果你是SQL Server 2008,那么请你使用date吧,虽然3个字节跟4个字节没有多大的差距,但是从设计上和逻辑清晰度上都有很大的提升。
如果用SQL的日期函数进行赋值,DateTime字段类型要用 GETDATE() ,DateTime2字段类型要用 SYSDATETIME() 。
有非常详细的参考:
http://www.csdn.net/article/1970-01-01/282777
http://blog.csdn.net/justdb/article/details/7575021
3、进行了一系列知识恶补后,查看python下时间类型的特点
begin = datetime.datetime.now()print type(begin)print str(begin)#运行结果:<type 'datetime.datetime'>2017-07-28 19:24:14.431000
4、找到问题原因,是由于python 和sqlserver的时间类型format不一样导致的。提供两种解决方案:
方案一:将python的datetime格式统一为数据库格式。
timeStamp = str(datetime.datetime.now())[0:-3]
方案二:修改数据库数据类型为范围更大的datetime2
- 'Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- 从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值/Conversion failed when converting date and/or time from c
- error: converting to execution character set: Invalid or incomplete multibyte or wide character
- 1575451 - "Conversion from UTF8 to charset failed" error in Dashboard Design or BI Launchpad
- time value from 1970 to date string.
- Converting string to/from integer
- Converting from String^ to BSTR
- The error: ORA-01830: date format picture ends before converting entire input string..
- Parse Date-time From String With PHP & MySQL
- short or long (date and time)format display
- Linux Set Date and Time From a Command Prompt
- sql常犯错误之不正确的谓词运算顺序Conversion failed when converting the varchar value 'abc' to data type int
- Failed to get the installation date from registry:Error solution
- Error : Unexpected protocol character/message
- converting to execution character set: Invalid or incomplete multibyte or wide character
- java.lang.String ERROR message="[ERR0125] Error when trying to administer Service
- A network-related or instance-specific error occurred when applicatoin connect to a DB
- 关于conversion from string to type date is not valid错误
- 【支付宝】余额宝团队招聘
- linux进程利用mmap映射区通信
- ListView动态模糊搜索显示ArrayList数据
- ssh暴力破解
- tarjin+dfs 杀人游戏
- 'Conversion failed when converting date and/or time from character string.DB-Lib error message 241,
- OpenCV之轨迹条(TrackBar)的创建和使用
- hbase读写demo
- html a超链接标签
- 图是否是树
- HDU2532 Engine(AC)
- HDU 6052 To my boyfriend
- 异步操作和async函数
- (hdu1394)Minimum Inversion Number (树状数组/线段树/归并排序)