I am trying to capture a specific word within a column. I want to extract the word that starts with 'lat'. Basically would like to capture latship , latprod, latdfu
| JobName |
| ------------ |
| pl_blob_consolidated - latship |
| pl_blob - latprod |
| pl_blob - latdfu - final |
| pl_blob - latdfu (final_blob) |
| pl_blob - latdfu - final_blob - sql |
DECLARE @tbl TABLE (id INT IDENTITY PRIMARY KEY, JobName VARCHAR(MAX));
INSERT INTO @tbl (JobName) VALUES
('pl_blob_consolidated - latship'),
('pl_blob - latprod'),
('pl_blob - latdfu - final'),
('pl_blob - latdfu (final_blob)'),
('pl_blob - latdfu - final_blob - sql');
Sample output:
JobName |
---|
latship |
latprod |
latdfu |
latdfu |
latdfu |
select DISTINCT SUBSTRING(JobName, CHARINDEX('lat', JobName)
, LEN(JobName) - CHARINDEX('lat', JobName) + 1)
, LEN(JobName) - CHARINDEX('lat', JobName) + 1
from tableName
Tried this, but won't stop after the word ends.
Thanks
2条答案
按热度按时间3ks5zfa01#
you can try this with some regular expressions (SQL Server function PATINDEX) : column name myField, table myTable:
n3h0vuf22#
Please try the following solution.
SQL
Output