0

I have a sql table where one of the columns in 'Action' an nvarchar(max). This column can have values like HelloKitty_1.MP4 or HeMan_2.MP4, or SpiderMan_1.MP4, Hulk_4.MP4.. Here 1, 2, 1 is what I call videoIndex.

I am writing a stored procedure in SQL where I pass in the videoIndex (int) and it returns me all rows with that index. So in example above, if pass in 1, it should return me HelloKitty_1.MP4 and SpiderMan_1.MP4.

I did the following snippet but I am getting this error "Conversion failed when converting the varchar value '%_' to data type int."

DECLARE @SearchPattern varchar(40)  
SET @SearchPattern = '%' + '_' + @videoindex + '.MP4'  

DECLARE @WatchVideoCount int;  
SET @WatchVideoCount =   
     (SELECT count(*)
     FROM [dbo].[VideoWatched]
     WHERE 
     [Action] LIKE @SearchPattern)  

SELECT @WatchVideoCount as TotalTimesUsersWatchedVideo, @videoindex as videoIndex

1 Answer 1

1

Modify this:

SET @SearchPattern = '%' + '_' + @videoindex + '.MP4'

To be:

SET @SearchPattern = '%' + '_' + CAST(@videoindex AS varchar(30)) + '.MP4'

Or you can use

SET @SearchPattern = CONCAT('%' , '_' , @videoindex , '.MP4')
Sign up to request clarification or add additional context in comments.

1 Comment

Or you can use SET @SearchPattern = CONCAT('%' , '_' , @videoindex , '.MP4')

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.