Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Searching for a text string

Author  Topic 

bcarney
Starting Member

7 Posts

Posted - 2012-06-12 : 09:13:52
I am trying to create a query to search a text string for a specific instance of characters. The data in the field is formatted like this:"xxxx\12a34". The slash is constant (Is somewhere in the string, not always in the same location in the string) and the "12a34" can be both alpha and numeric. The data is all located in the same column in the table.
Here is what I am looking to do: find every instance of "12a34" and still display the leading characters in the results. I was able to create a query that dropped the leading "xxxx\" but the "xxxx\" is required in the output.

Data structure:
“xxxx\12a34”
“XXAAX\12a34”
“1XXAX\12a34”

Search for: "12a34"

Return:
“XXAAX\12a34”
“1XXAX\12a34”
"xxxx\12a34"

I hope this is understandable.
I am new to SQL and not a programmer any help would be appreciated.
Thanks in advance
Brian

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2012-06-12 : 10:04:04
SELECT *
FROM yourTable
WHERE yourColumn Like '%12a34%')

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

DaleTurley
Yak Posting Veteran

76 Posts

Posted - 2012-06-13 : 12:16:43
Personally I would never use the LIKE '%criteria%' search as the performance on large tables can cause severe problems becuase of being unable to benefir from an index.

If the / is always present i would create a computed column of everything after the / create an index on the column, including the column with the full data query that..

Something like

ALTER TABLE table_name ADD new_column AS SUBSTRING(original_column, CHARINDEX('\', original_column, 0) + 1, LEN(original_column) - CHARINDEX('\', original_column, 0) + 1)

CREATE INDEX index_name ON table_name (new_column) INCLUDE (original_column)

SELECT original_column
FROM table_name
WHERE new_column = '12a43'
Go to Top of Page
   

- Advertisement -