简体   繁体   English

在SQL存储过程中提取字符串的某些部分

[英]extracting certain part of string in SQL stored procedure

I have this stored procedure at the moment. 我现在有这个存储过程。 I would like to limit the String ChanceOfSuccess to only a the percentage like "40%". 我想将String ChanceOfSuccess限制为仅一个百分比,例如“ 40%”。 Right now the string reads something like this "40% - Thinking about it". 现在,字符串显示为“ 40%-思考”。 What would be the best way to do this? 最好的方法是什么?

CREATE PROCEDURE [dbo].[procActivity_SelectbyOutstandingActivitiesNew]
AS
SELECT  C.[Name] AS [Customer], 
        C.CustomerId AS [CustomerID],
        E.FirstName + ' ' + E.Surname AS [Employee],
        AT.[TypeName] AS [Activity Type],
        A.[ActivityDate] AS ActivityDate,
        A.NextActivityDate,
        A.[ChanceOfSuccess] AS ChanceOfSuccess,
        A.[Comments] AS Comments,
        U.Surname + ' ' + U.FirstName AS [User],
        E.EmployeeId,
        A.ActivityId,
        CONVERT(INT, SUBSTRING(A.ChanceOfSuccess, 0, CHARINDEX ('%',A.ChanceOfSuccess))) AS [Success Percentage],
        A.[IsComplete]

FROM    Customer C
INNER JOIN Activity A ON A.CustomerId = C.CustomerId
INNER JOIN Employee E ON E.EmployeeId = A.EmployeeId AND E.CustomerId = C.CustomerId
INNER JOIN [ActivityType] AT ON A.[ActivityTypeId] = AT.[ActivityTypeId]
INNER JOIN [User] U ON A.[UserId] = U.[UserId]
LEFT OUTER JOIN Activity A2 ON A.CustomerId = A2.CustomerId AND A.UserId = A2.UserId AND A2.ActivityDate > A.ActivityDate
WHERE   C.[IsDeleted] = 0
AND     A.NextActivityDate <= GETDATE()
AND     E.IsDeleted = 0
AND     A.IsDeleted = 0
AND (A2.ActivityId IS NULL OR A2.IsDeleted > 0)
AND CONVERT(INT, SUBSTRING(A.ChanceOfSuccess, 0, CHARINDEX ('%',A.ChanceOfSuccess))) < 100
ORDER BY A.NextActivityDate ASC, Customer, A.ActivityDate ASC, [Success Percentage] DESC
GO

Homework? 家庭作业? you clearly didn't write the original query because it's already doing that work. 您显然没有编写原始查询,因为它已经完成了这项工作。

The line: 该行:

CONVERT(INT, SUBSTRING(A.ChanceOfSuccess, 0, CHARINDEX ('%',A.ChanceOfSuccess))) AS [Success Percentage],

Already does what you want and then converts it to an int, so remove the conversion and you should have what you want. 已经执行了您想要的操作,然后将其转换为int,因此删除转换,您应该拥有所需的内容。

DECLARE @v VARCHAR(50) = '40% - Thinking about it'

SELECT @V, SUBSTRING(@V, 0, charindex('%', @V)+1)

Here are some surgestions to how to rewrite your code: 以下是有关如何重写代码的一些建议:

create view [v_OutstandingActivities]
AS
SELECT  C.[Name] AS [Customer], 
        C.CustomerId AS [CustomerID],
        E.FirstName + ' ' + E.Surname AS [Employee],
        AT.[TypeName] AS [ActivityType],
        A.[ActivityDate] AS ActivityDate,
        A.NextActivityDate,
        A.[ChanceOfSuccess] AS ChanceOfSuccess,
        A.[Comments] AS Comments,
        U.Surname + ' ' + U.FirstName AS [UserName],
        E.EmployeeId,
        A.ActivityId,
        replace(A.ChanceOfSuccess, '%', '')+0 AS [SuccessPercentage],
        A.[IsComplete]

FROM    Customer C
INNER JOIN Activity A ON A.CustomerId = C.CustomerId
INNER JOIN Employee E ON E.EmployeeId = A.EmployeeId AND E.CustomerId = C.CustomerId
INNER JOIN [ActivityType] AT ON A.[ActivityTypeId] = AT.[ActivityTypeId]
INNER JOIN [User] U ON A.[UserId] = U.[UserId]
LEFT OUTER JOIN Activity A2 ON A.CustomerId = A2.CustomerId AND A.UserId = A2.UserId 
AND A2.ActivityDate > A.ActivityDate AND (A2.IsDeleted > 0)
WHERE   C.[IsDeleted] = 0
AND     A.NextActivityDate <= GETDATE()
AND     E.IsDeleted = 0
AND     A.IsDeleted = 0
AND replace(A.ChanceOfSuccess, '%', '') < 100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM