charindex的用法

时间:2021-08-11 01:55:59

declare @str nvarchar(50);
set @str='462,464,2';
select @str as '字符串'
select len(@str) as '字符长度'

select charindex(',',@str,1) as '第一个逗号的索引值'

select LEFT(@str,charindex(',',@str,1)-1) as '第一个值'

select SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)) as '从第一逗号开始截取出后面的字符串'

select LEFT(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),CHARINDEX(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)-1) as '中间的值'

select SUBSTRING(SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),charindex(',',SUBSTRING(@str,charindex(',',@str,1)+1,len(@str)),1)+1,len(@str)) as '最后面的值' --从第二个逗号开始截取出其后的字符串

-------------------------------------------------

ALTER PROCEDURE [dbo].[st_MES_InsertInspect]
( @ItemNo varchar(50), @ProdDate varchar(19),
@Bc varchar(10),@CreateMan varchar(50),
@Qty int,@Mo varchar(50))
WITH
EXECUTE AS CALLER
AS

insert into MES_Inspect
(BillNo,ItemNo,CheckType,ProductionTime,ProdNum,ProdBCCode,CreateMan,createtime,BatchNo)

select 'SL2-'+Convert(varchar(4),Getdate(),120)+'-612-'+datename(MM,GETDATE())+'-'+

(
--select right(100000+(convert(int,isnull(right(BillNO,3),0))+1),3)
--from mes_inspect
--where ID =(select MAX(id) from MES_Inspect where CheckType=5)

select case when len(SUBSTRING(BillNO,17,4))=3
then right(1000000+(convert(int,isnull(right(substring(BillNO,0,17)+'0'+ substring(BillNO,17,LEN(BillNO)),4),0))+1),4)

else right(1000000+(convert(int,isnull(right(BillNO,4),0))+1),4)
end
from mes_inspect
where ID =(select MAX(id) from MES_Inspect where CheckType=5)
),
@ItemNo,5,@ProdDate,@Qty,@Bc,@CreateMan,getdate(),@Mo