How can i use substring in SQL? [duplicate]

六眼飞鱼酱① 提交于 2019-12-12 01:35:15

问题


Possible Duplicate:
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# ?


回答1:


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.




回答2:


You could

SELECT DISTINCT SUBSTRING(....) FROM ...


来源:https://stackoverflow.com/questions/3634274/how-can-i-use-substring-in-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!