當前位置:首頁 » 編程語言 » sql語句todate
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql語句todate

發布時間: 2022-11-27 13:23:32

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