如何在VBA(Excel)中使用String.Format进行SQL查询

时间:2021-10-06 20:23:36

I'll have a query, lets say something like:

我会有一个查询,让我们说:

"SELECT * FROM MY_TABLE WHERE DATE BETWEEN {1} AND {2}"

Once I have it, I have to put my own dates, something like:

一旦我拥有它,我必须把我自己的日期,如:

String.Format("SELECT * FROM MY_TABLE WHERE DATE BETWEEN {1} AND {2}", Date1, Date2)

Merci in advance!

谢谢提前!

1 个解决方案

#1


1  

What you have shown does works fine. Just that the place holder variable starts at 0. Your code line should look like below considering that Date1 and Date2 are datetime variable and does exists.

你所展示的作品确实很好。只是占位符变量从0开始。考虑到Date1和Date2是datetime变量并且确实存在,您的代码行应如下所示。

String.Format("SELECT * FROM MY_TABLE WHERE DATE BETWEEN {0} AND {1}", Date1, Date2)

#1


1  

What you have shown does works fine. Just that the place holder variable starts at 0. Your code line should look like below considering that Date1 and Date2 are datetime variable and does exists.

你所展示的作品确实很好。只是占位符变量从0开始。考虑到Date1和Date2是datetime变量并且确实存在,您的代码行应如下所示。

String.Format("SELECT * FROM MY_TABLE WHERE DATE BETWEEN {0} AND {1}", Date1, Date2)