简体   繁体   中英

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%". Right now the string reads something like this "40% - Thinking about it". 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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