0

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
1
  • 1
    You should really be storing the chance of success in a separate integer field. Also this question isn't anything related to stored procedures it's more about string manipulation and sub stringing Commented Nov 17, 2011 at 9:05

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

no not homework. Internship work and I'm working on somebody's previous work. I'm not that great with sql.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.