怎么插入datetime 一条数据 sqlserver

 我来答
wangzhiqing999
推荐于2016-11-23 · TA获得超过1.6万个赞
知道大有可为答主
回答量:7048
采纳率:100%
帮助的人:3368万
展开全部
可以使用 CONVERT 函数,将 字符串类型的数据,转换为 datetime 类型。
CONVERT(DATETIME, '2012-01-02 09:06:18', 120)

其中那个 120 的参数, 取决于你前面的字符串信息是什么格式的。

执行下面的语句, 就可以一一对应得上。

SELECT 0 AS ID , Convert(VARCHAR(30), GETDATE(), 0 ) AS result UNION ALL
SELECT 100 AS ID , Convert(VARCHAR(30), GETDATE(), 100) AS result UNION ALL
SELECT 1 AS ID , Convert(VARCHAR(30), GETDATE(), 1 ) AS result UNION ALL
SELECT 101 AS ID , Convert(VARCHAR(30), GETDATE(), 101) AS result UNION ALL
SELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALL
SELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALL
SELECT 2 AS ID , Convert(VARCHAR(30), GETDATE(), 2 ) AS result UNION ALL
SELECT 102 AS ID , Convert(VARCHAR(30), GETDATE(), 102) AS result UNION ALL
SELECT 3 AS ID , Convert(VARCHAR(30), GETDATE(), 3 ) AS result UNION ALL
SELECT 103 AS ID , Convert(VARCHAR(30), GETDATE(), 103) AS result UNION ALL
SELECT 4 AS ID , Convert(VARCHAR(30), GETDATE(), 4 ) AS result UNION ALL
SELECT 104 AS ID , Convert(VARCHAR(30), GETDATE(), 104) AS result UNION ALL
SELECT 5 AS ID , Convert(VARCHAR(30), GETDATE(), 5 ) AS result UNION ALL
SELECT 105 AS ID , Convert(VARCHAR(30), GETDATE(), 105) AS result UNION ALL
SELECT 6 AS ID , Convert(VARCHAR(30), GETDATE(), 6 ) AS result UNION ALL
SELECT 106 AS ID , Convert(VARCHAR(30), GETDATE(), 106) AS result UNION ALL
SELECT 7 AS ID , Convert(VARCHAR(30), GETDATE(), 7 ) AS result UNION ALL
SELECT 107 AS ID , Convert(VARCHAR(30), GETDATE(), 107) AS result UNION ALL
SELECT 8 AS ID , Convert(VARCHAR(30), GETDATE(), 8 ) AS result UNION ALL
SELECT 108 AS ID , Convert(VARCHAR(30), GETDATE(), 108) AS result UNION ALL
SELECT 9 AS ID , Convert(VARCHAR(30), GETDATE(), 9 ) AS result UNION ALL
SELECT 109 AS ID , Convert(VARCHAR(30), GETDATE(), 109) AS result UNION ALL
SELECT 10 AS ID , Convert(VARCHAR(30), GETDATE(), 10 ) AS result UNION ALL
SELECT 110 AS ID , Convert(VARCHAR(30), GETDATE(), 110) AS result UNION ALL
SELECT 11 AS ID , Convert(VARCHAR(30), GETDATE(), 11 ) AS result UNION ALL
SELECT 111 AS ID , Convert(VARCHAR(30), GETDATE(), 111) AS result UNION ALL
SELECT 12 AS ID , Convert(VARCHAR(30), GETDATE(), 12 ) AS result UNION ALL
SELECT 112 AS ID , Convert(VARCHAR(30), GETDATE(), 112) AS result UNION ALL
SELECT 13 AS ID , Convert(VARCHAR(30), GETDATE(), 13 ) AS result UNION ALL
SELECT 113 AS ID , Convert(VARCHAR(30), GETDATE(), 113) AS result UNION ALL
SELECT 14 AS ID , Convert(VARCHAR(30), GETDATE(), 14 ) AS result UNION ALL
SELECT 114 AS ID , Convert(VARCHAR(30), GETDATE(), 114) AS result UNION ALL
SELECT 20 AS ID , Convert(VARCHAR(30), GETDATE(), 20 ) AS result UNION ALL
SELECT 120 AS ID , Convert(VARCHAR(30), GETDATE(), 120) AS result UNION ALL
SELECT 21 AS ID , Convert(VARCHAR(30), GETDATE(), 21 ) AS result UNION ALL
SELECT 121 AS ID , Convert(VARCHAR(30), GETDATE(), 121) AS result
追问
120???
追答
120 的意思就是,你的字符串的格式,必须是  '2012-01-02 09:06:18' 这个样子的。
也就是 年年年年-月月-日日 时时:分分:秒秒 这样的格式。

你要是
SELECT CONVERT(DATETIME, '01/02/12', 120)
那么结果就是
-----------------------
消息 241,级别 16,状态 1,第 1 行
从字符串向 datetime 转换时失败。

但是你指定了正确的参数
SELECT CONVERT(DATETIME, '01/02/12', 1)
-----------------------
2012-01-02 00:00:00.000
(1 行受影响)
jeangu0816
2011-06-22
知道答主
回答量:13
采纳率:0%
帮助的人:9.1万
展开全部
是用java编程吗?是想sqlserver中加一条含有时间类型的数据吗?首先得到当前系统时间,转换成你所有的格式,再转换成java.sql.Date 就可以啦
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式