我正在尝试通过知道列中的日期包含日期和时间来从表中检索记录。
假设我有一个名为t1
的表,其中只包含两列name
和date
。
存储在列日期中的数据,如8/3/2010 12:34:20 PM
。
我想通过此查询检索此记录(例如,我没有花时间):
Select * From t1 Where date="8/3/2010"
这个查询什么都没给我!
如何在没有时间的情况下只知道date
来检索date
?
答案 0 :(得分:86)
DATE
是Oracle中的保留关键字,因此我使用的是列名your_date
。
如果您在your_date
上有索引,我会使用
WHERE your_date >= TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND your_date < TO_DATE('2010-08-04', 'YYYY-MM-DD')
或BETWEEN
:
WHERE your_date BETWEEN TO_DATE('2010-08-03', 'YYYY-MM-DD')
AND TO_DATE('2010-08-03 23:59:59', 'YYYY-MM-DD HH24:MI:SS')
如果没有索引或记录没有太多
WHERE TRUNC(your_date) = TO_DATE('2010-08-03', 'YYYY-MM-DD')
应该足够了。不带参数的TRUNC
会从DATE
中删除小时,分钟和秒。
如果效果非常重要,请考虑在该列上添加Function Based Index
:
CREATE INDEX trunc_date_idx ON t1(TRUNC(your_date));
答案 1 :(得分:3)
就个人而言,我通常会选择:
select *
from t1
where date between trunc( :somedate ) -- 00:00:00
and trunc( :somedate ) + .99999 -- 23:59:59
答案 2 :(得分:3)
将日期列转换为正确的格式并进行比较:
SELECT * From my_table WHERE to_char(my_table.my_date_col,'MM/dd/yyyy') = '8/3/2010'
这部分
to_char(my_table.my_date_col,'MM/dd/yyyy')
将导致字符串'8/3/2010'
答案 3 :(得分:2)
您可以使用between函数获取2010-08-03 00:00:00:000 AND 2010-08-03 23:59:59:000
之间的所有记录答案 4 :(得分:0)
尝试以下方式。
Select * from t1 where date(col_name)="8/3/2010"
答案 5 :(得分:0)
structure(list(year = c(2015, 2014, 2012, 2003, 1991, 1987, 1977,
1975, 1968, 1965, 2016, 2015, 2013, 2012, 2011, 2010, 2009, 2008,
2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997,
1996, 1995, 1994, 1991, 1990, 1988, 1986, 1985, 1983, 1982, 1980,
1978, 1975, 1973, 1968, 2016, 2015, 2014, 2013, 2012, 2011, 2010,
2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999,
1998, 1997, 1995, 1994, 1993, 1992, 1991, 1990, 1987, 1986, 1985,
1984, 1983, 1981, 1973, 1972, 1971, 1970, 1969, 1968, 1967, 1964,
2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006,
2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1996, 1995, 1994,
1993, 1992, 1991, 1990, 1987, 1986, 1985, 1983, 1980, 1979, 1978,
1976, 1973, 1972, 1971, 1967, 1965, 1962, 2016, 2015, 2014, 2013,
2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2001, 2000,
1999, 1998, 1997, 1996, 1995, 1991, 1988, 1986, 1983, 1982, 1980,
1978, 1975, 1973, 1968, 2016, 2015, 2014, 2013, 2012, 2011, 2010,
2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2000, 1999, 1998,
1997, 1996, 1995, 1991, 1990, 1983, 1979, 1976, 2016, 2015, 2014,
2013, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2002,
2001, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990,
1988, 1986, 1983, 1982, 1980, 1978, 1975, 1973, 1968, 2016, 2015,
2014, 2013, 2012, 2011, 2010, 2009, 2008, 2006, 2005, 2004, 2003,
2001, 2000, 1999, 1998, 1996, 1995, 1994, 1988, 1986, 1985, 1984,
1983, 1975, 1973, 1968, 2016, 2015, 2014, 2012, 2011, 2010, 2009,
2008, 2006, 2005, 2004, 2002, 2001, 2000, 1998, 1997, 1996, 1995,
1994, 1991, 1988, 1986, 1983, 1982, 1978, 1977, 1975, 1973, 1968,
2016, 2015, 2014, 2012, 2009, 2004, 2003, 2002, 2001, 1999, 1998,
1997, 1995, 1993, 1992, 1987, 1985, 1979, 1965, 1964, 2015, 2014,
2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002,
2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1992, 1991, 1990,
1988, 1986, 1985, 1984, 1983, 1982, 1981, 1980, 1979, 1978, 1976,
1975, 1974, 1973, 1968, 1967, 1963, 1961, 2016, 2015, 2012, 2009,
2004, 2002, 2001, 1999, 1998, 1993, 1992, 1990, 1987, 1985, 1979,
1965, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008, 2006, 2005,
2004, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1991, 1988,
1986, 1985, 1984, 1983, 1976, 1975, 1973, 1968, 1961, 2016, 2015,
2014, 2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003,
2002, 2001, 2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992,
1991, 1990, 1989, 1988, 1986, 1983, 1982, 1980, 1979, 1977, 1976,
1975, 1973, 1968, 1961, 2016, 2015, 2014, 2012, 2011, 2010, 2009,
2008, 2006, 2005, 2004, 2001, 2000, 1998, 1996, 1995, 1994, 1988,
1986, 1983, 1978, 1975, 1973, 1968, 2016, 2015, 2014, 2013, 2012,
2011, 2010, 2009, 2008, 2007, 2006, 2005, 2004, 2003, 2002, 2001,
2000, 1999, 1998, 1997, 1996, 1995, 1994, 1993, 1992, 1991, 1990,
1989, 1988, 1987, 1986, 1985, 1983, 1981, 1980, 1979, 1978, 1973,
1972, 1970, 1969, 1968, 1967, 2016, 2015, 2014, 2013, 2012, 2011,
2010, 2009, 2008, 2007, 2006, 2005, 2004, 2002, 2000, 1999, 1998,
1997, 1996, 1995, 1994, 1991, 1989, 1988, 1986, 1983, 1982, 1978,
1976, 1975, 1973, 1969, 1968, 1967, 2016, 2015, 2014, 2012, 2011,
2010, 2009, 2008, 2006, 2005, 2002, 2001, 2000, 1999, 1998, 1997,
1996, 1995, 1994, 1992, 1991, 1990, 1988, 1986, 1984, 1983, 1975,
1973, 1968, 2016, 2015, 2014, 2013, 2012, 2011, 2010, 2009, 2008,
2007, 2006, 2005, 2004, 2003, 2002, 2001, 2000, 1999, 1998, 1997,
1996, 1995, 1994, 1993, 1992, 1991, 1990, 1989, 1988, 1987, 1986,
1985, 1984, 1983, 1982, 1981, 1980, 1978, 1977, 1976, 1975, 1973,
1972, 1970, 1969, 1968, 1967, 1966, 2016), theme = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L), .Label = c("ALL", "AB", "BB", "BS",
"CS", "DS", "EG", "FB", "GB", "KB", "KS", "LS", "LP", "MC", "NB",
"NW", "SB", "SH", "VM", "WH"), class = "factor"), count = c(1,
1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 6, 7, 2, 3, 7, 1, 5, 7, 2,
2, 2, 2, 5, 2, 3, 3, 2, 2, 1, 4, 1, 2, 3, 1, 1, 2, 1, 1, 1, 1,
1, 3, 12, 4, 5, 11, 4, 8, 8, 9, 4, 5, 9, 6, 10, 6, 8, 7, 7, 6,
2, 3, 2, 2, 2, 1, 3, 2, 1, 3, 1, 3, 1, 1, 2, 2, 3, 1, 1, 3, 2,
6, 9, 6, 4, 6, 3, 1, 4, 6, 2, 5, 5, 4, 2, 2, 5, 7, 4, 4, 3, 1,
3, 4, 1, 1, 1, 3, 1, 2, 2, 4, 2, 2, 2, 1, 1, 2, 1, 1, 1, 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, 3, 5, 3, 3, 4, 1, 2, 1, 2, 1, 3, 1, 1, 2,
1, 1, 1, 2, 4, 6, 7, 2, 5, 6, 4, 9, 3, 10, 1, 4, 4, 2, 1, 2,
1, 1, 1, 2, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 2, 1, 1, 2, 1, 2, 4,
1, 2, 1, 1, 2, 3, 2, 2, 1, 2, 1, 2, 3, 1, 2, 1, 2, 2, 1, 1, 2,
3, 2, 1, 1, 1, 1, 1, 11, 5, 12, 6, 9, 15, 9, 13, 7, 10, 7, 8,
2, 8, 2, 5, 5, 7, 8, 6, 6, 5, 2, 4, 7, 8, 1, 6, 2, 6, 6, 2, 2,
5, 5, 1, 1, 3, 2, 3, 2, 1, 1, 1, 2, 1, 1, 11)), .Names = c("year",
"theme", "count"), row.names = 58:654, class = "data.frame")
将为您提供日期,而不是Oracle中的时间。
答案 6 :(得分:0)
只需使用以下一项:
select * from t1 where to_date(date_column)='8/3/2010'