日期:2014-05-16 浏览次数:20438 次
----------------------------------------------------------------
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-05-09 10:58:58
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[huang]
if object_id('[huang]') is not null drop table [huang]
go
create table [huang]([col] nvarchar(36))
insert [huang]
select 'AM0434E-PSP-14KR' union all
select 'AM0437E-A-PSP-14KR'
--------------生成数据--------------------------
select col,REVERSE(SUBSTRING(REVERSE(col),PATINDEX('%-%',REVERSE(col) )+1,LEN(col)))New_col
from [huang]
----------------结果----------------------------
/*
col New_col
------------------------------------ ------------------------------------
AM0434E-PSP-14KR AM0434E-PSP
AM0437E-A-PSP-14KR AM0437E-A-PSP
*/
with tb(a)as(
select 'AM0434E-PSP-14KR' union
select 'AM0437E-A-PSP-14KR'
)
select left(a,len(a)-charindex('-',REVERSE(a))) from tb
declare @a varchar(100)
set @a='AM0434E-PSP-14KR'
select right(@a,charindex('-',REVERSE(@a))-1)
declare @a varchar(100)
set @a='AM0434E-PSP-14KR'
select left(@a,charindex('-',@a)-1)