简体   繁体   English

将日期时间转换为不同的格式

[英]Convert datetime to different format

I am using the datediff function and it doesn't seem to be working.我正在使用 datediff 函数,但它似乎不起作用。 It just gives an empty result in the data extension.它只是在数据扩展中给出一个空结果。

What I am currently using我目前使用的是什么

DATEDIFF(year, creation_date__c, SYSDATETIMEOFFSET()) AS AnniversaryYears

creation_date__c is of the following format: Monday, December 05, 2016 12:00 AM creation_date__c 的格式如下:Monday, December 05, 2016 12:00 AM

So when doing this online on w3schools it seems to work:因此,当在 w3schools 上在线执行此操作时,它似乎有效:

SELECT DATEDIFF(year, PARSE(‘Monday, December 05, 2016 12:00 AM’ AS datetime), SYSDATETIMEOFFSET()) AS DateDiff;

But when using the parse function with creation_date__c it gives the following error: An error occurred while checking the query syntax.但是当将 parse 函数与 creation_date__c 一起使用时,它会出现以下错误:检查查询语法时发生错误。 Errors: Argument data type datetime is invalid for argument 1 of parse function.错误:参数数据类型日期时间对于解析函数的参数 1 无效。

Any idea on how to parse it or what I might be doing wrong?关于如何解析它或我可能做错了什么的任何想法?

I believe you are trying to Parse a datetime to a datetime, please see the fix below.我相信您正在尝试将日期时间解析为日期时间,请参阅下面的修复程序。 In SSMS I get the same output from both statements.在 SSMS 中,我从两个语句中得到相同的输出。 To use PARSE(variable as DATETIME) you need to have a variable that is of a type that can be converted to DATETIME.要使用 PARSE(variable as DATETIME),您需要有一个可以转换为 DATETIME 类型的变量。

DECLARE @creation_date__c  VARCHAR(MAX) = 'Monday, December 05, 2016 12:00 AM' ; 

SELECT DATEDIFF(year, PARSE(@creation_date__c as datetime), SYSDATETIMEOFFSET()) AS 
DateDiff

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

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