如何在Excel中更改date以便date在SQL查询上运行?

目前,我有一个Excel工作表,其中我input一个开始date和结束date将转到VBA代码,并在SQL查询input它。 我目前遇到的问题是我收到一个错误:

ORA-01843不是一个有效的月份,因为Excel导入的date为05/01/2016 ,查询需要为05-may-2016

我如何改变?

以下是开始和结束date的代码:

 startDate = Worksheets("Sheet1").Range("B4").Text endDate = Worksheets("Sheet1").Range("B6").Text dbConnectStr = "Provider=msdaora;User Id=" & Uname dbConnectStr1 = "Provider=msdaora;User Id=xxendur ;Data Source=" & DSN Set Sql.ActiveConnection = objmyconn Sql.CommandText = "select system_date from syit_act_log where system_date between`enter code here` 'startDate' AND 'endDate' and action_id = 15 and log_desc not like '%svc_openlink_p%' order by system_date" Sql.CommandType = adCmdText Sql.Execute 

您需要使用以下格式设置您的datevariables:

 startDate = Format(Worksheets("Sheet1").Range("B4").Value2, "dd-mmm-yyyy") endDate = Format(Worksheets("Sheet1").Range("B6").Value2, "dd-mmm-yyyy") 'startDate and endDate will be look like "05-May-2016" 

三倍嗯会给你你所要求的月份名称。 然后在你的查询中使用这些variables。

编辑:

由于Ralph和Jeepedbuild议使用yyyymmdd标准

 startDate = Format(Worksheets("Sheet1").Range("B4").Value2, "yyyymmdd") endDate = Format(Worksheets("Sheet1").Range("B6").Value2, "yyyymmdd")