① sql查询 取当前系统时间为 to_date(sysdate,'yyyymmdd') 如何去两年前的当天该如何查询
select add_months(sysdate, -24) from al, 如果只要显示年月日的话,则可以这样:
select trunc(add_months(sysdate, -24,'DD') from al
② sql语句里怎么将String转换成Date
将一种数据类型的表达式显式转换为另一种数据类型的表达式。CAST 和 CONVERT 提供相似的功能。 CAST ( expression AS 类型[长度]) 如:cast('2005-05-01' as datetime) CONVERT ( datatype [ ( length ) ] , expression [ , style ] ) 如:convert(datetime,'2005-05-01')
③ 关于sql语句的to_date的用法问题
oracle吗?
你在java中把日期转换成 yyyy-mm-dd hh:mm:ss的格式后再 处理:
比如:
updatet_contentsettitle='zxcv',publishtime=to_date('2010-10-0120:00:00','yyyy-mm-ddhh24:mi:ss')whereid=1
④ SQL根据条件查询某一个月与上一个月的对比
为了方便转换,先写个转换自定义函数:(将年,月转换成日期)
create function Todate(@year smallint,@month tinyint)
returns datetime
as
begin
declare @date datetime
select @date=cast(
cast(@year as varchar(4))+‘-'+cast(@month as varchar(2)+'-1'
as datetime)
return(@date)
end
查询代码:
select
a.name ,a. year,a. month a.money,
case when a.money-b.money >0 then a.money-b.money else -1*(a.money-b.money ) end
as 下月对比
case when a.money-b.money >0 then '减少' else '增加' end as 得到下月结果
case when a.money-c.money >0 then a.money-b.money else -1*(a.money-c.money ) end
as 上月对比
case when a.money-c.money >0 then '增加' else '减少' end as 得到上月结果
from 工资表 a left join
(select name,dateadd(mm,-1, dbo.Todate(year,month)) as 下月,money from 工资表)b
on a.name=b.name and a.year=year(b.下月) and a.month=b.month(b.下月)
left join
(select name,dateadd(mm,1, dbo.Todate(year,month)) as 上月,money from 工资表)c
on a.name=b.name and a.year=year(c.上月) and a.month=b.month(c.上月)
⑤ SQL语句中什么时候应该用betweento date,什么时候应该用to_date
select * from table where rq between to_date('2009-09-01','yyyy-mm-dd') and to_date('2009-09-08','yyyy-mm-dd')
以上,希望对你有所帮助!
⑥ sql关于to_date的区别
区别:
前者是 年月日格式:to_char(time,'yyyy-mm-dd')
后者是 年月日 时分秒 to_date('2012-05-11 23:59:59','yyyy-mm-dd hh24:mi:ss')
组成:
在正式学习SQL语言之前,首先让我们对SQL语言有一个基本认识,介绍一下SQL语言的组成:
1.一个SQL数据库是表(Table)的集合,它由一个或多个SQL模式定义。
2.一个SQL表由行集构成,一行是列的序列(集合),每列与行对应一个数据项。
3.一个表或者是一个基本表或者是一个视图。基本表是实际存储在数据库的表,而视图是由若干基本表或其他视图构成的表的定义。
4.一个基本表可以跨一个或多个存储文件,一个存储文件也可存放一个或多个基本表。每个存储文件与外部存储上一个物理文件对应。
5.用户可以用SQL语句对视图和基本表进行查询等操作。在用户角度来看,视图和基本表是一样的,没有区别,都是关系(表格)。
6.SQL用户可以是应用程序,也可以是终端用户。SQL语句可嵌入在宿主语言的程序中使用,宿主语言有FORTRAN,COBOL,PASCAL,PL/I,C和Ada语言等。SQL用户也能作为独立的用户接口,供交互环境下的终端用户使用。
以上内容参考网络-SQL数据库
⑦ sql基本语句不会写,希望会的朋友百忙之中帮我解答一下
你可以通过下面的函数进行转换
to_timestamp('2006-01-01 12:10:10.1','yyyy-mm-dd hh24:mi:ss.ff')
insert into 表 values(字段内容);
你有9个字段,那9个字段的内容必须都指定好
⑧ 关于Oracle中to_date的用法
一、在使用Oracle的to_date函数来做日期转换时,很多Java程序员也许会直接的采用“yyyy-MM-dd HH:mm:ss”的格式作为格式进行转换,但是在Oracle中会引起错误:“ORA 01810 格式代码出现两次”。
select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mm:ss') from al;
如:
原因是SQL中不区分大小写,MM和mm被认为是相同的格式代码,所以Oracle的SQL采用了mi代替分钟。
select to_date('2005-01-01 13:14:20','yyyy-MM-dd HH24:mi:ss') from al;
二、另要以24小时的形式显示出来要用HH24
select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from al;//mi是分钟
select to_char(sysdate,'yyyy-MM-dd HH24:mm:ss') from al;//mm会显示月份 oracle中的to_date参数含义
1.日期格式参数 含义说明
D 一周中的星期几
DAY 天的名字,使用空格填充到9个字符
DD 月中的第几天
DDD 年中的第几天
DY 天的简写名
IW ISO标准的年中的第几周
IYYY ISO标准的四位年份
YYYY 四位年份
YYY,YY,Y 年份的最后三位,两位,一位
HH 小时,按12小时计
HH24 小时,按24小时计
MI 分
SS 秒
MM 月
Mon 月份的简写
Month 月份的全名
W 该月的第几个星期
WW 年中的第几个星期 1.日期时间间隔操作
当前时间减去7分钟的时间
select sysdate,sysdate - interval ’7’ MINUTE from al
当前时间减去7小时的时间
select sysdate - interval ’7’ hour from al
当前时间减去7天的时间
select sysdate - interval ’7’ day from al
当前时间减去7月的时间
select sysdate,sysdate - interval ’7’ month from al
当前时间减去7年的时间
select sysdate,sysdate - interval ’7’ year from al
时间间隔乘以一个数字
select sysdate,sysdate - 8 *interval ’2’ hour from al
2.日期到字符操作
select sysdate,to_char(sysdate,’yyyy-mm-dd hh24:mi:ss’) from al select sysdate,to_char(sysdate,’yyyy-mm-dd hh:mi:ss’) from al select sysdate,to_char(sysdate,’yyyy-ddd hh:mi:ss’) from al select sysdate,to_char(sysdate,’yyyy-mm iw-d hh:mi:ss’) from al
参考oracle的相关关文档(ORACLE901DOC/SERVER.901/A90125/SQL_ELEMENTS4.HTM#48515)
3. 字符到日期操作
select to_date(’2003-10-17 21:15:37’,’yyyy-mm-dd hh24:mi:ss’) from al
具体用法和上面的to_char差不多。
4. trunk/ ROUND函数的使用
select trunc(sysdate ,’YEAR’) from al select trunc(sysdate ) from al select to_char(trunc(sysdate ,’YYYY’),’YYYY’) from al
5.oracle有毫秒级的数据类型
--返回当前时间 年月日小时分秒毫秒
select to_char(current_timestamp(5),’DD-MON-YYYY HH24:MI:SSxFF’) from al;
--返回当前 时间的秒毫秒,可以指定秒后面的精度(最大=9)
select to_char(current_timestamp(9),’MI:SSxFF’) from al;
6.计算程序运行的时间(ms)
declare
type rc is ref cursor;
l_rc rc;
l_mmy all_objects.object_name%type;
l_start number default dbms_utility.get_time;
begin
for I in 1 .. 1000
loop
open l_rc for
’select object_name from all_objects ’||
’where object_id = ’ || i;
fetch l_rc into l_mmy;
close l_rc;
end loop;
dbms_output.put_line(round( (dbms_utility.get_time-l_start)/100, 2 ) ||’ seconds...’ );
end;
⑨ 求SQL 函数TO DATE的用法
TO_DATE,就是转换数据类型,转换成DATE的数据类型,然后存在表中吧。
如果是VARCHAR类型的话,读取的时候只能按字符串读取。不能按日期数据读取。
⑩ sql查询语句相关问题
我这是以oracle为例的.. .. ..
有些可能繁琐,但可以保证结果准确
楼上的第一题有误,只用话费多少是不可能精准定位出哪个电话号码在那几个月中花费那么多
1[0].select distinct t1.phone from
( select * from jifei
where to_date(month,'yyyymm') = to_date('2011-06','yyyy-mm') and expenses > 50 and expenses < 100 ) t1,
( select * from jifei
where to_date(month,'yyyymm') = to_date('2011-07','yyyy-mm') and expenses > 50 and expenses < 100 ) t2,
(select * from jifei
where to_date(month,'yyyymm') = to_date('2011-08','yyyy-mm') and expenses > 50 and expenses < 100 ) t3,
(
select * from jifei
where to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm') and expenses = 0 ) t4,
(
select * from jifei
where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses = 0 ) t5
where t1.phone = t2.phone
and t2.phone = t3.phone
and t3.phone = t4.phone
and t4.phone = t5.phone
1[1].select phone from
( select phone , 1 ct from jifei
where (to_date(month,'yyyymm') = to_date('2011-06','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-07','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-08','yyyy-mm')
and expenses > 50 and expenses < 100 )
or ( to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm')
or to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses = 0 )
group by phone having sum(ct) = 5
2. select distinct phone from (
select phone ,
substr(phone,length(phone)-3,1) fst,
substr(phone,length(phone)-2,1) sec,
substr(phone,length(phone)-1,1) thr,
substr(phone,length(phone),1) fou
from jifei
where to_date(month,'yyyymm') > todate ('201101','yyyymm') - 1/24/60/60/60
and to_date(month,'yyyymm') < todate ('201111','yyyymm') + 1/24/60/60/60
)
where (fst = sec and sec = thr and thr = fou)
or (fst = sec and thr = fou )
or (fst = thr and sec = fou )
--oracle 可以直接使用rowid,并不是没有办法
3. delete from jifei where rowid in (
select max(rowid) from jifei where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm')
group by phone,month,expenses having count(*) = 2
)
--其它数据库估计可以这样,不过用一条sql写不完
create table temp as select * from jifei where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm')
group by phone,month,expenses having count(*) = 2
)
truncate table jifei;
insert into jifei select * from temp;
drop table temp;
4. select distinct t1.phone from
(select phone,month from jifei
where to_date(month,'yyyymm') = to_date('2011-09','yyyy-mm') and expenses > 30) t1,
(select phone,month from jifei
where to_date(month,'yyyymm') = to_date('2011-10','yyyy-mm') and expenses > 30) t2
where t1.phone = t2.phone