SqlServer 基礎知識 數(shù)據(jù)檢索、查詢排序語句
更新時間:2011年10月25日 14:59:53 作者:
SqlServer 基礎知識 數(shù)據(jù)檢索、查詢排序語句,需要的朋友可以參考下。
復制代碼 代碼如下:
--執(zhí)行順序 From Where Select
select * from
(select sal as salary,comm as commission from emp ) x where salary<5000
--得出 Name Work as a Job
select ename +' Work as a'+job as msg from emp where deptno=10
--如果員工工資小于2000返回UnderPaid 大于等于4k 返回OverPaid 之間返回OK
select ename,sal,
case when sal<2000 then 'UnderPaid'
when sal>=4000 then 'OverPaid'
else
'OK'
end
from emp
--從表中隨機返回N條記錄 newid()
--order by 字句中指定數(shù)字常量時,是要求根據(jù)select列表中相應位置的列排序
--order by 字句中用函數(shù)時,則按函數(shù)在沒一行計算結果排序
select top 5 ename from emp order by newid()
--找空值is null
select * from emp where comm is null
--將空值轉換為實際值
--解釋:返回其參數(shù)中第一個非空表達式
--coalesce 聯(lián)合,合并,結合.英音:[,kəuə'les]美音:[,koə'lɛs]
select coalesce(comm, 1),empNo from emp
--按模式搜索
--返回匹配特定子串或模式的行
select ename,job
from emp
where deptno in(10,20)
--按子串排序 按照職位字段的 最后兩個字符排序
select ename, job from emp order by substring(job,len(job)-2,2)
--select top 2 len(job)-2 from emp
--select top 2 job from emp
--☆☆☆☆☆處理排序空值☆☆☆☆☆ [只能是大于0]
select ename ,sal,comm
from emp
order by 1 desc
-- 以降序或升序方式排序非空值,將空值放到最后,可以用case
select ename,sal,comm from
(
select ename ,sal,comm ,
case when comm is null then 0 else 1 end as A
from emp
) x
order by A desc ,comm desc


相關文章
sql server中的decimal或者numeric的精度問題
在sql server中定義列的數(shù)據(jù)類型decimal時需要制定其精度和小數(shù)位數(shù)。2009-05-05
使用sqlserver官方驅動包調(diào)用存儲過程遇到的坑及解決方法
這篇文章主要介紹了用sqlserver官方驅動包調(diào)用存儲過程遇到的一個坑,解決方法大概就是換驅動包,在在存儲過程首行添加 set nocount on ,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2022-10-10
SQL Server"錯誤 21002: [SQL-DMO]用戶 * 已經(jīng)存在問題解決
在分配數(shù)據(jù)庫登錄用戶的數(shù)據(jù)庫訪問權限的時候出現(xiàn)這種錯誤,如果選中某一個數(shù)據(jù)庫的任何一個角色就會出現(xiàn)這個錯誤2009-04-04

