問題描述
在 Microsoft SQL Server 2005 和 .NET 2.0 中,我想將當(dāng)前日期轉(zhuǎn)換為以下格式的字符串:YYYY-MM-DD".例如,2009 年 12 月 12 日將變?yōu)?009-12-20".我如何在 SQL 中執(zhí)行此操作.
In Microsoft SQL Server 2005 and .NET 2.0, I want to convert the current date to a string of this format: "YYYY-MM-DD". For example, December 12th 2009 would become "2009-12-20". How do I do this in SQL.
表定義中此 SQL 語句的上下文.換句話說,這是默認(rèn)值.因此,當(dāng)創(chuàng)建新記錄時,當(dāng)前日期的默認(rèn)值以上述格式存儲為字符串.
The context of this SQL statement in the table definiton. In other words, this is the default value. So when a new record is created the default value of the current date is stored as a string in the above format.
我正在嘗試:SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]
I'm trying: SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]
但 SQL 服務(wù)器不斷將其轉(zhuǎn)換為:('SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]')
But SQL server keeps converting that to: ('SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]')
所以結(jié)果就是:
'SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]'
'SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]'
以下是 Visual Studio 服務(wù)器資源管理器、表、表定義、屬性顯示的屏幕截圖:
Here's a screen shot of what the Visual Studio server explorer, table, table definition, properties shows:
這些包裝器位正在自動添加并將其全部轉(zhuǎn)換為文字字符串:(N' ')
These wrapper bits are being adding automatically and converting it all to literal string: (N' ')
這就是我嘗試使用我之前使用的基本 DATETIME 以外的其他內(nèi)容的原因:
Here's the reason I'm trying to use something other than the basic DATETIME I was using previously:
這是我將所有內(nèi)容連接到 ASP.NET GridView 并嘗試通過網(wǎng)格視圖進(jìn)行更新時遇到的錯誤:
This is the error I get when hooking everything to an ASP.NET GridView and try to do an update via the grid view:
正在使用的 SQL Server 版本不支持?jǐn)?shù)據(jù)類型日期".說明:在執(zhí)行當(dāng)前 Web 請求期間發(fā)生未處理的異常.請查看堆棧跟蹤以獲取有關(guān)錯誤及其在代碼中的來源的更多信息.
The version of SQL Server in use does not support datatype 'date'. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
異常詳細(xì)信息:System.ArgumentException:正在使用的 SQL Server 版本不支持?jǐn)?shù)據(jù)類型日期".
Exception Details: System.ArgumentException: The version of SQL Server in use does not support datatype 'date'.
源錯誤:
在執(zhí)行當(dāng)前 Web 請求期間生成了未處理的異常.可以使用下面的異常堆棧跟蹤來識別有關(guān)異常來源和位置的信息.
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
堆棧跟蹤:
[ArgumentException: 正在使用的 SQL Server 版本不支持?jǐn)?shù)據(jù)類型日期".]
[ArgumentException: The version of SQL Server in use does not support datatype 'date'.]
注意:我添加了一個相關(guān)問題來嘗試解決 正在使用的 SQL Server 不支持?jǐn)?shù)據(jù)類型日期"錯誤,因此我可以按照建議使用 DATETIME.
Note: I've added a related question to try to get around the SQL Server in use does not support datatype 'date' error so that I can use a DATETIME as recommended.
推薦答案
http://www.sql-server-helper.com/tips/date-formats.aspx
獲取所有日期時間格式的列表.
For a list of all the date time formats.
這篇關(guān)于SQL Server getdate() 轉(zhuǎn)換為類似“2009-12-20"的字符串的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!