Oracle——按时间段查询

本文介绍了如何使用SQL查询在多个时间段内进行筛选,并详细解释了如何将日期转换为特定格式,包括从服务器获取时间、格式化日期以及比较日期的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

按照多个时间段查询:包括秒

String sql = "from Sedimentpermit";
			sql += " where Carno like '%" + carno + "%'";
			sql += " and road like '%" + road + "%'";
			sql += " and to_char(VALIDDATE,'yyyy-mm-dd') >= '" + validDate
					+ "'";
			sql += " and ((to_char(BEGINDATE1,'yyyy-mm-dd hh24:mi:ss') <= '"
					+ time + "'";
			sql += " and to_char(ENDDATE1,'yyyy-mm-dd hh24:mi:ss') >= '" + time
					+ "') ";
			sql += " or (to_char(BEGINDATE2,'yyyy-mm-dd hh24:mi:ss') <= '"
					+ time + "'";
			sql += " and to_char(ENDDATE2,'yyyy-mm-dd hh24:mi:ss') >= '" + time
					+ "') ";
			sql += " or (to_char(BEGINDATE3,'yyyy-mm-dd hh24:mi:ss') <= '"
					+ time + "'";
			sql += " and to_char(ENDDATE3,'yyyy-mm-dd hh24:mi:ss') >= '" + time
					+ "')) ";
 

 

常用的日期转换方法

//获得服务器时间
	public static Date getServerTime(Context con) {
		String seamurl = DBHelp.getConfigValue(con, ConfigKey.seamurl);
		if (seamurl.equals("")) {
			seamurl = StringHelp.getResSeamUrl(con);
		}
		seamurl += "?requestType=getTime";
		String bs = "";
		Date date = null;
		try {
			for (int i = 0; i <= 5; i++) {
				bs = HttpHelp.getHttpBack(seamurl);
				if (bs.contains("time")) {
					date = DateHelp.stringToDate2(StringHelp.getXMLAtt(bs,
							"time"));
					return date;
				}
				// Thread.sleep(300);
			}
		} catch (Exception e) {
			e.printStackTrace();
			LogHelp.Log2SDErr(e);
			bs = "<time>" + getCurrentTime() + "</time>";
		}

		LogHelp.LogI("服务器时间=" + bs);
		if (date == null) {
			bs = "<time>" + getCurrentTime() + "</time>";
			date = DateHelp.stringToDate2(StringHelp.getXMLAtt(bs, "time"));
		}
		return date;
	}

	public static String convertDate(Date date, String format) {
		if (date != null) {
			DateFormat format1 = new SimpleDateFormat(format);
			String s = format1.format(date);
			return s;
		}
		return "";
	}

	public static String getCurTime() {
		return convertDate(new Date(), "HH:mm");
	}

	public static String getCurrentTime() {
		return convertDate(new Date(), "yyyy-MM-dd HH:mm:ss");
	}
	

	public static String getCurDate() {
		return convertDate(new Date(), "yyyy-MM-dd");
	}

	public static String getCurDate(Date date) {
		return convertDate(date, "yyyy-MM-dd");
	}

	public static String getFormateDate(int type, Date date) {
		// 获取系统时间,并格式化
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int mounth = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int min = calendar.get(calendar.MINUTE);
		int secound = calendar.get(calendar.SECOND);

		// 格式一:2011-11-1 12-12-66+mima 作为图片文件名
		if (type == 1) {
			String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
			Random random = new Random();
			int i = random.nextInt(10);
			String extra = a[i];
			String mima = MiMa.jiami(extra + AddZero(min) + AddZero(secound)
					+ random.nextInt(10), 6);
			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
					+ "-" + AddZero(secound) + " " + mima;
		}
		// 格式二:2011年11月1日 12时12分45秒 写在照片上
		else if (type == 2) {
			return String.valueOf(year) + "年" + AddZero(mounth) + "月"
					+ AddZero(day) + "日  " + AddZero(hour) + "时" + AddZero(min)
					+ "分" + AddZero(secound) + "秒";
		}
		// 格式三:2011-11-1 12-12-11 作为录音文件名
		else if (type == 3) {
			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
					+ "-" + AddZero(secound) + " ";
		}
		return null;
	}

	/**
	
	 * @annotation 获取指定格式的日期
	 */
	@SuppressWarnings("static-access")
	public static String getFormateDate(int type) {
		// 获取系统时间,并格式化
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date());
		int year = calendar.get(Calendar.YEAR);
		int mounth = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DATE);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int min = calendar.get(calendar.MINUTE);
		int secound = calendar.get(calendar.SECOND);

		// 格式一:2011-11-1 12-12-66+mima 作为图片文件名
		if (type == 1) {
			String a[] = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j" };
			Random random = new Random();
			int i = random.nextInt(10);
			String extra = a[i];
			String mima = MiMa.jiami(extra + AddZero(min) + AddZero(secound)
					+ random.nextInt(10), 6);
			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
					+ "-" + AddZero(secound) + " " + mima;
		}
		// 格式二:2011年11月1日 12时12分45秒 写在照片上
		else if (type == 2) {
			return String.valueOf(year) + "年" + AddZero(mounth) + "月"
					+ AddZero(day) + "日  " + AddZero(hour) + "时" + AddZero(min)
					+ "分" + AddZero(secound) + "秒";
		}
		// 格式三:2011-11-1 12-12-11 作为录音文件名
		else if (type == 3) {
			return String.valueOf(year) + "-" + AddZero(mounth) + "-"
					+ AddZero(day) + " " + AddZero(hour) + "-" + AddZero(min)
					+ "-" + AddZero(secound) + " ";
		}
		return null;
	}

	public static String AddZero(int i) {
		if (i >= 0 && i <= 9) {
			return "0" + i;
		}
		return String.valueOf(i);

	}

	/**
	 * 

	 * @annotation string 转 日期类型
	 */
	public static Date stringToDate(String string) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date stringToDate2(String string) {
		// SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = sdf.parse(string);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 

	 * @annotation 日期比较:都是sring型:0相等,>0大于,<0 小于
	 */
	public static long compareDate(String date1, String date2) {
		Date beginTime = DateHelp.stringToDate(date1);
		Date endTime = DateHelp.stringToDate(date2);
		return beginTime.getTime() - endTime.getTime();
	}

	/**
	 * 

	 * @annotation 日期比较:都是Date型
	 */
	public static long compareDate(Date date1, Date date2) {
		return date1.getTime() - date2.getTime();
	}
 

 

### Oracle SQL 中的时间段判断 #### 判断单个时间是否在指定时间段内 为了验证某个特定时间 `addtime` 是否位于给定的时间范围内,可以使用如下SQL语句: ```sql WHERE addtime >= TO_DATE('2010-1-1', 'YYYY-MM-DD') AND addtime < TO_DATE('2019-1-1', 'YYYY-MM-DD') ``` 此方法利用了 `TO_DATE()` 函数来转换字符串形式的日期到实际的日期对象,并设置了一个闭开区间来进行比较[^2]。 #### 判断两个时间段是否存在重叠 对于两个时间段 `(S1, E1)` 和 `(S2, E2)` 的交叉情况,则可以通过逻辑运算符组合条件表达式完成。具体来说就是当第一个区间的结束晚于第二个区间的开始并且早于其结束;或者是反过来的情况也成立时即认为这两个时段有交集: ```sql SELECT * FROM table_name WHERE NOT ((E1 < S2) OR (E2 < S1)); ``` 上述查询排除了两种不相交的情形——一种是第一个时间段完全发生在另一个之前,另一种则是相反情形。因此剩下的记录就代表那些存在部分或全部重合的时间间隔[^1]。 #### 动态计算相对现在的时间范围 如果想要基于当前系统时间为基准点向前追溯一定时期的数据,例如过去十天内的记录,可采用以下方式编写查询: ```sql SELECT * FROM tablename WHERE datetime >= SYSDATE - INTERVAL '10' DAY; ``` 同样地,针对更短周期的需求如最近一个小时的数据检索也可以相应调整减去的时间长度: ```sql SELECT * FROM tablename WHERE datetime >= SYSDATE - INTERVAL '1' HOUR; ``` 这些例子展示了如何灵活运用 `SYSDATE` 结合算术操作符以及 `INTERVAL` 来定义动态变化的时间窗口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值