一、substring函式
此函式可以用在hql語句的columns部分和where後面,主要目的可以對資料庫欄位中的某資料進行按條件擷取,並動態組合成自已想要的資料格式,具體使用方法如:
example: oldsid:812108m00003 -> newsid:08-m00003
hql = "select substring(oldsid,5, 2)+'-'+substring(oldsid,7,6) as newsid from table";
substring()函式一般帶有三個引數,第乙個為要格式化的源字段,第二個為開始執行的起始位置,(
下標從台開始
),第三個引數為從開始位置起要擷取幾位資料,所有上面語句的結果為:
newsid:08-m00003
另substring函式也可以用在where語句後面如:
hql = "from table a where substring(a.description, 1, 3) = 'cat'";
二、子查詢的用法
假設現在有兩張表company(公司表),employee(員工表),關係乙個公司有多個員工,所以乙個company記錄應該對應多條employee紀錄.即在employee有乙個fk
指向company的pk.
condition:
找出每乙個公司中員工工資(pay)最高的那一位.
hql:
select (select max(e.pay) from c.employee e) from company c
note:select後面的子查詢必須寫在()中,
其執行後對應的sql語句為:
select
(select
max(e.pay)
from
employee e
where
e.hsuid=c.hsuid) as col_0_0_
from
company c;
hql中的子查詢也可以帶查詢條件如下面也可以寫成:
select (select max(e.pay) from a.employee e where e.name=:name) from company a
select
(select
max(e.pay)
from
employee e
where
e.hsuid=c.hsuid and e.name = '01') as col_0_0_
from
company c;
三、coalesce
函式的用法
coalesce
返回其引數中第乙個非空表示式。
語法coalesce (expression [ ,...n ])
引數expression
任何型別的表示式。
n 表示可以指定多個表示式的佔位符。所有表示式必須是相同型別,或者可以隱性轉換為相同的型別。
返回型別
將相同的值作為expression 返回。
注釋如果所有自變數均為null,則coalesce 返回null 值。
coalesce(expression1,...n) 與此case 函式等價:
case
when (expression1 is not null) then expression1
...
when (expressionn is not null) then expressionn
else null
示例:表score代表某同學本次考試各科成績的得分.結構如下:
語文數學英語
58 null 95
95 null 0
null null 34
null null null
0 95 null
0 0 0
null 95 95
95 null null
hql:
select a from score a where coalesce(a.chinese,a.math,a.english) = 95;
執行後結果為:
95 null 0
null 95 95
95 null null
(3 rows affected)
第一行因為a.chinese(語文)不空,所以where a.chinese = 95;return false;
第三行因為a.english = 34,所以return false;
第五行因為a.chinese = 0;標誌也不為空,所以return false;
四. isnull 函式和nullif 函式
sql server
裡的isnull 與asp 中的isnull不同,sql server 中有兩個引數,語法:
isnull(check_expression, replacement_value)
nullif
用於檢查兩個表示式,語法:
nullif(expression, expression)
·六.locate函式的用法
locate
函式返回第乙個string在第二個string中的起始位置,如果第乙個string或第二個string為空,則結果反回空(null),
如果第乙個字串中第二個中沒有找見,則返回零(0)
,
·語法介紹:
locate( string1, string2 [, start] )
string1:
任何字串表示式,此字串作為乙個字串物件去字串2中進行批配.
string2:
任何字串表示式,此字串包括第乙個字串.
start:
從字串string2中搜尋string1是的起始位置,假如沒有設定,或為null,或所賦值小於壹,則統一從1開始搜尋.
·examples:·
select locate( 'peter', 'peter steve tom' )
七.cast函式的用法
cast
函式[資料型別轉換] ·
功能:返回轉換為提供的資料型別的表示式的值。
語法:cast(expression as data type)
引數:expression 要轉換的表示式
data type 目標資料型別
用法:如果未指定字串型別的長度,資料庫伺服器將選擇適當的長度。如果沒有為十進位制轉換指定精度和小數字數,則資料庫伺服器將選擇適當的值。
·示例
下面的函式確保字串被用作日期:
select cast( '2000-10-31' as date )
計算表示式
1 + 2
的值,並將結果轉換為單字元字串。
select cast( 1 + 2 as char )
可以使用cast函式縮短字串:
select cast( 'surname' as char(5) )
HQL常用函式
criteria查詢對查詢條件進行了物件導向封裝,符合程式設計人員的思維方式,不過hql hibernate querylanguage 查詢提供了更加豐富的和靈活的查詢特性,因此hibernate將hql查詢方式立為官方推薦的標準查詢方式,hql查詢在涵蓋criteria查詢的所有功能的前提下,提...
MySql中常用轉換函式介紹
cast函式 convert函式 用法 cast expr as type convert expr,type convert expr using transcoding name select convert abc using utf8 將varchar 轉為int 用 cast str as...
HQL筆記 常用函式
substrign 要擷取的字元屬性字段,開始位置,擷取長度 字元擷取 trim 字串物件屬性列 去掉兩端的空格 lower 字串物件屬性列 轉換小寫 upper 字串物件屬性列 轉換大寫 length 欄位名 字元長度 current date 資料庫當前日期 current time 資料庫當前...