Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

sql - SQL查询以在SQL Server中插入日期时间(Sql query to insert datetime in SQL Server)

I want to insert a datetime value into a table(SQL Server) using the sql query below

(我想使用下面的SQL查询将datetime值插入表(SQL Server)中)

insert into table1(approvaldate)values(18-06-12 10:34:09 AM);

But I get this Error msg.

(但我收到此错误消息。) Incorrect syntax near '10'.

I tried it with the quotes

(我用引号尝试过)

insert into table1(approvaldate)values('18-06-12 10:34:09 AM');

I get this error message Cannot convert varchar to datetime

(我收到此错误消息Cannot convert varchar to datetime)

Kindly help!

(请帮助!)

Thanks.

(谢谢。)

  ask by Shee translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You will want to use the YYYYMMDD for unambiguous date determination in SQL Server.

(您将需要使用YYYYMMDD来确定SQL Server中的明确日期。)

insert into table1(approvaldate)values('20120618 10:34:09 AM');

If you are married to the dd-mm-yy hh:mm:ss xm format, you will need to use CONVERT with the specific style.

(如果您已采用dd-mm-yy hh:mm:ss xm格式,则需要使用具有特定样式的CONVERT。)

insert table1 (approvaldate)
       values (convert(datetime,'18-06-12 10:34:09 PM',5));

5 here is the style for Italian dates.

(5是意大利约会的风格。)

Well, not just Italians, but that's the culture it's attributed to in Books Online .

(好吧,不仅是意大利语,这就是它在Online Books中归因于的文化。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...