ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 Excel Home精品图文教程库
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
300集Office 2010微视频教程 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
12
返回列表 发新帖
楼主: ning84

sql where 日期 = ‘2023/6/1’没通过。

[复制链接]

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-14 09:01 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

每天必须学习SQL基本知识,争取十天在进步。
从零开始学习SQL(图文教程)-Excel VBA程序开发-ExcelHome技术论坛 -  https://club.excelhome.net/forum ... =1123322&page=3

image.png


Where and方法调试通过。
Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"

Where Between and 方法没有通过。是不是日期形式只能用Where ## abd ##

Where 日期 Between  #" & Format(oDate, "yyyy/mm") & "# and #" & Format(oDate + 32, "yyyy/mm") & "#"

Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')   From [Sheet1$A1:Z47627] Where 日期 Between  #2022/09# and #2022/10#


  1. Sub SheetToSheet()
  2.     Dim Rs As Recordset, Rs1 As Recordset
  3.     Dim Str, ShtStr
  4.     Dim Sht As Worksheet
  5.     Dim oDate As Date
  6.         Set Sht = Sheet1
  7.         ShtStr = "From [" & Sht.Name & "$]"
  8.         Str = "Select Distinct Format(表名,'yyyy年mm月') " & ShtStr & "Where  表名 Is Not Null "
  9.         Set Rs = SqlRetuRs(Str)
  10.         Sheet2.Cells(6, 1).CopyFromRecordset Rs
  11.         With Rs
  12.             .MoveFirst

  13.             For ii = 0 To .RecordCount - 1
  14.                  oDate = .Fields(0)
  15.                  Str = "Select Format(日期,'yyyy年mm月'), Format(日期,'yyyy年mm月dd日 h:mm:ss') ,目标文件夹 " & _
  16.                         " From [Sheet1$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"
  17.                  'Str = "Select Format(日期,'yyyy年mm月'), 目标文件夹 " & _
  18.                         " From 6[Sheet1$] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"
  19.                  Str = "Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')  " & _
  20.                         " From [Sheet1$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"
  21.                  Str = "Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')  " & _
  22.                         " From [Sheet1$A1:Z47627] Where 日期 Between  #" & Format(oDate, "yyyy/mm") & "# and #" & Format(oDate + 32, "yyyy/mm") & "#"
  23.                  Debug.Print Str
  24.                  Sheet1.Activate
  25.                  Set Rs1 = SqlRetuRs(Str)
  26.                  With Rs1
  27.                       Str = .Fields(1)
  28.                       Set Sht = ThisWorkbook.Sheets(Str)
  29.                       With Sht
  30.                            .Activate
  31.                            .Cells.Clear
  32.                            .Cells.Font.Size = 7
  33.                       End With
  34.                  End With
  35.                  Sht.Cells(4, 1).CopyFromRecordset Rs1
  36.                  .MoveNext
  37.             Next ii
  38.         End With
  39. End Sub
复制代码



TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-14 09:10 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
Where Between and 语句调试日期没有通过。



                 Str = "Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')  " & _
                        " From [Sheet1$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and
日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"
                 Str = "Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')  " & _
                        " From [Sheet1$A1:Z47627] Where 日期 Between  '#" & Format(oDate, "yyyy/mm") & "#' and '#" & Format(oDate + 32, "yyyy/mm") & "#'"

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-14 09:34 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助

感觉语句没问题。结果不通过。
Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')   From [Sheet1$A1:Z47627] Where 日期 Between  #2022/09/1# and #2022/10/1#
’‘
Select 目标文件夹, Format(日期,'yyyy年mm月dd日 h:mm:ss'),Format(日期,'yyyy年mm月')   From [Sheet1$A1:Z47627] Where 日期 Between  '#2022/09#' and '#2022/10#'
image.png





由好好学习了------------从零开始学习SQL(图文教程)-Excel VBA程序开发-ExcelHome技术论坛 -  https://club.excelhome.net/forum ... =1123322&page=3
感觉我的语句没问题。
select * from [Sheet1$A:D] where 出生年月 between #1982/1/1# and #1986/1/1#








  1. Option Explicit

  2. Sub Test4()
  3.     Dim Conn As Object, Rst As Object
  4.     Dim strConn As String, strSQL As String
  5.     Dim i As Integer, PathStr As String
  6.     Set Conn = CreateObject("ADODB.Connection")
  7.     Set Rst = CreateObject("ADODB.Recordset")
  8.     PathStr = ThisWorkbook.FullName   '设置工作簿的完整路径和名称
  9.     Select Case Application.Version * 1    '设置连接字符串,根据版本创建连接
  10.     Case Is <= 11
  11.         strConn = "Provider=Microsoft.Jet.Oledb.4.0;Extended Properties=excel 8.0;Data source=" & PathStr
  12.     Case Is >= 12
  13.         strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & PathStr & ";Extended Properties=""Excel 12.0;HDR=YES"";"""
  14.     End Select
  15.     '设置SQL查询语句
  16.     Conn.Open strConn    '打开数据库链接
  17.     strSQL = "select * from [Sheet1$A:D] where 出生年月 between #" & Sheet1.Range("G3") & "# and #" & Sheet1.Range("H3") & "#"
  18.     Debug.Print strSQL
  19.     Stop
  20.     'strSQL = "select * From [sheet1$A:D] where 出生年月 Between (select * From [sheet1$G2:G3]) and (select * From [sheet1$H2:H3])"
  21.     'strSQL = "select * From [sheet1$A:D] where 出生年月>=(select * From [sheet1$G2:G3]) and 出生年月<=(select * From [sheet1$H2:H3])"
  22.     Set Rst = Conn.Execute(strSQL)    '执行查询,并将结果输出到记录集对象
  23.     With Sheet1.Range("J:M")
  24.         .Cells.Clear
  25.         For i = 0 To Rst.Fields.Count - 1    '填写标题
  26.             .Cells(1, i + 1) = Rst.Fields(i).Name
  27.         Next i
  28.         .Range("A2").CopyFromRecordset Rst
  29.         .Cells.EntireColumn.AutoFit  '自动调整列宽
  30.     End With
  31.     Rst.Close    '关闭数据库连接
  32.     Conn.Close
  33.     Set Conn = Nothing
  34.     Set Rst = Nothing
  35. End Sub
复制代码





SQL查询二个日期范围之内的数据.rar

23.08 KB, 下载次数: 1

TA的精华主题

TA的得分主题

发表于 2024-5-11 00:09 | 显示全部楼层
两个日期之间的日期或时间, 日期字段格式可以是日期,可以是文本
Where DateDiff('D','2022/09/1',日期)>=0  and  DateDiff('D',日期,'2022/10/1')>=0
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2024-6-4 10:05 , Processed in 0.035864 second(s), 8 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表