sql字符串截取

--截取指定字符串后的字符串(例如截取http://后面的字符串)
--方法一:
Declare @S1 varchar(100)
Select @S1='http://www.163.com' 
Select Substring(@S1,CHARINDEX('www',@S1)+1,Len(@S1))
/*此处也可以这样写:*/
Select Substring(@S1,CHARINDEX('//',@S1)+2,Len(@S1))

--方法二
Select @S1='http://www.163.com'
Select Substring(@S1,PATINDEX('%www%',@S1)+1,Len(@S1))

-- 方法三:
Select @S1='http://www.163.com'
Select REPLACE(@S1,'http://','')

--方法四:
Select @S1='http://www.163.com'
Select STUFF(@S1,CHARINDEX('http://',@S1),Len('http://'),'')

--普通字符截取
select SUBSTRING(OperationAdminUserName,1,4) from db_owner.OperationAdminUser

--截取指定字符“-”之前的所有字符
select SUBSTRING(OperationAdminUserName,0,CHARINDEX('-',OperationAdminUserName)) 
from db_owner.OperationAdminUser

--截取指定字符“-”之后的所有字符
select SUBSTRING(OperationAdminUserName,CHARINDEX('-',OperationAdminUserName),LEN(OperationAdminUserName)) 
from db_owner.OperationAdminUser

select SUBSTRING(OperationAdminUserName,CHARINDEX('-',OperationAdminUserName)+1,LEN(OperationAdminUserName)) 
from db_owner.OperationAdminUser

--where条件字符截取
select * from db_owner.OperationAdminUser where SUBSTRING(OperationAdminUserName,1,4) = 'dich'

--left截取
select left('[email protected]',charindex('@','[email protected]')-1)
select left(OperationAdminUserName,CHARINDEX('-',OperationAdminUserName)) from db_owner.OperationAdminUser

--right截取
select right('[email protected]',charindex('@','[email protected]')-1)

select right(OperationAdminUserName,CHARINDEX('-',OperationAdminUserName)) from db_owner.OperationAdminUser

========================================================

========================================================

存取过程中调用某个存储过程查询某个字段

declare
@company varchar(10)
exec proc_getCompanyByUserid @loginUserID, @company out;
select urr.RoleID,urr.OperationAdminUserID,oa.ActualName 
from db_owner.UserRoleRelation urr
left join db_owner.OperationAdminUser oa on urr.OperationAdminUserID = oa.OperationAdminUserID
where RoleID = 3 and SUBSTRING(oa.OperationAdminUserName,1,4) = @company

proc_getCompanyByUserid存储过程

ALTER proc [db_accessadmin].[proc_getCompanyByUserid]
(
  @userid varchar(11),
  @company varchar(10) out
)

as 
select @company=Company from db_owner.OperationAdminUser where OperationAdminUserID=@userid


你可能感兴趣的:(sql字符串截取)