SQL小練之查詢和更改

2021-09-17 08:52:49 字數 2399 閱讀 6572

1.找出姓名倒數第三位是a且不屬於20號部門的員工資訊

sql server:

select * from emp where ename like '%a__' and deptno != 20;

這裡用到了萬用字元進行模糊查詢,like 這個關鍵字就是用於字串模糊查詢的

%a_ _中,用下劃線佔位符,a _ _則標識字元a處於倒數第三個位置。

oracle:

select * from emp where instr(ename,'a',length(ename)-2,1)=length(ename)-2

在此處使用instr(a,b,i,j)函式,其中a是源字串,b是目標字串,i是起始位置,j是匹配序號(第i個匹配的)。它只檢索一次,返回值是要擷取的字串在源字串中的位置。

2.將emp的入職日期當中月份兩位數字統一替換成兩個

oracle:

select replace(to_char(hiredate,'yyyy-mm-dd'),substr(to_char(hiredate,'yyyy-mm-dd'),5,4),'-**-') from emp

使用replace(a,b,c)函式,a表示字串,b表示被替換的字串,c表示替換的字串,

也就是,這個函式把字串a裡面所包含的所有的b字串用c替換掉,然後返回替換後的新字串。

3.將會員資訊表的姓名欄位只保留姓氏,其餘內容全部替換成乙個*,返回修改後的結果

oracle:select replace(username,substr(username,2,length(username)-1),'*') from t_user_info

與上題類似

4將員工姓名中的a, b, c字母統一替換為z,返回修改後的姓名

sqlserver:

update emp set ename = replace(ename,'a','z'); update emp set ename = replace(ename,'b','z'); update emp set ename = replace(ename,'c','z');

oracle:

select translate(ename,'abc','zzz') from emp

translate(string,from_str,to_str),返回將(所有出現的)from_str中的每個字元替換為to_str中的相應字元以後的string。translate 是 replace 所提供的功能的乙個超集。如果 from_str 比 to_str 長,那麼在 from_str 中而不在 to_str 中的額外字元將從 string 中被刪除,因為它們沒有相應的替換字元。to_str 不能為空。oracle 將空字串解釋為 null,並且如果translate 中的任何引數為null,那麼結果也是 null。

5.emp 將工資精確到百位,返回修改後的工資

select round(sal,-2) from emp

round(m,n),n若為正數,則表示將m精確到小數點後n位。反之,精確到小數點前n位。

6.將工資擷取至千位,返回修改後的工資

oracle:

select trunc(sal,-3) from emp

只舍不入。

7. emp 查詢工資為整百的員工,返回員工姓名

select salary,username from emp where mod(salary,100)=0

*8.查詢員工年薪([工資+提成]12),返回員工姓名和年薪

oracle:

select ename,(sal+nvl(comm,0))*12, sal, comm from emp

nvl函式格式是nvl( string1, replace_with)。它的功能是如果string1為null,則nvl函式返回replace_with的值,否則返回string1的值,如果兩個引數都為null ,則返回null。

9. 查詢員工入職十年後的日期,擷取到當天零點。

oracle:

select trunc(add_months(hiredate,120), 'hh24') from emp; select trunc(sysdate, 'd') from dual;

trunc函式的另乙個用法,擷取時間。

SQL之更改累積和的值

首先建立乙個示例的檢視 sql create or replace view test sum id amt,trx 2as3select 1,100,pr from dual union all 4select 2,100,pr from dual union all 5select 3,50,p...

演算法小練 字首和

混了半學期開始好好補題 好好做人ing 題目來自洛谷,題解聽從裡面大佬的指點,所以 慢慢爬吧。a.地毯 乙個簡單的字首和 由於資料比較小可以直接暴力過,但是資料大的話怎麼辦呢 直接tle?include using namespace std int ar 1 2000 2000 intmain f...

WEEK1 SQL注入小練

第一周學習web,不知道學習筆記寫什麼寫點做題筆記吧。進來先判斷,這道題考的應該是sql注入,所以先試試用 or 繞過登入邏輯看看成不成功 其實我也只會這一點點 1 or1 1 其實這裡的 是可以不需要的但我還是打上了。發現登陸成功了,並且有回顯,所以我們要利用回顯的點去進行注入。首先是判斷unio...