2008年10月14日 星期二

Like

---使用北風資料庫
Use Northwind

/*
Like '%_[][^]'
*/

--列出'A'開頭客戶
select *
from dbo.Customers
where CompanyName like 'A%'

--列出'S'結尾客戶
select *
from dbo.Customers
where CompanyName like '%s'

--列出客戶名稱有含'B'的客戶
select *
from dbo.Customers
where CompanyName like '%B%'

--列出電話倒數第二個字是'2'的客戶
select *
from dbo.Customers
where Phone like '%2_'

--列出電話第二個字是'2',倒數第二個字是'2'的客戶
select *
from dbo.Customers
where Phone like '_2%2_'

--列出'A' 或'B'或'C'開頭客戶
select *
from dbo.Customers
where CompanyName like 'A%' or CompanyName like 'B%' or CompanyName like 'c%'
-- or
select *
from dbo.Customers
where CompanyName like '[ABC]%'
-- or
select *
from dbo.Customers
where CompanyName like '[A-C]%'

--列出Not 'A' 且Not 'B'且Not 'C'開頭客戶
select *
from dbo.Customers
where CompanyName not like 'A%' and CompanyName not like 'B%' and CompanyName not like 'c%'
-- or
select *
from dbo.Customers
where CompanyName like '[^ABC]%'
-- or
select *
from dbo.Customers
where CompanyName like '[^A-C]%'

-------------------------test
update dbo.Products
set QuantityPerUnit='70%'
where ProductID=1

--Select *
from dbo.Products
where ProductID=1

--列出有%產品
Select *
from dbo.Products
where QuantityPerUnit like '%#%%' escape '#'

沒有留言: