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

SQL Server getdate() to a string like quot;2009-12-20quot;(SQL Server getdate() 转换为类似“2009-12-20的字符串)
本文介绍了SQL Server getdate() 转换为类似“2009-12-20"的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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 语句的上下文.换句话说,这是默认值.因此,当创建新记录时,当前日期的默认值以上述格式存储为字符串.

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 服务器不断将其转换为:('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]')

所以结果就是:

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

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

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

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

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

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

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

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

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

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 版本不支持数据类型日期".说明:在执行当前 Web 请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.

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.

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

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

源错误:

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

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 版本不支持数据类型日期".]

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

注意:我添加了一个相关问题来尝试解决 正在使用的 SQL Server 不支持数据类型日期"错误,因此我可以按照建议使用 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.

这篇关于SQL Server getdate() 转换为类似“2009-12-20"的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Number of working days between two dates(两个日期之间的工作日数)
How do I use dateadd to get the first day of last year?(如何使用 dateadd 获取去年的第一天?)
SQL- Count occurrences of a specific word within all stored procedures(SQL- 计算所有存储过程中特定单词的出现次数)
SQL query to make a column of numbers a string(使一列数字成为字符串的 SQL 查询)
T-SQL: Best way to replace NULL with most recent non-null value?(T-SQL:用最新的非空值替换 NULL 的最佳方法?)
Count days in date range with set of exclusions which may overlap(使用一组可能重叠的排除项计算日期范围内的天数)