hbase(main):001:0> scan '-ROOT-' ROW COLUMN+CELL .META.,,1 column=info:regioninfo, timestamp=1340249081981, value={NAME => '.META.,, 1', STARTKEY => '', ENDKEY => '', ENCODED => 1028785192,} .META.,,1 column=info:server, timestamp=1341304672637, value=Hadoop46:60020 .META.,,1 column=info:serverstartcode, timestamp=1341304672637, value=1341301228326 .META.,,1 column=info:v, timestamp=1340249081981, value=\x00\x00 1 row(s) in 1.3230 seconds hbase(main):002:0> import java.util.Date => Java::JavaUtil::Date hbase(main):003:0> Date.new(1341304672637).toString() => "Tue Jul 03 16:37:52 CST 2012" hbase(main):004:0> Date.new(1341301228326).toString() => "Tue Jul 03 15:40:28 CST 2012"
在shell中,如果有可讀日期,能否轉(zhuǎn)成long類型呢?
hbase(main):005:0> import java.text.SimpleDateFormat => Java::JavaText::SimpleDateFormat hbase(main):006:0> import java.text.ParsePosition => Java::JavaText::ParsePosition hbase(main):015:0> SimpleDateFormat.new("yy/MM/dd").parse("12/07/03",ParsePosition.new(0)).getTime() => 1341244800000
參考
http://abloz.com/hbase/book.html
摘要: from:http://abloz.comauthor:ablozhoudate:2012.7.3在hbase的官方文檔里,講述了hbase的bin目錄下的ruby程序,可以采用如下的方式執(zhí)行:如果要使用腳本,可以看Hbase的bin 目錄.在里面找到后綴為 *.rb的腳本.要想運(yùn)行這個(gè)腳本,要這樣 $ ./bin/hbase org.jruby.Main PATH_TO_SCRIPT 如... 閱讀全文 轉(zhuǎn)自:http://www.cnblogs.com/linjiqin/archive/2013/03/08/2949339.htmlHBase 為用戶提供了一個(gè)非常方便的使用方式, 我們稱之為“HBase Shell”。
HBase Shell 提供了大多數(shù)的 HBase 命令, 通過 HBase Shell 用戶可以方便地創(chuàng)建、刪除及修改表, 還可以向表中添加數(shù)據(jù)、列出表中的相關(guān)信息等。
備注:寫錯(cuò) HBase Shell 命令時(shí)用鍵盤上的“Delete”進(jìn)行刪除,“Backspace”不起作用。
在啟動(dòng) HBase 之后,用戶可以通過下面的命令進(jìn)入 HBase Shell 之中,命令如下所示:
hadoop@ubuntu:~$ hbase shell HBase Shell; enter 'help<RETURN>' for list of supported commands. Type "exit<RETURN>" to leave the HBase Shell Version 0.94.3, r1408904, Wed Nov 14 19:55:11 UTC 2012 hbase(main):001:0>
具體的 HBase Shell 命令如下表 1.1-1 所示:
下面我們將以“一個(gè)學(xué)生成績(jī)表”的例子來詳細(xì)介紹常用的 HBase 命令及其使用方法。
這里 grad 對(duì)于表來說是一個(gè)列,course 對(duì)于表來說是一個(gè)列族,這個(gè)列族由三個(gè)列組成 china、math 和 english,當(dāng)然我們可以根據(jù)我們的需要在 course 中建立更多的列族,如computer,physics 等相應(yīng)的列添加入 course 列族。(備注:列族下面的列也是可以沒有名字的。)
1). create 命令
創(chuàng)建一個(gè)具有兩個(gè)列族“grad”和“course”的表“scores”。其中表名、行和列都要用單引號(hào)括起來,并以逗號(hào)隔開。
hbase(main):012:0> create 'scores', 'name', 'grad', 'course'
2). list 命令
查看當(dāng)前 HBase 中具有哪些表。
hbase(main):012:0> list
3). describe 命令
查看表“scores”的構(gòu)造。
hbase(main):012:0> describe 'scores'
4). put 命令
使用 put 命令向表中插入數(shù)據(jù),參數(shù)分別為表名、行名、列名和值,其中列名前需要列族最為前綴,時(shí)間戳由系統(tǒng)自動(dòng)生成。
格式: put 表名,行名,列名([列族:列名]),值
例子:
a. 加入一行數(shù)據(jù),行名稱為“xiapi”,列族“grad”的列名為”(空字符串)”,值位 1。
hbase(main):012:0> put 'scores', 'xiapi', 'grad:', '1'
hbase(main):012:0> put 'scores', 'xiapi', 'grad:', '2' --修改操作(update)
b. 給“xiapi”這一行的數(shù)據(jù)的列族“course”添加一列“<china,97>”。
hbase(main):012:0> put 'scores', 'xiapi', 'course:china', '97'
hbase(main):012:0> put 'scores', 'xiapi', 'course:math', '128'
hbase(main):012:0> put 'scores', 'xiapi', 'course:english', '85'
5). get 命令
a.查看表“scores”中的行“xiapi”的相關(guān)數(shù)據(jù)。
hbase(main):012:0> get 'scores', 'xiapi'
b.查看表“scores”中行“xiapi”列“course :math”的值。
hbase(main):012:0> get 'scores', 'xiapi', 'course :math'
或者
hbase(main):012:0> get 'scores', 'xiapi', {COLUMN=>'course:math'}
hbase(main):012:0> get 'scores', 'xiapi', {COLUMNS=>'course:math'}
備注:COLUMN 和 COLUMNS 是不同的,scan 操作中的 COLUMNS 指定的是表的列族, get操作中的 COLUMN 指定的是特定的列,COLUMNS 的值實(shí)質(zhì)上為“列族:列修飾符”。COLUMN 和 COLUMNS 必須為大寫。
6). scan 命令
a. 查看表“scores”中的所有數(shù)據(jù)。
hbase(main):012:0> scan 'scores'
注意:
scan 命令可以指定 startrow,stoprow 來 scan 多個(gè) row。
例如:
scan 'user_test',{COLUMNS =>'info:username',LIMIT =>10, STARTROW => 'test', STOPROW=>'test2'}
b.查看表“scores”中列族“course”的所有數(shù)據(jù)。
hbase(main):012:0> scan 'scores', {COLUMN => 'grad'}
hbase(main):012:0> scan 'scores', {COLUMN=>'course:math'}
hbase(main):012:0> scan 'scores', {COLUMNS => 'course'}
hbase(main):012:0> scan 'scores', {COLUMNS => 'course'}
7). count 命令
hbase(main):068:0> count 'scores'
8). exists 命令
hbase(main):071:0> exists 'scores'
9). incr 命令(賦值)
10). delete 命令
刪除表“scores”中行為“xiaoxue”, 列族“course”中的“math”。
hbase(main):012:0> delete 'scores', 'xiapi', 'course:math'
11). truncate 命令
hbase(main):012:0> truncate 'scores'
12). disbale、drop 命令
通過“disable”和“drop”命令刪除“scores”表。
hbase(main):012:0> disable 'scores' --enable 'scores'
hbase(main):012:0> drop 'scores'
13). status命令
hbase(main):072:0> status
14). version命令
hbase(main):073:0> version
另外,在 shell 中,常量不需要用引號(hào)引起來,但二進(jìn)制的值需要雙引號(hào)引起來,而其他值則用單引號(hào)引起來。HBase Shell 的常量可以通過在 shell 中輸入“Object.constants”。
最近在hadoop實(shí)際使用中有以下幾個(gè)小細(xì)節(jié)分享:
1 中文問題
從url中解析出中文,但hadoop中打印出來仍是亂碼?我們?cè)?jīng)以為hadoop是不支持中文的,后來經(jīng)過查看源代碼,發(fā)現(xiàn)hadoop僅僅是不支持以gbk格式輸出中文而己。
這是TextOutputFormat.class中的代碼,hadoop默認(rèn)的輸出都是繼承自FileOutputFormat來的,F(xiàn)ileOutputFormat的兩個(gè)子類一個(gè)是基于二進(jìn)制流的輸出,一個(gè)就是基于文本的輸出TextOutputFormat。
public class TextOutputFormat<K, V> extends FileOutputFormat<K, V> {
protected static class LineRecordWriter<K, V>
implements RecordWriter<K, V> {
private static final String utf8 = “UTF-8″;//這里被寫死成了utf-8
private static final byte[] newline;
static {
try {
newline = “/n”.getBytes(utf8);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + utf8 + ” encoding”);
}
}
…
public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {
this.out = out;
try {
this.keyValueSeparator = keyValueSeparator.getBytes(utf8);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + utf8 + ” encoding”);
}
}
…
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
Text to = (Text) o;
out.write(to.getBytes(), 0, to.getLength());//這里也需要修改
} else {
out.write(o.toString().getBytes(utf8));
}
}
…
}
可以看出hadoop默認(rèn)的輸出寫死為utf-8,因此如果decode中文正確,那么將Linux客戶端的character設(shè)為utf-8是可以看到中文的。因?yàn)閔adoop用utf-8的格式輸出了中文。
因?yàn)榇蠖鄶?shù)數(shù)據(jù)庫(kù)是用gbk來定義字段的,如果想讓hadoop用gbk格式輸出中文以兼容數(shù)據(jù)庫(kù)怎么辦?
我們可以定義一個(gè)新的類:
public class GbkOutputFormat<K, V> extends FileOutputFormat<K, V> {
protected static class LineRecordWriter<K, V>
implements RecordWriter<K, V> {
//寫成gbk即可
private static final String gbk = “gbk”;
private static final byte[] newline;
static {
try {
newline = “/n”.getBytes(gbk);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + gbk + ” encoding”);
}
}
…
public LineRecordWriter(DataOutputStream out, String keyValueSeparator) {
this.out = out;
try {
this.keyValueSeparator = keyValueSeparator.getBytes(gbk);
} catch (UnsupportedEncodingException uee) {
throw new IllegalArgumentException(”can’t find ” + gbk + ” encoding”);
}
}
…
private void writeObject(Object o) throws IOException {
if (o instanceof Text) {
// Text to = (Text) o;
// out.write(to.getBytes(), 0, to.getLength());
// } else {
out.write(o.toString().getBytes(gbk));
}
}
…
}
然后在mapreduce代碼中加入conf1.setOutputFormat(GbkOutputFormat.class)
即可以gbk格式輸出中文。
2 關(guān)于計(jì)算過程中的壓縮和效率的對(duì)比問題
之前曾經(jīng)介紹過對(duì)輸入文件采用壓縮可以提高部分計(jì)算效率。現(xiàn)在作更進(jìn)一步的說明。
為什么壓縮會(huì)提高計(jì)算速度?這是因?yàn)閙apreduce計(jì)算會(huì)將數(shù)據(jù)文件分散拷貝到所有datanode上,壓縮可以減少數(shù)據(jù)浪費(fèi)在帶寬上的時(shí)間,當(dāng)這些時(shí)間大于壓縮/解壓縮本身的時(shí)間時(shí),計(jì)算速度就會(huì)提高了。
hadoop的壓縮除了將輸入文件進(jìn)行壓縮外,hadoop本身還可以在計(jì)算過程中將map輸出以及將reduce輸出進(jìn)行壓縮。這種計(jì)算當(dāng)中的壓縮又有什么樣的效果呢?
測(cè)試環(huán)境:35臺(tái)節(jié)點(diǎn)的hadoop cluster,單機(jī)2 CPU,8 core,8G內(nèi)存,redhat 2.6.9, 其中namenode和second namenode各一臺(tái),namenode和second namenode不作datanode
輸入文件大小為2.5G不壓縮,records約為3600萬條。mapreduce程序分為兩個(gè)job:
job1:map將record按user字段作key拆分,reduce中作外連接。這樣最后reduce輸出為87億records,大小540G
job2:map讀入這87億條數(shù)據(jù)并輸出,reduce進(jìn)行簡(jiǎn)單統(tǒng)計(jì),最后的records為2.5億條,大小16G
計(jì)算耗時(shí)54min
僅對(duì)第二個(gè)階段的map作壓縮(第一個(gè)階段的map輸出并不大,沒有壓縮的必要),測(cè)試結(jié)果:計(jì)算耗時(shí)39min
可見時(shí)間上節(jié)約了15min,注意以下參數(shù)的不同。
不壓縮時(shí):
Local bytes read=1923047905109
Local bytes written=1685607947227
壓縮時(shí):
Local bytes read=770579526349
Local bytes written=245469534966
本地讀寫的的數(shù)量大大降低了
至于對(duì)reduce輸出的壓縮,很遺憾經(jīng)過測(cè)試基本沒有提高速度的效果。可能是因?yàn)榈谝粋€(gè)job的輸出大多數(shù)是在本地機(jī)上進(jìn)行map,不經(jīng)過網(wǎng)絡(luò)傳輸?shù)脑颉?nbsp;
附:對(duì)map輸出進(jìn)行壓縮,只需要添加 jobConf.setMapOutputCompressorClass(DefaultCodec.class)
3 關(guān)于reduce的數(shù)量設(shè)置問題
reduce數(shù)量究竟多少是適合的。目前測(cè)試認(rèn)為reduce數(shù)量約等于cluster中datanode的總cores的一半比較合適,比如 cluster中有32臺(tái)datanode,每臺(tái)8 core,那么reduce設(shè)置為128速度最快。因?yàn)槊颗_(tái)機(jī)器8 core,4個(gè)作map,4個(gè)作reduce計(jì)算,正好合適。
附小測(cè)試:對(duì)同一個(gè)程序
reduce num=32,reduce time = 6 min
reduce num=128, reduce time = 2 min
reduce num=320, reduce time = 5min
4某次正常運(yùn)行mapreduce實(shí)例時(shí),拋出錯(cuò)誤
java.io.IOException: All datanodes xxx.xxx.xxx.xxx:xxx are bad. Aborting…
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.processDatanodeError(DFSClient.java:2158)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.access$1400(DFSClient.java:1735)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream$DataStreamer.run(DFSClient.java:1889)
java.io.IOException: Could not get block locations. Aborting…
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.processDatanodeError(DFSClient.java:2143)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream.access$1400(DFSClient.java:1735)
at org.apache.hadoop.dfs.DFSClient$DFSOutputStream$DataStreamer.run(DFSClient.java:1889)
經(jīng)查明,問題原因是linux機(jī)器打開了過多的文件導(dǎo)致。用命令ulimit -n可以發(fā)現(xiàn)linux默認(rèn)的文件打開數(shù)目為1024,修改/ect/security/limit.conf,增加hadoop soft 65535
再重新運(yùn)行程序(最好所有的datanode都修改),問題解決
P.S:據(jù)說hadoop dfs不能管理總數(shù)超過100M個(gè)文件,有待查證
5 運(yùn)行一段時(shí)間后hadoop不能stop-all.sh的問題,顯示報(bào)錯(cuò)
no tasktracker to stop ,no datanode to stop
問題的原因是hadoop在stop的時(shí)候依據(jù)的是datanode上的mapred和dfs進(jìn)程號(hào)。而默認(rèn)的進(jìn)程號(hào)保存在/tmp下,linux 默認(rèn)會(huì)每隔一段時(shí)間(一般是一個(gè)月或者7天左右)去刪除這個(gè)目錄下的文件。因此刪掉hadoop-hadoop-jobtracker.pid和 hadoop-hadoop-namenode.pid兩個(gè)文件后,namenode自然就找不到datanode上的這兩個(gè)進(jìn)程了。
在配置文件中的export HADOOP_PID_DIR可以解決這個(gè)問題
這里還有個(gè)文章, 提及了幾個(gè)hadoop/mapred的優(yōu)化細(xì)節(jié)
http://thethethethethethe.spaces.live.com/blog/cns!A001241972EA08EA!228.entry
分類:
Sybase 函數(shù)
Sybase字符串函數(shù)
長(zhǎng)度和語法分析
datalength(char_expr)
在char_expr中返回字符的長(zhǎng)度值,忽略尾空
substring(expression,start,length)
返回部分字符串
right(char_expr,int_expr)
返回char_expr右邊的int_expr字符
基本字符串運(yùn)算
upper(char_expr)
把char_expr轉(zhuǎn)換成大寫形式
lower(char_expr)
把char_expr轉(zhuǎn)換成小寫形式
space(int_expr)
生成有int_expr個(gè)空格的字符串
replicate(char_expr,int_expr)
重復(fù)char_expr,int_expr次
stuff(expr1,start,length,expr2)
用expr2代替epxr1中start起始長(zhǎng)為length的字符串
reverse(char_expr)
反寫char_expr中的文本
ltrim(char_expr)
刪除頭空
rtrim(char_expr)
刪除尾空
格式轉(zhuǎn)換
ascii(char_expr)
返回char_expr中第一個(gè)字符的ASCII值
char(int_expr)
把ASCII碼轉(zhuǎn)換為字符
str(float_expr[,length[,decimal]])
進(jìn)行數(shù)值型到字符型轉(zhuǎn)換
soundex(char_expr)
返回char_expr的soundex值
difference(char_expr1,char_expr2)
返回表達(dá)式soundex值之差
串內(nèi)搜索
charindex(char_expr,expression)
返回指定char_expr的開始位置,否則為0
patindex("%pattern%",expression)
返回指定樣式的開始位置,否則為0
datalength用于確定可變字符串的長(zhǎng)度
soundex用于確定字符串是否發(fā)音相似
difference返回0-4之間的值,0表示最不相似,4表示最相似
通配符
% 匹配任何數(shù)量的字符或無字符
_ 匹配任何單個(gè)字符(空間占位符)
[] 規(guī)定有效范圍,或某個(gè)"OR"條件
[ABG] A,B,G
[A-C] A,B,C
[A-CE-G] A,B,C,E,F,G
[^ABG] 除了A,B,G
[^A-C] 除了A,B,C
escape子句
用某個(gè)轉(zhuǎn)義字符可在搜索字符串時(shí)將通配符作為文字來包含。
ANSI-89 SQL標(biāo)準(zhǔn)定義了escape子句指定某個(gè)轉(zhuǎn)義字符
缺省情況下,[]來轉(zhuǎn)義某個(gè)通配符,例:
select * from test_tab
where description like "%20[%]%"
語法:
like char_expression escape escape_character
例
select * from test_tab
where description like "%20#%%" escape "#"
+ 可用于串接字符
select au_laname+","+au_fname from authors
數(shù)學(xué)函數(shù)
abs(numeric_expr)
返回指定值的絕對(duì)值
ceiling(numeric_expr)
返回大于或等于指定值的最小整數(shù)
exp(float_expr)
給出指定值的指數(shù)值
floor(numeric_expr)
返回小于或等于指定值的最大整數(shù)
pi()
返回常數(shù)3.1415926
power(numeric_expr,power)
返回numeric_expr的值給power的冪
rand([int_expr])
返回0-1之間的隨機(jī)浮點(diǎn)數(shù),可指定基值
round(numeric_expr,int_expr)
把數(shù)值表達(dá)式圓整到int_expr指定的精度
sign(int_expr)
返回正+1,零0或負(fù)-1
sqrt(float_expr)
返回指定值的平方根
SQL SERVER支持所有標(biāo)準(zhǔn)的三角函數(shù)和其他有用的函數(shù)
日期函數(shù)
getdate()
返回當(dāng)前的系統(tǒng)日期和時(shí)間
datename(datepart,date_expr)
以字符串形式返回date_expr指定部分的值,轉(zhuǎn)換成合適的名字
datepart(datepart,date_expr)
作為整數(shù)返回date_expr值的指定部分
datediff(datepart,date_expr1,date_expr2)
返回date_expr2-date_expr1,通過指定的datepart度量
dateadd(datepart,number,date_expr)
返回日期,通過在date_expr上增加指定number的日期部件而產(chǎn)生的
datepart
日期部件 縮寫 值范圍
年 yy 1753-9999
季度 qq 1-4
月 mm 1-12
每年中的天 dy 1-366
天 dd 1-31
星期 wk 1-54
星期天 dw 1-7(1=sunday)
小時(shí) hh 0-23
分鐘 mi 0-59
秒 ss 0-59
毫秒 ms 0-999
例:
select invoice_no,
datediff(dd,date_shipped,getdate())
from invoices
where balance_due>0
轉(zhuǎn)換函數(shù)convert
此函數(shù)把值從一種類型改變成另一種類型
convert(datetype [(length)],expression)
select "Advance="+convert(char(12),advance)
from titles
日期轉(zhuǎn)換
convert(datetype[(length)],expression,format)
format指定將日期轉(zhuǎn)換為什么格式,有以下值:
沒有世紀(jì) 有世紀(jì) 轉(zhuǎn)換字符串中日期格式
0 or 100 mon dd yyy hh:miAM(or PM)
1 101 mm/dd/yy
2 102 yy.mm.dd
3 103 dd/mm/yy
4 104 dd.mm.yy
5 105 dd-mm-yy
6 106 dd mon yy
7 107 mon dd,yy
8 108 hh:mm:ss
9 or 109 mon dd,yyyy hh:mi:ss:mmmAM(or PM)
10 110 mm-dd-yy
11 111 yy/mm/dd
12 112 yymmdd
系統(tǒng)函數(shù)
函數(shù) 定義
訪問和安全性信息
host_id() 客戶進(jìn)程的當(dāng)前主機(jī)進(jìn)程ID號(hào)
host_name() 客戶進(jìn)程的當(dāng)前主計(jì)算機(jī)名
suser_id(["login_name"]) 用戶的SQL Server ID號(hào)
suser_name([server_user_id]) 用戶的SQL Server登錄名
user_id(["name_in_db"]) 用戶在數(shù)據(jù)庫(kù)中的ID號(hào)
user_name([user_id]) 用戶在數(shù)據(jù)庫(kù)中的名字
user 用戶在數(shù)據(jù)庫(kù)中的名字
show_role() 用戶的當(dāng)前活動(dòng)角色
數(shù)據(jù)庫(kù)和對(duì)象信息
db_id(["db_name"]) 數(shù)據(jù)庫(kù)ID號(hào)
db_name([db_id]) 數(shù)據(jù)庫(kù)名
object_id("objname") 數(shù)據(jù)庫(kù)對(duì)象ID號(hào)
object_name(obj_id]) 數(shù)據(jù)庫(kù)對(duì)象號(hào)
col_name(obj_id,col_id) 對(duì)象的欄名
col_length("objname","colname") 欄的長(zhǎng)度
index_col("objname",index_id,key#) 已索引的欄名
valid_name(char_expr) 若char_expr不是有效標(biāo)識(shí)符,則返回0
數(shù)據(jù)函數(shù)
datalength(expression) 按字節(jié)返回expression的長(zhǎng)度
tsequal(timestamp1,timestamp2) 比較時(shí)戳值,若時(shí)戳值不匹配,則返回出錯(cuò)消息
isnull()
isnull函數(shù)用指定的值代替查詢欄或合計(jì)中的空值
例:
select avg(isnull(total_order,$0))
from invoices
日期函數(shù)
getdate()
得到當(dāng)前時(shí)間,可以設(shè)置得到各種時(shí)間格式.
datepart(日期部分,日期)
取指定時(shí)間的某一個(gè)部分,年月天時(shí)分秒.
datediff(日期部分,日期1,日期2)
計(jì)算指定的日期1和日期2的時(shí)間差多少.
dateadd(日期部分,數(shù)值表達(dá)式,日期)
計(jì)算指定時(shí)間,再加上表達(dá)式指定的時(shí)間長(zhǎng)度.
--取時(shí)間的某一個(gè)部分
select datepart(yy,getdate()) --year
select datepart(mm,getdate()) --month
select datepart(dd,getdate()) --day
select datepart(hh,getdate()) --hour
select datepart(mi,getdate()) --min
select datepart(ss,getdate()) --sec
--取星期幾
set datefirst 1
select datepart(weekday,getdate()) --weekday
--字符串時(shí)間
select getdate() -- '03/11/12'
select convert(char,getdate(),101) -- '09/27/2003'
select convert(char,getdate(),102) -- '2003.11.12'
select convert(char,getdate(),103) -- '27/09/2003'
select convert(char,getdate(),104) -- '27.09.2003'
select convert(char,getdate(),105) -- '27-09-2003'
select convert(char,getdate(),106) -- '27 Sep 2003'
select convert(char,getdate(),107) --'Sep 27, 2003'
select convert(char,getdate(),108) --'11:16:06'
select convert(char,getdate(),109) --'Sep 27 2003 11:16:28:746AM'
select convert(char,getdate(),110) --'09-27-2003'
select convert(char,getdate(),111) --'2003/09/27'
select convert(char,getdate(),112) --'20030927'
select rtrim(convert(char,getdate(),102))+' '+(convert(char,getdate(),108)) -- '2003.11.12 11:03:41'
--整數(shù)時(shí)間
select convert(int,convert(char(10),getdate(),112)) -- 20031112
select datepart(hh,getdate())*10000 + datepart(mi,getdate())*100 + datepart(ss,getdate()) -- 110646
--時(shí)間格式 "YYYY.MM.DD HH:MI:SS" 轉(zhuǎn)換為 "YYYYMMDDHHMISS"
declare @a datetime,@tmp varchar(20),@tmp1 varchar(20)
select @a=convert(datetime,'2004.08.03 12:12:12')
select @tmp=convert(char(10),@a,112)
select @tmp
select @tmp1=convert(char(10),datepart(hh,@a)*10000 + datepart(mi,@a)*100 + datepart(ss,@a))
select @tmp1
select @tmp=@tmp+@tmp1
select @tmp
--當(dāng)月最后一天
declare
@tmpstr varchar(10)
@mm int,
@premm int,
@curmmlastday varchar(10)
begin
select @mm=datepart(month,getdate())--當(dāng)月
select @premm=datepart(month,dateadd(month,-1,getdate())) --上個(gè)月
if (@mm>=1 and @mm<=8)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else if (@mm>=9 and @mm<=11)
select @tmpstr=convert(char(4),datepart(year,getdate()))+'.'+convert(char(2),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
else
select @tmpstr=convert(char(4),datepart(year,dateadd(year,1,getdate())))+'.0'+convert(char(1),datepart(month,dateadd(month,1,getdate())))+'.'+'01'
select @curmmlastday=convert(char(10),dateadd(day,-1,@tmpstr),102) --當(dāng)月最后一天
end
源文檔 <http://hi.baidu.com/hwaspf/blog/item/a0ef87be66326e0d18d81f17.html>