简体   繁体   English

如何在SQL Server 2012中使用转换功能?

[英]How to use convert function in SQL Server 2012?

I need to set the hours, minutes and seconds in a certain datetime column to 00:00:00.0 after copying datetime values to it from another column. 从另一列复制datetime值后,我需要将某个datetime列中的小时,分​​钟和秒设置为00:00:00.0。

In the table assignments I have a datetime column activation_date , into which I just copied datetimes from another datetime column creation_date for rows that have null as their activation_date values. 在表assignments我有一个datetime列activation_date ,我刚刚从另一个datetime列creation_date复制了datetimes,以获取具有null作为activation_date值的行。 Both columns are in the same table. 两列都在同一表中。 With the following: 具有以下内容:

UPDATE assignments 
SET activation_date = creation_date
WHERE activation_date IS NULL;

Now, the hours, minutes and seconds in the activation_date column need to be always 0:00:00.0, which isn't the case in the creation_date values. 现在, activation_date列中的小时,分​​钟和秒必须始终为0:00:00.0,而不是creation_date值中的情况。

For example: A copied value is 2015-10-08 12:09:45.743 , but I want it to be 2015-10-08 00:00:00.0 in its new home, which is the activation_date column. 例如:一个复制的值是2015-10-08 12:09:45.743 ,但我想这是2015-10-08 00:00:00.0在新的家园,这是activation_date列。

I found out, that there's a function in SQL Server 2008 and above for this, called 我发现,SQL Server 2008及更高版本中有一个名为

SELECT CONVERT(Date, GETDATE())

As a complete SQL noob that I am, I can do the selection with said conversion as follows: 作为我的完整SQL noob,我可以按如下所示进行选择:

SELECT CONVERT(date, activation_date) AS activation_date 
FROM assignments

Now, being a mere selection, this of course doesn't change anything. 现在,仅作为选择,这当然不会改变任何东西。 How I'm supposed to use this function in a UPDATE and SET manner, is beyond me. 我应该如何以UPDATE和SET方式使用此功能,这超出了我的范围。 Any help is appreciated. 任何帮助表示赞赏。 Thank you. 谢谢。

Since you are using SQL 2012 version, how about use the FORMAT function and do something like this ? 由于您使用的是SQL 2012版本,因此如何使用FORMAT函数并执行类似的操作?

SELECT FORMAT(@date, 'yyyy-MM-dd 00:00:00.0')

result will be something like below: 结果将如下所示:

result: 2016-02-16 00:00:00.0

And then your update will be something like below: 然后您的更新将如下所示:

UPDATE assignments 
SET activation_date = FORMAT(creation_date, 'yyyy-MM-dd 00:00:00.0')
WHERE activation_date IS NULL;

For some format reference , i forgot the link where i found this, but i've saved this on my SQL snippets so i am sharing you the below different formats that you can use :) 对于某些格式参考,我忘记了找到该链接的链接,但是我已将其保存在我的SQL代码段中,因此我与您分享以下可以使用的不同格式:)

/*
FORMAT(). In my mind, this single function is one of the hands-down best new features of 
SQL Server 2012, simply because the functionality that it provides has been so sorely needed 
for so long. And for .NET developers, the immediate and obvious benefits of this new function 
should be readily apparent just by looking at Figure 6. Another thing that I like about the new 
FORMAT() function is that it represents an additional influx of CLR functionality directly into 
T-SQL -- something that I hope to see more of in the future.
*/

SELECT
        FORMAT(GETDATE(), 'yyyy-MM-dd') AS [ISO Formatted Date],
        FORMAT(GETDATE(), 'yyyy-MM-dd hh:mm:ss') AS [Full ISO],
        FORMAT(GETDATE(), 'MMMM dd, yyyy') AS [Long-hand Date (EN)],
        FORMAT(GETDATE(), 'MMMM dd, yyyy', 'fr-FR') AS [French Date],
        FORMAT(22.7, 'C', 'en-US') AS [US Currency],
        FORMAT(22.7, 'C', 'en-GB') AS [UK Currency],
        FORMAT(99 * 2.226, '000.000') AS [Padded Decimal],
        FORMAT(12345678, '0,0') AS [Finally: Commas in Large Numbers]
;

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

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