SQL Query doing strange thing

雨燕双飞 提交于 2019-12-23 04:51:27

问题


If I run this query on SQL Server Express 2008 :

Insert NoteBook (Date, Note) Values ('11/04/2011 11:02:46', 'test')

It stored the date as 04/11/2011

How can I prevent this?


回答1:


Use the ISO-8601 format: YYYYMMDD (or YYYY-MM-DDTHH:MM:SS) - it works always, regardless of your SQL Server language and locale settings.

INSERT INTO dbo.NoteBook(Date, Note) 
VALUES('2011-04-11T11:02:46', 'test')

The date in SQL Server is NOT stored in any particular string-oriented format - a date is a date is a date, regardless of what you see.

You see a string representation of the date - but again: it's NOT stored that way - and thus you cannot "prevent" it from being stored that way...

Check your language settings in SQL Server:

SELECT @@LANGUAGE

What language do you have?? The language defines the default format in which dates are shown.



来源:https://stackoverflow.com/questions/5619877/sql-query-doing-strange-thing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!