简体   繁体   English

使用Convert()函数的T-SQL日期?

[英]T-SQL Dates using Convert() function?

I am bit confusing here? 我在这里有点困惑?

declare @date1 datetime = '2016-01-21 14:10:47.183'

I want to convert '2016-01-21 14:10:47.183' To '21-01-2016' 我想将'2016-01-21 14:10:47.183''21-01-2016'

when I tried: select convert(date,@date1,105) I am getting: 2016-01-21 当我尝试时: select convert(date,@date1,105)我正在: 2016-01-21

But with: select convert(varchar(10),@date1,105) I am getting: 21-01-2016 但随着: select convert(varchar(10),@date1,105)我得到: 21-01-2016

Why I am not having same results with above code? 为什么我的上述代码没有相同的结果?

Why should I convert to varchar ? 为什么要转换为varchar

Thanks in advance 提前致谢

This is just presentation matter and should be done in application layer. 这只是表示问题,应该在应用程序层中完成。 If you cannot do it in application you could use FORMAT (SQL Server 2012+): 如果无法在应用程序中执行此操作,则可以使用FORMAT (SQL Server 2012+):

declare @date1 datetime = '2016-01-21 14:10:47.183'

SELECT FORMAT(@date1, 'dd-mm-yyyy');

LiveDemo


Why I am not having same results with above code? 为什么我的上述代码没有相同的结果?

select convert(date,@date1,105)
-- DATETIME -> DATE
-- vs
select convert(varchar(10),@date1,105)
-- DATETIME -> VARCHAR(10) using specific style

If you only to skip time part use SELECT CAST(@date1 AS DATE) and do not bother how it is presented. 如果仅跳过时间部分,请使用SELECT CAST(@date1 AS DATE)而不必理会它的显示方式。 It is still DATE . 现在仍然是DATE

To sum up: in SQL query use DATE as date, in application display it with desired format. 总结一下:在SQL查询中,使用DATE作为日期,在应用程序中以所需的格式显示它。

The reason why is because once you put a value in a datetime column (or date or any of the other variations on date-time datatypes) in SQL Server. 之所以这样,是因为一旦在SQL Server中将一个值放入datetime列(或date或date-time数据类型中的任何其他变体)中。 SQL Server ceases to think of that date as having any particular format. SQL Server不再认为该日期具有任何特定格式。 It translates it into numbers, and stores it that way internally. 它将其转换为数字,并以内部方式进行存储。

So when you select a date from a date time column, SQL Server displays it in the default format that you have selected based on your environment/local settings. 因此,当您从日期时间列中选择一个日期时,SQL Server将根据您的环境/本地设置以默认格式显示该日期。

If you want to display it in any other format, you have to first convert it to a string, because as far as SQL Server is concerned, dates don't have formats. 如果要以任何其他格式显示它,则必须首先将其转换为字符串,因为就SQL Server而言,日期没有格式。 They are just numbers. 它们只是数字。 The 21st day of March is the 21st day of March, whether you write it as 3/21 or 21/3. 3月21日是3月21日,无论您将其写为3/21还是21/3。

So when you try to convert a date to a date with a different format, SQL Server just ignores you because dates don't have formats. 因此,当您尝试将日期转换为具有其他格式的日期时,SQL Server只会忽略您,因为日期没有格式。 However, if you want to convert that date to a string, SQL Server will be happy to help you display that string in any format you like. 但是,如果要将日期转换为字符串,SQL Server将很乐意帮助您以所需的任何格式显示该字符串。

Hope this helps, but sounds like some further research into how SQL Server stores dates would help your understanding. 希望这会有所帮助,但是听起来像是对SQL Server存储日期的方式进行进一步的研究将有助于您的理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM