繁体   English   中英

编写一个 SQL 查询,从过滤后的日期列中选择数据

[英]Write an SQL query selecting data from filtered Date columns

我正在尝试在 Access 中查询交叉表/记录集(在 Excel 表上尝试过 ADO),其中列中有日期,例如

显示为日期的列

如果我想提取仅包含 2014 年列的有限数据,我该如何编写这样的查询? 请注意,这些列可能会有所不同。

像这样的东西,虽然可能有更简单的方法

Sub Akward_Query()

Dim strSQL As String
Dim strSQL2 As String
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim intYear As Integer

intYear = 2014

strSQL = "Select top 1 * from tbl_Test"

Set rst = New ADODB.Recordset

rst.Open strSQL, CurrentProject.Connection, 1

strSQL2 = "Select "

For Each fld In rst.Fields

    If InStr(1, fld.Name, "Week Ending") > 0 And InStr(1, fld.Name, intYear)>0 Then

        strSQL2 = strSQL2 & "[" & fld.Name & "],"

    End If

Next fld

If Right(strSQL2, 1) = "," Then strSQL2 = Left(strSQL2, Len(strSQL2) - 1)

strSQL2 = strSQL2 & " from tbl_Test"

End Sub

暂无
暂无

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

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