SQL Server getdate() 转换为类似“2009-12-20”的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1937101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 04:49:26  来源:igfitidea点击:

SQL Server getdate() to a string like "2009-12-20"

sqlsql-servertsql

提问by Adam Kane

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.

在 Microsoft SQL Server 2005 和 .NET 2.0 中,我想将当前日期转换为以下格式的字符串:“YYYY-MM-DD”。例如,2009 年 12 月 12 日将变为“2009-12-20”。我如何在 SQL 中执行此操作。

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.

表定义中此 SQL 语句的上下文。换句话说,这是默认值。因此,当创建新记录时,当前日期的默认值以上述格式存储为字符串。

I'm trying: SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]

我正在尝试: 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]')

但 SQL 服务器不断将其转换为: ('SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]')

so the result is just:

所以结果就是:

'SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]'

'SELECT CONVERT(VARCHAR(10), GETDATE(), 102) AS [YYYY.MM.DD]'

Here's a screen shot of what the Visual Studio server explorer, table, table definition, properties shows:

下面是 Visual Studio 服务器资源管理器、表、表定义、属性显示的屏幕截图:

alt text

替代文字

These wrapper bits are being adding automatically and converting it all to literal string: (N' ')

这些包装器位正在自动添加并将其全部转换为文字字符串:(N' ')

Here's the reason I'm trying to use something other than the basic DATETIME I was using previously:

这就是我尝试使用我之前使用的基本 DATETIME 以外的其他内容的原因:

This is the error I get when hooking everything to an ASP.NET GridView and try to do an update via the grid view:

这是我将所有内容连接到 ASP.NET GridView 并尝试通过网格视图进行更新时遇到的错误:

Server Error in '/' Application.

“/”应用程序中的服务器错误。

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.

正在使用的 SQL Server 版本不支持数据类型“日期”。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

Exception Details: System.ArgumentException: The version of SQL Server in use does not support datatype 'date'.

异常详细信息:System.ArgumentException:正在使用的 SQL Server 版本不支持数据类型“日期”。

Source Error:

源错误:

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.

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常来源和位置的信息。

Stack Trace:

堆栈跟踪:

[ArgumentException: The version of SQL Server in use does not support datatype 'date'.]

[ArgumentException: 正在使用的 SQL Server 版本不支持数据类型“日期”。]

Note: I've added a related question to try to get around the SQL Server in use does not support datatype 'date' errorso that I can use a DATETIME as recommended.

注意:我添加了一个相关问题来尝试解决正在使用SQL Server 不支持数据类型“日期”错误,以便我可以按照建议使用 DATETIME。

回答by amrinder

http://www.sql-server-helper.com/tips/date-formats.aspx

http://www.sql-server-helper.com/tips/date-formats.aspx

For a list of all the date time formats.

获取所有日期时间格式的列表。

回答by gbn

CONVERT(CHAR(10), MyDateValue, 120)

However, why not store date as, er, "date" or "smalldatetime"? And use GETDATE() as default

但是,为什么不将日期存储为“日期”或“小日期时间”?并使用 GETDATE() 作为默认值

Reasons to do it properly

正确执行的原因

  • implicit conversions because of datatype precedence
  • language dependent (don't mention ISO: SQL Server does not play well with date only ISO pre 2008)
  • 10 bytes to store, or 12 as varchar. Worse if time is needed (smalldatetime is 4)
  • 由于数据类型优先级的隐式转换
  • 语言相关(不要提及 ISO:SQL Server 不能很好地处理仅日期 ISO 2008 之前的版本)
  • 要存储 10 个字节,或作为 varchar 存储 12 个字节。如果需要时间则更糟(smalldatetime 为 4)

Edit:

编辑:

You don't use a SELECT for a default. You use a function or a constant.

您不使用 SELECT 作为默认值。您使用函数或常量。

From CREATE TABLE: ...DEFAULT constant_expression...

创建表...DEFAULT constant_expression...

So wrap the CONVERT in a UDF. Or use GETDATE().

因此,将 CONVERT 包装在 UDF 中。或者使用 GETDATE()。

And why are you using style 102?

为什么要使用样式 102?

回答by marc_s

If you REALLYmust store your date as VARCHAR (there's really no good reasons for that....), then you'd have to define this in your table definition:

如果您真的必须将日期存储为 VARCHAR(确实没有充分的理由......),那么您必须在表定义中定义它:

CREATE TABLE yourTable
(........,
 TimestampString NVARCHAR(50) DEFAULT CONVERT(VARCHAR(50), GETDATE(), 102),
 ..........)

Drop the AS [YYYY.MM.DD]part in your statement and you should be fine.

删除AS [YYYY.MM.DD]声明中的部分,你应该没问题。

And please: a date string will never need 1024 characters! Make the NVARCHAR field only as long as they really need to be....

请注意:日期字符串永远不需要 1024 个字符!仅在 NVARCHAR 字段真正需要时才制作它们....

But seriously: I would ALWAYSstore dates as DATETIME fields - if you absolutely need to have a string representation, why not just add a computed field for that:

但说真的:我总是将日期存储为 DATETIME 字段 - 如果您绝对需要一个字符串表示,为什么不为此添加一个计算字段:

ALTER TABLE YourTable
  ADD TimestampString AS CONVERT(NVARCHAR(50), YourDateField, 102) PERSISTED

This way, you get a new computed field "TimestampString", which always represents the 102-style VARCHAR representation of your DATETIME field, and then you can do all date computations on a DATETIME field as it should be.

这样,您将获得一个新的计算字段“TimestampString”,它始终表示 DATETIME 字段的 102 样式 VARCHAR 表示,然后您可以按原样对 DATETIME 字段进行所有日期计算。