#
現(xiàn)在很多網(wǎng)站上某些活動都有限制同一IP只能投一票的規(guī)定,但是有時候迫于壓迫,又不得不想辦法多投幾票,以前是采用Apache里的HttpClient來實(shí)現(xiàn)這些功能,日前正在看Ruby,就用它也來玩下:
require 'net/http'
##獲得網(wǎng)頁內(nèi)容
def query_url(url)
return Net::HTTP.get(URI.parse(url));
end
#抓取cnproxy上所有的代理列表,并將結(jié)果保存到proxy.txt中去
#你可以修改這塊代碼或者其他的代理服務(wù)器列表
def find_all_proxy
z="3";j="4";r="2";l="9";c="0";x="5";i="7";a="6";p="8";s="1"
pf = File.new("proxy.txt","w+")
for page_no in 1..10
url = "http://www.cnproxy.com/proxy#{page_no}.html"
content = query_url(url)
#print content
## ^$?./\[]{}()+*
for array in content.scan(/<td>(.*?)<SCRIPT type=text\/javascript>document.write\(":"\+(.*?)\)<\/SCRIPT><\/td>/)
if array.length == 2
pf.write("#{array[0]}:#{eval(array[1])}\n")
end
end
end
pf.close
end
##處理請求
def open_url_with_proxy(url)
pf = File.open("proxy.txt","r")
d = []
pf.each { |line| d << line }
for var in d
print "User Proxy #{var}\n"
begin
proxy = Net::HTTP::Proxy(var.split(":")[0],var.split(":")[1].to_i)
print proxy.get(URI.parse(url));
#print proxy.start("www.google.com",80){|http|
# response = http.get('/index.html')
# puts response.body
#}
rescue
##吃掉異常
end
end
end
##主程序
begin
if !FileTest.exist?( "proxy.txt" )
find_all_proxy
end
open_url_with_proxy('http://www.google.com/index.html');
end
這里需要注意的是代理服務(wù)器的端口不能是String類型,Ruby竟然不會自動轉(zhuǎn)換,搞得我浪費(fèi)了N多時間.
最近終于空下來了,所以下個Ruby玩玩,安裝Ruby很簡單,去
官網(wǎng)下載一個
一鍵安裝包既可,linux下的安裝,大家Google下就有很多教程了.對于IDE網(wǎng)上說NetBeans支持得很完美,但是因?yàn)楸救吮容^喜歡Eclipse,所以還是跟大家推薦
EasyEclipse for Ruby and Rails,當(dāng)然你可以選擇只下RoR的插件而不弄個全新的Eclipse.
以前一直在用Java寫爬蟲工具抓圖片,對HttpClient包裝,正則表達(dá)式處理那個是累啊,就算弄好了工具類,有時候一會又想不起來放哪兒,但Ruby對方面包裝的就很強(qiáng)大,短短幾十行代碼就搞定了這一切:
頁面獲取和文件下載的方法.
util.rb:
require 'net/http'
def query_url(url)
return Net::HTTP.get(URI.parse(url));
end
def save_url(url,dir,filename)
filename = url[url.rindex('/')+1, url.length-1] if filename == nil || filename.empty?
require 'open-uri'
Dir.mkdir("#{dir}") if dir != nil && !dir.empty? && !FileTest.exist?(dir)
open(url) do |fin|
if true
File.new("#{dir}#{filename}","wb").close
open("#{dir}#{filename}","wb") do |fout|
while buf = fin.read(1024) do
fout.write buf
STDOUT.flush
end
end
end
end
end
抓取圖片的具體應(yīng)用:
require "util"
begin
start_url = 'http://list.mall.taobao.com/1424/g-d-----40-0--1424.htm'
while start_url != nil && !start_url.empty? do
print "開始下載#{start_url}\n"
content = query_url(start_url)
next_page = content.scan(/ <a href="(.*?)" class="next-page"><span>下一頁<\/span><\/a>/)
next_url = nil
next_url = next_page[0][0] if next_page != nil && next_page.length > 0 && next_page[0].length > 0
imgs = content.scan(/<img src="(http:\/\/img[\d].*?)" \/>/)
for img in imgs
url = img[0];
save_url(url,"d:\\mall\\",nil)
end
start_url = next_url;
# break;
end
end
使用一天之后感覺ruby的語法很自然,很好理解,上手比較容易,而且相關(guān)包封裝的也很好,確實(shí)比較適合拿來玩玩小程序.
在開發(fā)中時常會遇到這樣的需求:讓某些描述信息(這些描述信息已經(jīng)進(jìn)行過安全html過濾,所以不會包含Javascript等腳本語言,但是允許正常的鏈接)里的鏈接失效,但是不要或者這些描述信息.如要以下代碼塊里的鏈接失效
<div id="desc">
<a href="http://www.9i56.cn">無聊網(wǎng)</a>
</div>
只需要再后面插入下段Javascript既可
<script type="text/javascript">
var elements = document.getElementById('desc').getElementsByTagName('A');
for (var i = 0, len = elements.length; i < len; ++i) {
elements[i].onclick = function(){return false;};
elements[i].href = "#";
}
var elementsArea = document.getElementById('desc').getElementsByTagName('area');
for (var i = 0, len = elementsArea.length; i < len; ++i) {
elementsArea[i].onclick = function(){return false;};
elementsArea[i].href = "#";
}
</script>
目前只知道a和area標(biāo)簽可以放href屬性來進(jìn)行跳轉(zhuǎn),不知道大家還知道有其它的方式可以用href跳轉(zhuǎn)嗎?
日常工作中,常常要將在公司做的東西拷回家,或者要從家里拷東西到公司,但是如果用U盤拷又太麻煩,上web發(fā)郵件又有點(diǎn)煩,所以就做了下面的小程序,發(fā)送前切版里的內(nèi)容到指定郵箱來傳遞文件.
相關(guān)技術(shù)點(diǎn):
1.JMail郵件發(fā)送
2.剪切板提取
具體代碼實(shí)現(xiàn)如下:
/*
* Created on 2008-3-5
*/
package org.dueam.ft;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import sun.misc.BASE64Encoder;
/**
* 剪切板內(nèi)容發(fā)生
* @author <a href="mailto:windonly@gmail.com">Anemone</a>
* hz,zj,china(2008-3-5)
*/
public class ClipboardFileTransmission {
/**
* @param args
* @throws EmailException
* @throws IOException
* @throws UnsupportedFlavorException
* @throws HeadlessException
*/
@SuppressWarnings("unchecked")
public static void main(String[] args) throws EmailException, HeadlessException, UnsupportedFlavorException,
IOException {
String context = null;
List<File> fileList = null;
/**
* 處理前切版
*/
for (DataFlavor df : Toolkit.getDefaultToolkit().getSystemClipboard().getAvailableDataFlavors()) {
//如果拷貝的是文本內(nèi)容
if (df.equals(DataFlavor.stringFlavor)) {
context = (String) Toolkit.getDefaultToolkit().getSystemClipboard().getData(DataFlavor.stringFlavor);
}
else if (df.equals(DataFlavor.javaFileListFlavor)) {
//如果拷貝的是文件則當(dāng)附件發(fā)送
fileList = (List<File>) Toolkit.getDefaultToolkit().getSystemClipboard().getData(
DataFlavor.javaFileListFlavor);
}
}
if ((null == context || "".equals(context)) && (fileList == null || fileList.isEmpty())) {
return;
}
if (null == context || "".equals(context)) {
context = "具體資料請看附件";
}
MultiPartEmail email = new MultiPartEmail();
// 發(fā)送服務(wù)器
email.setHostName("smtp.163.com");
//服務(wù)器用戶和密碼(如果你自己搞了臺不用驗(yàn)證的郵件服務(wù)器就不用了)
email.setAuthentication("XXX", "XXX");
//接收的郵箱
email.addTo("XXX@gmail.com", "我的資料庫");
//發(fā)送服務(wù)器的郵件地址,現(xiàn)在很多郵件提供商都有驗(yàn)證這個同用戶名是否對應(yīng),還是老老實(shí)實(shí)填真實(shí)的吧
email.setFrom("XXX@163.com", "Anemone");
email.setSubject("[日常資料傳遞]-" + getTime());
//文本編碼
email.setCharset("utf-8");
email.setMsg(context);
if (null != fileList)
for (File f : fileList) {
if (f.exists() && f.isFile()) {
//處理附件
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(f.getPath());
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(getTime() + "By Anemone");
BASE64Encoder enc = new BASE64Encoder();
//附件中文名問題
attachment.setName("=?GBK?B?" + enc.encode(f.getName().getBytes()) + "?=");
email.attach(attachment);
}
}
email.send();
}
public static String getTime() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
return df.format(new Date());
}
}
以上代碼在163和gmail之間測試通過過,建議用exe4j打成EXE文件,然后扔到system32目錄下面,這樣只要想發(fā)送資料的時候,只要復(fù)制下資料,再執(zhí)行下這個命令就一切都OK了.
相關(guān)類包:
下載
ArrayUtils 擁有以下方法:
- toString
- 將一個數(shù)組轉(zhuǎn)換成String,用于打印數(shù)組
- isEquals
- 判斷兩個數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
- toMap
- 將一個數(shù)組轉(zhuǎn)換成Map,如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
- clone
- 拷貝數(shù)組
- subarray
- 截取子數(shù)組
- isSameLength
- 判斷兩個數(shù)組長度是否相等
- getLength
- 獲得數(shù)組的長度
- isSameType
- 判段兩個數(shù)組的類型是否相同
- reverse
- 數(shù)組反轉(zhuǎn)
- indexOf
- 查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置
- lastIndexOf
- 反向查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置
- contains
- 查詢某個Object是否在數(shù)組中
- toObject
- 將基本數(shù)據(jù)類型轉(zhuǎn)換成外包型數(shù)據(jù)
- isEmpty
- 判斷數(shù)組是否為空(null和length=0的時候都為空)
- addAll
- 合并兩個數(shù)組
- add
- 添加一個數(shù)據(jù)到數(shù)組
- remove
- 刪除數(shù)組中某個位置上的數(shù)據(jù)
- removeElement
- 刪除數(shù)組中某個對象(從正序開始搜索,刪除第一個)
eg:
// 1.打印數(shù)組
ArrayUtils.toString(new int[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(new Integer[] { 1, 4, 2, 3 });// {1,4,2,3}
ArrayUtils.toString(null, "I'm nothing!");// I'm nothing!
// 2.判斷兩個數(shù)組是否相等,采用EqualsBuilder進(jìn)行判斷
// 只有當(dāng)兩個數(shù)組的數(shù)據(jù)類型,長度,數(shù)值順序都相同的時候,該方法才會返回True
// 2.1 兩個數(shù)組完全相同
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new int[] { 1, 2, 3 });// true
// 2.2 數(shù)據(jù)類型以及長度相同,但各個Index上的數(shù)據(jù)不是一一對應(yīng)
ArrayUtils.isEquals(new int[] { 1, 3, 2 }, new int[] { 1, 2, 3 });// true
// 2.3 數(shù)組的長度不一致
ArrayUtils.isEquals(new int[] { 1, 2, 3, 3 }, new int[] { 1, 2, 3 });// false
// 2.4 不同的數(shù)據(jù)類型
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, new long[] { 1, 2, 3 });// false
ArrayUtils.isEquals(new Object[] { 1, 2, 3 }, new Object[] { 1, (long) 2, 3 });// false
// 2.5 Null處理,如果輸入的兩個數(shù)組都為null時候則返回true
ArrayUtils.isEquals(new int[] { 1, 2, 3 }, null);// false
ArrayUtils.isEquals(null, null);// true
// 3.將一個數(shù)組轉(zhuǎn)換成Map
// 如果數(shù)組里是Entry則其Key與Value就是新Map的Key和Value,如果是Object[]則Object[0]為KeyObject[1]為Value
// 對于Object[]數(shù)組里的元素必須是instanceof Object[]或者Entry,即不支持基本數(shù)據(jù)類型數(shù)組
// 如:ArrayUtils.toMap(new Object[]{new int[]{1,2},new int[]{3,4}})會出異常
ArrayUtils.toMap(new Object[] { new Object[] { 1, 2 }, new Object[] { 3, 4 } });// {1=2,
// 3=4}
ArrayUtils.toMap(new Integer[][] { new Integer[] { 1, 2 }, new Integer[] { 3, 4 } });// {1=2,
// 3=4}
// 4.拷貝數(shù)組
ArrayUtils.clone(new int[] { 3, 2, 4 });// {3,2,4}
// 5.截取數(shù)組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 4);// {1,5}
// 起始index為2(即第三個數(shù)據(jù))結(jié)束index為4的數(shù)組
ArrayUtils.subarray(new int[] { 3, 4, 1, 5, 6 }, 2, 10);// {1,5,6}
// 如果endIndex大于數(shù)組的長度,則取beginIndex之后的所有數(shù)據(jù)
// 6.判斷兩個數(shù)組的長度是否相等
ArrayUtils.isSameLength(new Integer[] { 1, 3, 5 }, new Long[] { 2L, 8L, 10L });// true
// 7.獲得數(shù)組的長度
ArrayUtils.getLength(new long[] { 1, 23, 3 });// 3
// 8.判段兩個數(shù)組的類型是否相同
ArrayUtils.isSameType(new long[] { 1, 3 }, new long[] { 8, 5, 6 });// true
ArrayUtils.isSameType(new int[] { 1, 3 }, new long[] { 8, 5, 6 });// false
// 9.數(shù)組反轉(zhuǎn)
int[] array = new int[] { 1, 2, 5 };
ArrayUtils.reverse(array);// {5,2,1}
// 10.查詢某個Object在數(shù)組中的位置,可以指定起始搜索位置,找不到返回-1
// 10.1 從正序開始搜索,搜到就返回當(dāng)前的index否則返回-1
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 6);// 2
ArrayUtils.indexOf(new int[] { 1, 3, 6 }, 2);// -1
// 10.2 從逆序開始搜索,搜到就返回當(dāng)前的index否則返回-1
ArrayUtils.lastIndexOf(new int[] { 1, 3, 6 }, 6);// 2
// 11.查詢某個Object是否在數(shù)組中
ArrayUtils.contains(new int[] { 3, 1, 2 }, 1);// true
// 對于Object數(shù)據(jù)是調(diào)用該Object.equals方法進(jìn)行判斷
ArrayUtils.contains(new Object[] { 3, 1, 2 }, 1L);// false
// 12.基本數(shù)據(jù)類型數(shù)組與外包型數(shù)據(jù)類型數(shù)組互轉(zhuǎn)
ArrayUtils.toObject(new int[] { 1, 2 });// new Integer[]{Integer,Integer}
ArrayUtils.toPrimitive(new Integer[] { new Integer(1), new Integer(2) });// new int[]{1,2}
// 13.判斷數(shù)組是否為空(null和length=0的時候都為空)
ArrayUtils.isEmpty(new int[0]);// true
ArrayUtils.isEmpty(new Object[] { null });// false
// 14.合并兩個數(shù)組
ArrayUtils.addAll(new int[] { 1, 3, 5 }, new int[] { 2, 4 });// {1,3,5,2,4}
// 15.添加一個數(shù)據(jù)到數(shù)組
ArrayUtils.add(new int[] { 1, 3, 5 }, 4);// {1,3,5,4}
// 16.刪除數(shù)組中某個位置上的數(shù)據(jù)
ArrayUtils.remove(new int[] { 1, 3, 5 }, 1);// {1,5}
// 17.刪除數(shù)組中某個對象(從正序開始搜索,刪除第一個)
ArrayUtils.removeElement(new int[] { 1, 3, 5 }, 3);// {1,5}
來杭州這么久從來沒見過這么厲害的臺風(fēng),竟然可以把杭城改造成半個威尼斯.不知道為什么雨天總是給人壓抑的感覺,雖然自認(rèn)為很喜歡雨天,但是卻無法將撲滅心中沮喪的心情,很想一笑而過,可惜往往無功而返.有時候很想問問自己,你真的了解鏡子里那個人么?或者說你想去了解嗎?
現(xiàn)在網(wǎng)絡(luò)上發(fā)帖機(jī)橫行,如何在盡可能少地影響用戶體驗(yàn)的同時阻止發(fā)帖機(jī)是每個網(wǎng)站面臨的課題.
一 常見發(fā)帖機(jī)類型及其原理
1)程序自動構(gòu)建字段內(nèi)容,然后通過程序自動POST到服務(wù)器.
2)通過按鍵精靈之類的模擬器模擬鍵盤和鼠標(biāo)操作,以達(dá)到自動發(fā)帖的目的.
二 發(fā)帖機(jī)行為分析
發(fā)帖機(jī)主要是為了發(fā)布廣告信息,所以一般發(fā)帖機(jī)都是采用即時創(chuàng)建帳號==>然后發(fā)帖==>然后閃人流程散布信息.
三 解決方案
1)通過增加一個特殊字段,系統(tǒng)可以通過驗(yàn)證這個特殊字段來確認(rèn)當(dāng)前信息是否是系統(tǒng)實(shí)時產(chǎn)生.這種方法可以有效抑制通過程序自動POST數(shù)據(jù)的發(fā)帖機(jī),但是無法阻止模擬器類型的發(fā)帖機(jī).
2)驗(yàn)證碼.驗(yàn)證碼是最有效的阻止發(fā)帖機(jī)的手段,但驗(yàn)證碼也影響了用戶的發(fā)帖體驗(yàn),根據(jù)發(fā)帖機(jī)的行為分析我們可以采用如果當(dāng)前用戶注冊還未滿一個月或者發(fā)帖數(shù)還未達(dá)到10之前必需輸入驗(yàn)證碼,這樣既不影響老用戶的發(fā)帖體驗(yàn)又可以達(dá)到抑制發(fā)帖機(jī)的作用.
四 特殊字段產(chǎn)生策略
可以采用時間戳+時間戳加密(
加密方案參見我的另一篇文章),然后在服務(wù)器驗(yàn)證當(dāng)前客戶端傳上來的時間戳是否在允許的timeout之內(nèi).
四 索引與分頁--怎么樣SQL運(yùn)行的更快
- 正確的使用索引
Where條件落在索引上
不要在where的=前使用函數(shù),否則無法使用索引
Is Null可能無法使用索引
不正確的隱式轉(zhuǎn)換可能不能使用索引
如果能在索引獲得數(shù)據(jù),就不要回表
如果是復(fù)合索引,注意第2個字段以后,可能使用不到索引
- 正確的使用hint
如果有別名,一定要有別名
格式如/*+ index(t index_name) */
- 無需回表查詢的分頁寫法
存在以下表T1(A,B,C,D) T1上有索引字段(B,C) .如果只是查B,C兩個字段則:
select *
from (select tt.b, tt.c, rownum as rn
from (select t.b, t.c from t1 t where c = 2 order by t.c) tt
where rownum < 3)
where rn > 1
- 需回表查詢的分頁寫法
select /*+ ordered use_nl(t,t1) */
*
from (select rid from (
select rownum rn,rid from (
select rowid rid from t1
where c=2
order by c desc)
where rownum <= 50)
where rn >=1) t,
t1
where t.rid=t1.rowid;
三 null的那些事
- 在order 中,簡單把null認(rèn)為是最大
- 與null的運(yùn)算,返回null
SQL> select 1 + null from dual;
1+NULL
----------
- 與null的字符串合并,忽略null
SQL> select 'Hi'||null from dual;
'HI'||NULL
----------
Hi
- Null的查詢?yōu)閕s null
- Count(field),不包括null
- 如果索引條目全為null,則索引不記錄null
- In/not in與null
- Exists/not exists與null
SQL> select * from t1;
A B
---------- ----------
1 1
2
3
SQL> select * from t2;
A B
---------- ----------
1 1
2
SQL> select * from t1 where b in (select B from t2);
A B
---------- ----------
1 1
SQL> select * from t1 where b not in (select B from t2);
A B
---------- ----------
SQL> select * from t1 where exists (select * from t2 where t2.b = t1.b);
A B
---------- ----------
1 1
SQL> select * from t1 where not exists (select * from t2 where t2.b = t1.b);
A B
---------- ----------
3
2
exists主要用于片面的,有滿足一個條件的即可, 所以速度快很多. in 主要用于具體的集合操作, 有多少滿足條件.
近日公司的Oracle牛人給我們開發(fā)人員做了一次有關(guān)Oracle的培訓(xùn),感覺收獲頗大,故記錄下來,好他日溫習(xí)之用.
一 常用的SQL語句
-
select name,count(*) from table where .. group by ... 中能查詢的字段只能為group by的字段.
- select * from table where rownum < 5 order by id 中查詢出來的結(jié)果不是按數(shù)據(jù)中的ID排序的,而只是將select * from table where rownum < 5 的結(jié)果集按ID排序,所以如果你要按ID排序,你需要用子查詢實(shí)現(xiàn):
select * from ( select * from table order by id ) where rownum < 5
-
select * from table where name like 'A\_%' escape '\';將'\'后面的字符不當(dāng)關(guān)鍵字來處理,這個字符可以自定義.
-
insert into test(id,name) values(9,'It''s life'); or ||chr(39)|| 如果你想插入'可以使用''或者||chr(39)||方式插入.
-
如果你想將T1中B更新為T2中的B值,千萬要注意限定T1的范圍,否則T1的全部列將會更新,如update t1 t set t.B = (select tt.B from t2 tt where tt.A = t.A)將會t1中所有列都更新,如果t2中不存在對應(yīng)值,則t1中的值則為NULL,所以應(yīng)該將以上語句改造成update t1 t set t.B = (select tt.B from t2 tt where tt.A = t.A) where t.A in (select A from t2)
-
number(5,2):如果用 insert into test values(123.235)進(jìn)行插入時,將會使用四舍五入的方式插入即值為123.24;如果是insert into test values(12345)則無法插入數(shù)據(jù)
二 Oracle 函數(shù)
- 一般函數(shù)是數(shù)據(jù)庫設(shè)定的字符集來計(jì)算,現(xiàn)在一般的oracle都是16位,所以一個漢字長度為1,而函數(shù)后面加b則按字節(jié)來計(jì)算如:length('中國')=2 lenghtb('中國')=4 .
- Substr與substrb 字符串截取函數(shù),負(fù)數(shù)代表從右開始截取
SQL> select substr('我是中國人',2) from dual;
SUBSTR('我是中國人',2)
----------------------
是中國人
SQL> select substrb('我是中國人',2) from dual;
SUBSTRB('我是中國人',2)
-----------------------
是中國人
SQL> select substr('我是中國人',-2) from dual;
SUBSTR('我是中國人',-2)
-----------------------
國人
SQL> select substrb('我是中國人',-2) from dual;
SUBSTRB('我是中國人',-2)
------------------------
人
Length與lengthb 長度計(jì)算函數(shù)
SQL> select length('我是中國人') from dual;
LENGTH('我是中國人')
--------------------
5
SQL> select lengthb('我是中國人') from dual;
LENGTHB('我是中國人')
---------------------
10
Instr與Instrb 字符串查找函數(shù) instr(原字符串,查的字符串,起始位置,第幾個匹配) 返回字符串位置,找不到返回0 .
SQL> select Instr('abcabcdabcdef','a',1,3) from dual;
INSTR('ABCABCDABCDEF','A',1,3)
------------------------------
8
Upper與lower 大小寫轉(zhuǎn)換函數(shù)
SQL> select upper('AaBbCc') from dual;
UPPER('AABBCC')
---------------
AABBCC
SQL> select lower('AaBbCc') from dual;
LOWER('AABBCC')
---------------
aabbcc
Trim/Rtrim/Ltrim 字符串trim函數(shù)
SQL> select trim(' A B ') from dual;
TRIM('AB')
----------
A B
SQL> select rtrim('xABx','x') from dual;
RTRIM('XABX','X')
-----------------
xAB
SQL> select ltrim('xABx','x') from dual;
LTRIM('XABX','X')
-----------------
ABx
Trunc 截取函數(shù)(不進(jìn)行四舍五入)
SQL> select trunc(1234.123456,'-2') from dual;
TRUNC(1234.123456,'-2')
-----------------------
1200
SQL> select trunc(1234.123456,'2') from dual;
TRUNC(1234.123456,'2')
----------------------
1234.12
SQL> select trunc(1234.123456,'4') from dual;
TRUNC(1234.123456,'4')
----------------------
1234.1234
SQL> select trunc(1234.123456,'5') from dual;
TRUNC(1234.123456,'5')
----------------------
1234.12345
SQL> select trunc(sysdate,'yy') from dual;
TRUNC(SYSDATE,'YY')
-------------------
2007-01-01
SQL> select trunc(sysdate,'mi') from dual;
TRUNC(SYSDATE,'MI')
-------------------
2007-10-01 11:55:00
SQL> select trunc(sysdate,'dd') from dual;
TRUNC(SYSDATE,'DD')
-------------------
2007-10-01
SQL> select trunc(sysdate,'day') from dual;
TRUNC(SYSDATE,'DAY')
--------------------
2007-09-30
Next_day與last_day
SQL> select sysdate from dual;
SYSDATE
-----------
2007-10-01
SQL> select next_day(sysdate,'星期一') from dual;
NEXT_DAY(SYSDATE,'星期一')
--------------------------
2007-10-08 11:57:29
SQL> select next_day(sysdate,1) from dual;
NEXT_DAY(SYSDATE,1)
-------------------
2007-10-07 11:57:42
SQL> select next_day(sysdate,2) from dual;
NEXT_DAY(SYSDATE,2)
-------------------
2007-10-08 11:57:56
SQL> select last_day(sysdate) from dual;
LAST_DAY(SYSDATE)
-----------------
2007-10-31 12:00:
Round 四舍五入函數(shù)
SQL> select round(123.456,2) from dual;
ROUND(123.456,2)
----------------
123.46
SQL> select round(123.456,-2) from dual;
ROUND(123.456,-2)
-----------------
100
SQL> select round(123.456,-1) from dual;
ROUND(123.456,-1)
-----------------
120
Ceil與floor 取整函數(shù)
SQL> select ceil(1.1) from dual;
CEIL(1.1)
----------
2
SQL> select floor(9.9) from dual;
FLOOR(9.9)
----------
9
Decode與nvl Decode相當(dāng)于一個三元運(yùn)算函數(shù) nvl 如果值為空時默認(rèn)值.