ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

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

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

[复制链接]

TA的精华主题

TA的得分主题

发表于 2023-10-6 09:29 | 显示全部楼层 |阅读模式
image.png



最简单的SQL,怎么没通过。
Str = "Select   日期 From [TraverseFolderFile$A1:Z47627] Weree 日期 = '#2023/6/1#'"
Str = "Select   日期 From [TraverseFolderFile$A1:Z47627] Weree format(日期,'yyyy/m/d') = format('2023/6/1','yyyy/m/d') "


image.png

TA的精华主题

TA的得分主题

发表于 2023-10-6 09:51 | 显示全部楼层
本帖最后由 f8b1987 于 2023-10-6 13:43 编辑

日期不用加引号,这个带时间的,最好是取整

image.png

TA的精华主题

TA的得分主题

发表于 2023-10-6 09:52 | 显示全部楼层
日期 >= '#2023/6/1#' and 日期 < '#2023/7/1#'"

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-6 13:15 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
f8b1987 发表于 2023-10-6 09:51
日期不用加引号,这个带时间的,最好是取整


谢谢回复,下面语句通过,但格式转换比骄麻烦。

Select   * From [TraverseFolderFile$A1:A47627] Where 日期 = 45078.4746990741


image.png



麻烦给个例子,加深理解。


下面语句没有通过。



Str = "Select   * From [TraverseFolderFile$] Weree 日期 >= '#2023/6/1#' and 日期 < '#2023/7/1#'"
Str = "Select   日期 From [TraverseFolderFile$A1:A47627] Weree 日期 >= '#2023/6/1#' and 日期 < '#2023/7/1#'"
Str = "Select   日期 From [TraverseFolderFile$A1:A47627] Weree 日期 = '#2023/6/1#' "
Str = "Select   * From [TraverseFolderFile$A1:A47627] Weree 日期 >= '#2023/6/1#' and 日期 < '#2023/7/1#'"


TA的精华主题

TA的得分主题

发表于 2023-10-6 13:43 | 显示全部楼层
参考
    ' 构建查询语句
    strSQL = "SELECT * FROM [SS$A1:A23] WHERE [日期时间] > #2023-09-05# AND [日期时间] < #2023-09-10#"

TA的精华主题

TA的得分主题

发表于 2023-10-6 16:20 | 显示全部楼层
"select * From [Sheet1$A:A] where int(日期)=#2023-10-06#"
GIF 2023-10-06 16-20-11.gif

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-6 18:44 | 显示全部楼层
limonet 发表于 2023-10-6 16:20
"select * From [Sheet1$A:A] where int(日期)=#2023-10-06#"

谢谢回复

  1. Function MergeDate(Rs As Recordset, Sht As Worksheet)
  2.     Dim Rng As Range
  3.         With Sht
  4.               Set Rng = .Cells(.Cells(65536, 2).End(xlUp).Row + 2, 1)
  5.               Debug.Print Rng.Address
  6.               Rng.CopyFromRecordset Rs
  7.               Set Rng = Rng.Resize(Rs.RecordCount + 0, 1)
  8.               Rng.Select
  9.               Rng.Merge
  10.               Set Rng = Rng.CurrentRegion
  11.               Rng.Select
  12. ''
  13.     With Selection.Borders(xlEdgeLeft)
  14.         .LineStyle = xlContinuous
  15.         .ColorIndex = xlAutomatic
  16.         .TintAndShade = 0
  17.         .Weight = xlMedium
  18.     End With
  19.     With Selection.Borders(xlEdgeTop)
  20.         .LineStyle = xlContinuous
  21.         .ColorIndex = xlAutomatic
  22.         .TintAndShade = 0
  23.         .Weight = xlMedium
  24.     End With
  25.     With Selection.Borders(xlEdgeBottom)
  26.         .LineStyle = xlContinuous
  27.         .ColorIndex = xlAutomatic
  28.         .TintAndShade = 0
  29.         .Weight = xlMedium
  30.     End With
  31.     With Selection.Borders(xlEdgeRight)
  32.         .LineStyle = xlContinuous
  33.         .ColorIndex = xlAutomatic
  34.         .TintAndShade = 0
  35.         .Weight = xlMedium
  36.     End With
  37.     With Selection.Borders(xlInsideVertical)
  38.         .LineStyle = xlContinuous
  39.         .ColorIndex = xlAutomatic
  40.         .TintAndShade = 0
  41.         .Weight = xlMedium
  42.     End With
  43.     With Selection.Borders(xlInsideHorizontal)
  44.         .LineStyle = xlContinuous
  45.         .ColorIndex = xlAutomatic
  46.         .TintAndShade = 0
  47.         .Weight = xlMedium
  48.     End With
  49. '''
  50.         End With
  51. End Function


  52. Private Sub dSqlCreateFolder()
  53.     Application.DisplayAlerts = False

  54.     Dim oFolder As Folder, oFile As File, oFile1 As File
  55.     Dim Fso As Scripting.FileSystemObject
  56.         Set Fso = New Scripting.FileSystemObject
  57.     Dim Sht As Worksheet
  58.         Set Sht = Sheet4
  59.         Set Sht = Sheets("TraverseFolderFile")
  60.         Sht.Activate
  61.     Dim Rng As Range, oRng As Range
  62.         Set Rng = Sht.Range("A1:Z" & Sht.Cells(65536, 1).End(xlUp).Row + 10)
  63.         Debug.Print Rng.Address
  64.     Dim Rs As Recordset, Rs1 As Recordset, Str
  65.     Dim oPath, oPath1
  66.     Dim oDate As Date
  67.         ''
  68.         With Sheet2
  69.             .Activate
  70.             .Cells.Clear
  71.             .Cells.Font.Size = 9
  72.             ''
  73.             Str = "Select Distinct  Format(日期,'yyyy-mm')" & _
  74.                " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] "
  75.             'Debug.Print Str
  76.             ''
  77.             Set Rs = SqlRetuRs(Str)
  78.             .Cells(2, 20).CopyFromRecordset Rs
  79.             
  80.             Rs.MoveFirst
  81.             
  82.             For ii = 0 To Rs.RecordCount - 1
  83.                If IsDate(Rs.Fields(0)) Then
  84.                     oDate = Rs.Fields(0)
  85.                     Str = "Select Format(日期,'yyyy-mm'),  Format(日期,'yyyy-mm-dd')" & _
  86.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm/dd") & "# and 日期 < #" & Format(oDate + 1, "yyyy/mm/dd") & "#"
  87.                     Str = "Select Format(日期,'yyyy-mm'),  Format(日期,'yyyy-mm-dd')" & _
  88.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 1, "yyyy/mm") & "#"

  89.                     Str = "Select distinct Format(日期,'yyyy年mm月'),  Format(日期,'yyyy年mm月dd日')" & _
  90.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"

  91.                         Set Rs1 = SqlRetuRs(Str)
  92.                         If Rs1.RecordCount > 0 Then
  93.                             MergeDate Rs1, Sheet2
  94.                         End If
  95.                         
  96.                 End If
  97.                 Rs.MoveNext
  98.             Next ii
  99.        End With
  100.        Application.DisplayAlerts = True

  101. End Sub
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-6 19:03 | 显示全部楼层
limonet 发表于 2023-10-6 16:20
"select * From [Sheet1$A:A] where int(日期)=#2023-10-06#"

谢谢回复,各位的回复。

image.png






  1. Function MergeDate(Rs As Recordset, Sht As Worksheet)
  2.     Dim Rng As Range
  3.         With Sht
  4.               Set Rng = .Cells(.Cells(65536, 2).End(xlUp).Row + 2, 1)
  5.               Debug.Print Rng.Address
  6.               Rng.CopyFromRecordset Rs
  7.               Set Rng = Rng.Resize(Rs.RecordCount + 0, 1)
  8.               Rng.Select
  9.               Rng.Merge
  10.               Set Rng = Rng.CurrentRegion
  11.               Rng.Select
  12. ''
  13.     With Selection.Borders(xlEdgeLeft)
  14.         .LineStyle = xlContinuous
  15.         .ColorIndex = xlAutomatic
  16.         .TintAndShade = 0
  17.         .Weight = xlMedium
  18.     End With
  19.     With Selection.Borders(xlEdgeTop)
  20.         .LineStyle = xlContinuous
  21.         .ColorIndex = xlAutomatic
  22.         .TintAndShade = 0
  23.         .Weight = xlMedium
  24.     End With
  25.     With Selection.Borders(xlEdgeBottom)
  26.         .LineStyle = xlContinuous
  27.         .ColorIndex = xlAutomatic
  28.         .TintAndShade = 0
  29.         .Weight = xlMedium
  30.     End With
  31.     With Selection.Borders(xlEdgeRight)
  32.         .LineStyle = xlContinuous
  33.         .ColorIndex = xlAutomatic
  34.         .TintAndShade = 0
  35.         .Weight = xlMedium
  36.     End With
  37.     With Selection.Borders(xlInsideVertical)
  38.         .LineStyle = xlContinuous
  39.         .ColorIndex = xlAutomatic
  40.         .TintAndShade = 0
  41.         .Weight = xlMedium
  42.     End With
  43.     With Selection.Borders(xlInsideHorizontal)
  44.         .LineStyle = xlContinuous
  45.         .ColorIndex = xlAutomatic
  46.         .TintAndShade = 0
  47.         .Weight = xlMedium
  48.     End With
  49. '''
  50.         End With
  51. End Function


  52. Private Sub dSqlCreateFolder()
  53.     Application.DisplayAlerts = False

  54.     Dim oFolder As Folder, oFile As File, oFile1 As File
  55.     Dim Fso As Scripting.FileSystemObject
  56.         Set Fso = New Scripting.FileSystemObject
  57.     Dim Sht As Worksheet
  58.         Set Sht = Sheet4
  59.         Set Sht = Sheets("TraverseFolderFile")
  60.         Sht.Activate
  61.     Dim Rng As Range, oRng As Range
  62.         Set Rng = Sht.Range("A1:Z" & Sht.Cells(65536, 1).End(xlUp).Row + 10)
  63.         Debug.Print Rng.Address
  64.     Dim Rs As Recordset, Rs1 As Recordset, Str
  65.     Dim oPath, oPath1
  66.     Dim oDate As Date
  67.         ''
  68.         With Sheet2
  69.             .Activate
  70.             .Cells.Clear
  71.             .Cells.Font.Size = 9
  72.             ''
  73.             Str = "Select Distinct  Format(日期,'yyyy-mm')" & _
  74.                " From [" & Sht.Name & "$" & Rng.Address(0, 0) & "] "
  75.             'Debug.Print Str
  76.             ''
  77.             Set Rs = SqlRetuRs(Str)
  78.             .Cells(2, 20).CopyFromRecordset Rs
  79.             
  80.             Rs.MoveFirst
  81.             
  82.             For ii = 0 To Rs.RecordCount - 1
  83.                If IsDate(Rs.Fields(0)) Then
  84.                     oDate = Rs.Fields(0)
  85.                     Str = "Select Format(日期,'yyyy-mm'),  Format(日期,'yyyy-mm-dd')" & _
  86.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm/dd") & "# and 日期 < #" & Format(oDate + 1, "yyyy/mm/dd") & "#"
  87.                     Str = "Select Format(日期,'yyyy-mm'),  Format(日期,'yyyy-mm-dd')" & _
  88.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 1, "yyyy/mm") & "#"

  89.                     Str = "Select distinct Format(日期,'yyyy年mm月'),  Format(日期,'yyyy年mm月dd日')" & _
  90.                         " From [TraverseFolderFile$A1:Z47627] Where 日期 >= #" & Format(oDate, "yyyy/mm") & "# and 日期 < #" & Format(oDate + 32, "yyyy/mm") & "#"
  91.                     'Str = "Select distinct Format(日期,'yyyy年mm月'),  Format(日期,'yyyy年mm月dd日')" & _
  92.                         " From [TraverseFolderFile$A1:Z47627] Where int(日期)= #" & Format(oDate, "yyyy/mm") & "#"

  93.                         Set Rs1 = SqlRetuRs(Str)
  94.                         If Rs1.RecordCount > 0 Then
  95.                             MergeDate Rs1, Sheet2
  96.                         End If
  97.                         
  98.                 End If
  99.                 Rs.MoveNext
  100.             Next ii
  101.        End With
  102.        Application.DisplayAlerts = True

  103. End Sub
复制代码

TA的精华主题

TA的得分主题

发表于 2023-10-7 09:22 | 显示全部楼层
#是日期型,你再加个单引号又是字符型了;去掉单引号试试呢
Str = "Select   日期 From [TraverseFolderFile$A1:Z47627] Weree 日期 = #2023/6/1#"

TA的精华主题

TA的得分主题

 楼主| 发表于 2023-10-8 10:31 | 显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用  · 内置多项VBA编程加强工具       ★ 免费下载 ★      ★使用手册
  1. Sub ShtMoveJPGtoFolder()
  2.      Dim T: T = Time
  3.          Debug.Print T
  4.      Dim Fso As FileSystemObject
  5.          Set Fso = New FileSystemObject
  6.      Dim Str, Str1
  7.      Dim Sht As Worksheet
  8.      Dim Rng As Range
  9.      Dim Rs As Recordset
  10.           Set Sht = Sheet2
  11.           Set Rng = Sht.Cells(900000, 1)
  12.           Str1 = " From [" & Sht.Name & "$A1:Z" & Rng.End(xlUp).Row & "] Where 日期 <> Null"
  13.           Str = "Select Format(日期,'yyyy年mm月'),  Format(日期,'yyyy年mm月dd日')" & _
  14.                ",文件夹,文件 " & Str1
  15.           Str = "Select 文件夹, " & _
  16.                 "'\' + Format(日期'yyyy年mm月')+ '\' +  Format(日期,'yyyy年mm月dd日') + '\'+文件 " & Str1
  17.           Str = "Select 文件夹, " & _
  18.                 "\ + Format(日期'yyyy年mm月')+ \ +  Format(日期,'yyyy年mm月dd日') + \+文件 " & Str1
  19.           Str = "Select 文件夹, " & _
  20.                 " Format(日期'yyyy年mm月') +  Format(日期,'yyyy年mm月dd日') + 文件 " & Str1
  21.           Str = "Select 文件夹, " & _
  22.                 " Format(日期'yyyy年mm月') +  Format(日期,'yyyy年mm月dd日') " & Str1
  23.           Str = "Select '\' +  Format(日期,'yyyy年mm月')+ '\' + Format(日期,'yyyy年mm月dd日')+ '\' + 文件" & _
  24.                ",文件夹 " & Str1
  25.           Set Rs = SqlRetuRs(Str)
  26.           Debug.Print Rs.RecordCount
  27.           With Rs
  28.               .MoveFirst
  29.               For ii = 0 To .RecordCount - 1
  30.                   Str = .Fields(1)
  31.                   Str1 = ThisWorkbook.Path & .Fields(0)
  32.                   With Fso
  33.                        If .FileExists(Str) = True And .FileExists(Str1) = False Then
  34.                             Debug.Print Str, Str1
  35.                             Debug.Print Format(Time - T, "h:mm:ss")
  36.                             .MoveFile Str, Str1
  37.                        End If
  38.                   End With
  39.                   .MoveNext
  40.               Next ii              ''
  41.           End With
  42.           Debug.Print Format(Time - T, "h:mm:ss")
  43. End Sub
复制代码



Select '\' +  Format(日期,'yyyy年mm月')+ '\' + Format(日期,'yyyy年mm月dd日')+ '\' + 文件,文件夹  From [TraverseFolderFile$A1:Z47617] Where 日期 <> Null


您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

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

GMT+8, 2024-6-16 11:54 , Processed in 0.037697 second(s), 9 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

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

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

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