Sql server 日期列格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5203179/
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 09:35:25  来源:igfitidea点击:

Sql server date Column format

sqlsql-serverdatetime

提问by Irakli Lekishvili

I have one column in SQL Server table with type dateand its default setting is yyyy-MM-dd. I want to chage it like dd-MM-yyyy.

我在 SQL Server 表中有一个类型的列date,它的默认设置是yyyy-MM-dd. 我想改变它dd-MM-yyyy

How can I do it?

我该怎么做?

回答by Guffa

its default setting is yyyy-MM-dd

它的默认设置是 yyyy-MM-dd

No, it's not. There is no formatting information at all associated with the field.

不,这不对。根本没有与该字段相关联的格式信息。

The value is not formatted by the database, it's returned only as a point in time. Formatting that value into it's textual representation is done by the applcation that is getting the data from the database.

该值不是由数据库格式化的,它仅作为一个时间点返回。将该值格式化为它的文本表示是由从数据库中获取数据的应用程序完成的。

So, there is nothing that you can do in the database to change how the date value is formatted, you have to change that where the data is displayed.

因此,您无法在数据库中执行任何操作来更改日期值的格式,您必须更改数据的显示位置。

回答by Muhammad Akhtar

Are you looking for this ?

你在找这个吗?

SELECT convert(varchar, getdate(), 105) – dd-mm-yyyy

and see other format from here http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

并从这里查看其他格式http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

回答by richard

You can format it for output like so:

您可以将其格式化为输出,如下所示:

SELECT convert(varchar, my_date_column, 105)

SELECT convert(varchar, my_date_column, 105)

See herefor different formats.

请参阅此处了解不同的格式。

But what @Guffa says is true.

但@Guffa 说的是真的。

回答by bjorsig

The database stores the data as a point in time. When you select from, for instance using Query Analyzer og SSMS, the format on screen will be dependent on the localization used on the server.

数据库将数据存储为一个时间点。当您选择时,例如使用查询分析器 og SSMS,屏幕上的格式将取决于服务器上使用的本地化。

You should format the date in your application. You can use CONVERT(varchar(19), datecolumn, 105)where 105 is a code for particular format see SQL Server help for more codes. You need to understand that if you do that, you have changed the datatype of the column to varchar, that means you cannot use it for instance to do date calculations or other tasks specific for datetime.

您应该在应用程序中格式化日期。您可以使用CONVERT(varchar(19), datecolumn, 105)其中 105 是特定格式的代码,请参阅 SQL Server 帮助以获取更多代码。您需要了解,如果您这样做,您已将列的数据类型更改为 varchar,这意味着您不能使用它来执行日期计算或其他特定于日期时间的任务。

I believe the best approach is to let the applications handle date formats and let the database simply store it. Most programming languages and reporting tool have much better ways to format dates than SQL Server.

我相信最好的方法是让应用程序处理日期格式并让数据库简单地存储它。大多数编程语言和报告工具都有比 SQL Server 更好的日期格式。

回答by user2671937

I think you can use format() function for that as an alternative way.

我认为您可以使用 format() 函数作为替代方法。

Syntax:

句法:

SELECT FORMAT(column_name,format) FROM table_name;

enter code here (sample below)

在此处输入代码(以下示例)

SELECT ProductName, Price, FORMAT(Now(),'DD-MM-YYYY') AS PerDate FROM Products;

SELECT ProductName, Price, FORMAT(Now(),'DD-MM-YYYY') AS PerDate FROM Products;