在SQLserver中将Datetime转换为日期数据类型[重复]

时间:2023-01-20 16:32:19

This question already has an answer here:

这个问题在这里已有答案:

I have to remove time stamp from a datetime datatype in SQL server and want to keep the output data type as date with this format -> mm/dd/yyyy (This outpu should be displayed as date data type not as a string data type ). I am using SQL server 2012.

我必须从SQL Server中的datetime数据类型中删除时间戳,并希望将输出数据类型保持为此格式的日期 - > mm / dd / yyyy(此outpu应显示为日期数据类型而不是字符串数据类型) 。我正在使用SQL Server 2012。

Please help

请帮忙

Thanks

谢谢

2 个解决方案

#1


4  

This is too long for a comment.

这个评论太长了。

In SQL Server, a date is essentially a datetime with no time component. You can do the conversion simply by doing:

在SQL Server中,日期本质上是一个没有时间组件的日期时间。您只需执行以下操作即可进行转换:

cast(datetimecol as date)

Both, though are stored using an internal format. The output format is controlled by internationalization settings (or the database default). You can output either type of column as a string whose format is mm/dd/yyyy by using conversion format 101:

两者都使用内部格式存储。输出格式由国际化设置(或数据库默认值)控制。您可以使用转换格式101将任一类型的列输出为格式为mm / dd / yyyy的字符串:

select convert(varchar(10), <col>, 101)

The output is then a string with the format you want.

然后输出是具有所需格式的字符串。

#2


0  

Have you tried CONVERT(date, [FieldName]) ?

你试过CONVERT(日期,[FieldName])吗?

Please find documentation on this and CAST() here.

请在此处找到有关此文档和CAST()的文档。

#1


4  

This is too long for a comment.

这个评论太长了。

In SQL Server, a date is essentially a datetime with no time component. You can do the conversion simply by doing:

在SQL Server中,日期本质上是一个没有时间组件的日期时间。您只需执行以下操作即可进行转换:

cast(datetimecol as date)

Both, though are stored using an internal format. The output format is controlled by internationalization settings (or the database default). You can output either type of column as a string whose format is mm/dd/yyyy by using conversion format 101:

两者都使用内部格式存储。输出格式由国际化设置(或数据库默认值)控制。您可以使用转换格式101将任一类型的列输出为格式为mm / dd / yyyy的字符串:

select convert(varchar(10), <col>, 101)

The output is then a string with the format you want.

然后输出是具有所需格式的字符串。

#2


0  

Have you tried CONVERT(date, [FieldName]) ?

你试过CONVERT(日期,[FieldName])吗?

Please find documentation on this and CAST() here.

请在此处找到有关此文档和CAST()的文档。