两个日期之间的日期差,格式为01/06/18

时间:2018-11-09 00:36:05

标签: mysql sql

大家好,我试图获取两个日期之间的日期差。我从多维数据集中获取数据并保存在临时表中。我试图将日期字段定义为varchar以及datetime和date 我的日期格式为03/04/18 我正在尝试Sub UpdateReturns() Dim stPath As String, mailfile As String, Fname As String, Ename As String Dim fso As Object, fld As Object Dim i As Integer, count As Integer Const strSearch = "From:" stPath = "C:\010. Working Docs" Set fso = CreateObject("Scripting.FileSystemObject") Set fld = fso.GetFolder(stPath) mailfile = Dir(stPath & "\Approve*.txt") count = 0 Do While mailfile <> "" count = count + 1 mailfile = Dir() Loop i = 0 mailfile = Dir(stPath & "\Approve*.txt") Do While count >= 1 Open mailfile For Input As #1 Do Until EOF(1) Line Input #1, textline If InStr(textline, "From:") > 0 Then Fname = mailfile Ename = textline End If Loop Close #1 Range("C" & (40 + count)).Value = Fname Range("D" & (40 + count)).Value = Ename mailfile = Dir() count = count - 1 Loop End Sub 以获取天数差异。但是出现以下错误。

当我在临时表中将日期字段定义为DateTime或Date时 “操作符类型冲突:ntext与日期时间不兼容” 当我在临时表中将日期字段定义为Varchar时 “从字符串转换日期和/或时间时转换失败。”

预先感谢

2 个答案:

答案 0 :(得分:1)

我建议您使用str_to_date()和适当的格式修饰符将这些字符串转换为日期。

DATEDIFF(str_to_date(PAAR.DateReceived,'%d/%m/%y'), str_to_date(O.DateReceived,'%d/%m/%y'))

请参阅:https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date

答案 1 :(得分:-1)

尝试一下

set @sdate = '01/01/18';
set @edate = '01/26/18';

set @sdate = CONCAT_WS('-',SUBSTR(@sdate FROM 7 FOR 2),SUBSTR(@sdate FROM 1 FOR 2),SUBSTR(@sdate FROM 4 FOR 2));
set @edate = CONCAT_WS('-',SUBSTR(@edate FROM 7 FOR 2),SUBSTR(@edate FROM 1 FOR 2),SUBSTR(@edate FROM 4 FOR 2));

SELECT DATEDIFF(date(@edate), date(@sdate));