如何从查询中的SQL Server选择日期格式,如MM/DD/YYYY [英] How to select Date format like MM/DD/YYYY from sql server in query
问题描述
我正在开发一个ASP Web应用程序,因为许多Web窗体都有Date字段,我在sql server 2005数据库中给出日期并在其中存储日期,如MM/DD/YYYY.但是每当我回溯相同的数据并显示在文本框中时,时间日期格式就类似于DD/MM/YYYY.
由于这种格式更改,asp Web应用程序引发了格式异常.
请任何人让我了解如何从sql server中检索MM/DD/YYYY格式的日期值.
谢谢
Uday
Hi,
I am developing an asp web application in that many web forms have Date field, i am giveing date and seveing in sql server 2005 database it storing date like MM/DD/YYYY. But whenever i retraives that same deta and displaying into textbox that time date format is like DD/MM/YYYY.
Because of this format change asp web application throwing format exception.
Please anybody let me to how retrive date value in MM/DD/YYYY format from sql server.
Thanks
Uday
推荐答案
看看使用CAST
和CONVERT
- ^ ].
更具体地说,针对您的情况,请查看代码101.
Have a look at usingCAST
andCONVERT
- http://msdn.microsoft.com/en-us/library/aa226054(v=sql.80).aspx[^].
More specifically for your case, have a look at code 101.
首先,您应确保将数据存储在SQL Date字段中-我假设您已经或不会看到您描述的问题.
其次,您根本不应就显示日期格式做出决定,也不应假定用户也以任何特定格式输入日期.请记住,网站的用户可能在世界任何地方,并且期望以其当前的本地格式输入和显示日期.因此,请不要使用文本框:而是使用日历控件-这还将验证用户输入的日期,以确保它们确实存在.
Firstly, You should make sure that you are storing your data in SQL Date fields - I assume you are or you would not see the problem you describe.
Secondly, you should not be making decisions about display date format at all, and you should not assume that the user is entering dates in any particular format either. Bear in mind that the users of a website could be anywhere in the world, and will expect dates to be entered and displayed in their current local format. So, don''t use a textbox: use a Calender control instead - that will also validate the dates entered by the user to ensure that they actually exist.
在select语句中使用此日期:
use this in your select statement :
CONVERT(char(10), OrderDate, 101) newdate
想更多地了解101?在这里阅读:
http://msdn.microsoft.com/en-us/library/ms187928.aspx [ ^ ]
want to know more about 101 ? read here :
http://msdn.microsoft.com/en-us/library/ms187928.aspx[^]
这篇关于如何从查询中的SQL Server选择日期格式,如MM/DD/YYYY的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!