简体   繁体   English

在分隔日期字段(年、月、日)之间查找

[英]Find between with separated date fields (year,month,day)

I have the following dates in my table in separated fields.我在分隔字段中的表格中有以下日期。 How can I write a query to show the values between two dates.如何编写查询以显示两个日期之间的值。 For example: values between 2/1/2011 and 2/6/2011:例如:2/1/2011 和 2/6/2011 之间的值:

day         month            year          value 
--------------------------------------------------
2             6                2011         120
3             7                2011         130
5             5                2011         100
6             1                2011         50

I had the same scenario but with Month column displaying Month name.我有同样的情况,但月份列显示月份名称。 With slight modification on the given query i was able to fetch the data.通过对给定查询的轻微修改,我能够获取数据。

SELECT        *
FROM            Table_Name AS Tbl_Date
WHERE        (Year * 10000 + DATEPART(mm, CAST(Month + Year AS DATETIME)) * 100 + 1 
BETWEEN 20111101 AND 20121201)

Hope this will help希望这会有所帮助

As others have said, my first suggestion would be to use Date.正如其他人所说,我的第一个建议是使用日期。 Or if you need more detailed information than your example, Datetime or Timestamp with Time Zone.或者,如果您需要比您的示例更详细的信息,Datetime 或 Timestamp with Time Zone。

But in case you actually have to work with this data, I think something like this should work, depending on your flavor of SQL:但如果你真的必须使用这些数据,我认为这样的事情应该可以工作,这取决于你对 SQL 的风格:

SELECT value, CONVERT(DATE,CONCAT(CONCAT(CONCAT(CONCAT(day, "-"), month), "-"), year), 105) date FROM table_name where (2/1/2011) <= date and date <= (2/6/2011);

(with Oracle SQL, you can use to_date instead of convert and optionally use the || concatenation operator; with SQL server you have to use the + concatenation operator; with MySQL this should be right) (with Oracle SQL, you can use to_date instead of convert and optionally use the || concatenation operator; with SQL server you have to use the + concatenation operator; with MySQL this should be right)

(2/1/2011) and (2/6/2011) could either be strings that you convert similar to the other convert, or inputted using a PreparedStatement or something like it as dates directly (this would be preferable). (2/1/2011) 和 (2/6/2011) 可以是与其他转换类似的字符串,也可以是使用 PreparedStatement 或类似的东西直接作为日期输入的字符串(这会更好)。

To convert to Date for easier comparisons without worrying about dmy or mdy, in a standard fashion:要转换为 Date 以便于比较而不用担心 dmy 或 mdy,以标准方式:

 DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0)))

So, something like this.所以,像这样的东西。 The safest date format to use is yyyymmdd (especially with SQL Server)最安全的日期格式是yyyymmdd (尤其是 SQL 服务器)

SELECT
  value,
  DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) AS realdate
FROM Mytable_name
WHERE
  '20110201' <= DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) 
  and
  DATEADD(year, year-1900, DATEADD(month, month-1, DATEADD(day, day-1, 0))) <= '20110206'

if you are using oracle database then you can use TO_DATE and TO_CHAR functions to achive this target...如果您使用的是 oracle 数据库,那么您可以使用 TO_DATE 和 TO_CHAR 函数来实现此目标...

as follow-如下-

  select * from table_name 
  where to_date(day||month||year,'DDMMYYYY') 
  between &mindate and &maxdate

min date you can put 2-jan-2011 and max date as 2-jun-2011最小日期你可以把 2-jan-2011 和最大日期作为 2-jun-2011

I hope it should work for you:)我希望它对你有用:)

well i found the answer that i wanted thanks guys好吧,我找到了我想要的答案谢谢大家

SELECT Tbl_Date.Value,Tbl_Date.Year,Tbl_Date.Month,Tbl_Date.Day from Tbl_Date
where   ((Tbl_Date.Year*10000)+(Tbl_Date.Month*100)+Tbl_Date.Day)  between 110102 and 110602 

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

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