?
Solved

search for string value thats part of a string

Posted on 2012-09-21
5
Medium Priority
?
510 Views
Last Modified: 2012-09-21
Hi Experts

Im using sql server 2008 R2 and im wanting to create an stored procedure that has one table involved with it that has one column varchar not null.

I want to know if its possible to pass a string param to a stored procedure and check to see if that string value is part a string.

e.g.

param = "Test Report"

and if there is a string value in table with "\\Test Directory\Test Report" that then returns the whole string value "\\Test Directory\Test Report"

Hope this makes sense :)

Thanks
0
Comment
Question by:razza_b
5 Comments
 
LVL 7

Expert Comment

by:Beneford
ID: 38421702
You can use the LIKE condition in an SQL statement:

SELECT * FROM myTable WHERE myColumn LIKE "%Test Report%"
(the % symbol is the wild card)

There's more documentation here: http://msdn.microsoft.com/en-us/library/aa933232(v=sql.80).aspx
0
 
LVL 17

Expert Comment

by:Barry Cunney
ID: 38421713
USE CHARINDEX function to check if one string is contained in another string

IF (SELECT CHARINDEX('SQL', 'Microsoft SQL Server')) > 0
    BEGIN
       PRINT 'String Found'
    END
0
 
LVL 1

Author Comment

by:razza_b
ID: 38421795
thanks for feeback guys.

If i have something like this..

CREATE    PROCEDURE [MES].[getSSRSReport]  
  @Report varchar(50)
 
AS    
SET NOCOUNT ON

        SELECT * FROM MES.SSRSReports WHERE Reports LIKE %@Report -- how do i use param with wildcard??

SET NOCOUNT OFF


-- report in table --

-- /TestDir/TestReport
0
 
LVL 7

Accepted Solution

by:
Beneford earned 1400 total points
ID: 38421867
Concatenate a string:

... WHERE Reports LIKE '%' + @param + '%'
0
 
LVL 2

Assisted Solution

by:Brainfeb
Brainfeb earned 600 total points
ID: 38421874
Try this

CREATE    PROCEDURE [MES].[getSSRSReport]  
  @Report varchar(50)
 
AS    
SET NOCOUNT ON

Declare @Sql varchar(5000)

      SET @Sql='SELECT * FROM MES.SSRSReports WHERE Reports LIKE ''%'''+ @Report + '%'''
     
      EXEC(@Sql)

SET NOCOUNT OFF
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question