SQL Server中的通配符
通配符_
"_"号表示任意单个字符,该符号只能匹配一个字符."_"可以放在查询条件的任意位置,且只能代表一个字符.一个汉字只使用一个"_"表示.
例子:
if PATINDEX('%[吖-做]%','需要判断的字符')>0 -- 判断是否有字符
print '有汉字'
else
print '无汉字'
通配符%
"%"符号是字符匹配符,能匹配0个或更多字符的任意长度的字符串.在SQL语句中可以在查询条件的任意位置放置一个%来代表一个任意长度的字符串.在查询条件时也可以放置两个%进行查询,但在查询条件中最好不要连续出现两个%
通配符[]
在模式查询中可以利用"[]"来实现查询一定范围的数据.[]用于指定一定范围内的任何单个字符,包括两端数据
通配符[^]
[^]用来查询不属于指定范围 ([a-f]) 或集合 ([abcdef]) 的任何单个字符。
如:select * from alluser
where username like 'M[^abc]%'
表示从表alluser中查询用户名以M开头,且第二个字符不是a,b,c信息.
ESCAPE子句的模式匹配
可搜索包含一个或多个特殊通配符的字符串。例如,customers 数据库中的 discounts 表可能存储含百分号 (%) 的折扣值。若要搜索作为字符而不是通配符的百分号,必须提供 ESCAPE 关键字和转义符。例如,一个样本数据库包含名为 comment 的列,该列含文本 30%。若要搜索在 comment 列中的任何位置包含字符串 30% 的任何行,请指定由 Where comment LIKE '%30!%%' ESCAPE '!' 组成的 Where 子句。如果不指定 ESCAPE 和转义符,SQL Server 将返回所有含字符串 30 的行。
下例说明如何在 pubs 数据库 titles 表的 notes 列中搜索字符串"50% off when 100 or more copies are purchased":
Select notes from titles
Where notes LIKE '50%% off when 100 or more copies are purchased'
ESCAPE '%'
escape的主要用途
1.使用 ESCAPE 关键字定义转义符。 在模式中,当转义符置于通配符之前时,该通配符就解释为普通字符。例如,要搜索在任意位置包含字符串 5% 的字符串,请使用: Where ColumnA LIKE '%5/%%' ESCAPE '/'
2.ESCAPE 'escape_character' 允许在字符串中搜索通配符而不是将其作为通配符使用。 escape_character 是放在通配符前表示此特殊用途的字符。
Select * from finances Where description LIKE 'gs_' ESCAPE 'S'
意思就是: 比如,我们要搜索一个字符串 "g_" ,如果直接 like "g_",那么 "_"的作用就是通配符,而不是字符,结果,我们会查到比如 "ga","gb","gc",
相关文档:
Subquery: (single-row subqueries and multi-rows subqueries).
select select_list
from table
where expr operator (select select_list from table);
single-row subqueries operator: =, >, >=, <, <=, <>
e.g.:
1. select department_id, min(salary) from employees group by department_id ......
PairWise subquery:
e.g.:
select * from wf_docsort where (ndocsortid,nmoduleinfoid) in (select ndocsortid, nmoduleinfoid from wf_docsort where instr(cname,'文')>0)
the above sql is the same function as:
select * from wf_docsort where ndocsortid = (select ndocsortid from wf_docsort where ......
--> Title : SQL Server系统视图
--> Author : wufeng4552
--> Date : 2009-10-28
目录视图
目录视图返回 SQL Server 数据库引擎使用的信息。建议您使用目录视图这一最常用的目录元数据界面,它可为您提供最有效的方法来获取、转换并显示此信息的自定义形式。所有用户可用目录元 ......
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DAL
{
......