How can i use substring in SQL? [duplicate]
Possible Duplicate:
开发者_C百科 How can I rearrange string with SQL?
Declare @CustTotalCount as int
Declare @CustMatchCount as int
select @CustTotalCount = count(*) from ENG_CUSTOMERTALLY
select @CustMatchCount = count(*) from Task where MPDReference in(
select ENG_CUSTOMERTALLY_CUSTOMERTASKNUMBER from dbo.ENG_CUSTOMERTALLY)
if(@CustTotalCount>@CustMatchCount)
select distinct
substring(ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO, charindex('-', ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO)
+ 1, 1000)
from dbo.ENG_CUSTOMERMYCROSS where
ENG_CUSTOMERMYCROSS_CUSTOMER_NUMBER in(
select ENG_CUSTOMERTALLY_CUSTOMERTASKNUMBER from ENG_CUSTOMERTALLY1
except
select MPDReference from Task )
i can convert below string data
- A320-200001-01-1(1)
- A320-200001-01-1(2)
- A320-200001-01-1(1)
TO
- 200001-01-1(1)
- 200001-01-1(2)
- 200001-01-1(1)
But i need
- 200001-01-1
- 200001-01-1
- 200001-01-1
distict ---> 200001-01-1 How can i do that SQL and C# ?
I gave this answer in the duplicate question too.
Here's a technique that uses PATINDEX
, which can use wild cards.
SUBSTRING(ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO,
PATINDEX('%[0-9]%', ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO),
PATINDEX('%(%', ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO)
- PATINDEX('%[0-9]%', ENG_CUSTOMERMYCROSS_MYTECHNIC_TASK_NO)
)
The start for your substring is the position of the first numeric value (%[0-9]%). The length value is the position of the first parenthesis ('%(%') less the starting position.
You could
SELECT DISTINCT SUBSTRING(....) FROM ...
精彩评论