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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Find string

Author  Topic 

rocknpop
Posting Yak Master

201 Posts

Posted - 2013-11-29 : 03:29:16

DECLARE @TBL TABLE (NAME VARCHAR(100))

INSERT INTO @TBL VALUES('A.XYZ'),('B.PQR'),('C.JPG'),('A.ABC')

I want to find only *.XYZ,*.PQR,*.JPG and nothing else. Can we write a single regular expression to return this?

Thanks

--------------------
Rock n Roll with SQL

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-11-29 : 03:34:15
[code]where right(NAME, 3) in ('XYZ', 'PQR', 'JPG')[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-29 : 04:35:47
[code]SELECT * FROM @TBL WHERE NAME LIKE '%[.XYZ|.PQR|.JPG]'[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rocknpop
Posting Yak Master

201 Posts

Posted - 2013-11-29 : 05:16:46
Thanks to both for the solution.

--------------------
Rock n Roll with SQL
Go to Top of Page

rocknpop
Posting Yak Master

201 Posts

Posted - 2013-11-29 : 08:20:22
khtan: What if the length of the extension is 4, say .jpeg?

Visakh:

DECLARE @TBL TABLE (NAME VARCHAR(100))

INSERT INTO @TBL VALUES('A.xyz'),('B.pqr'),('C.jpg'),('A.ABC'),('A.jpeg')

SELECT * FROM @TBL WHERE NAME LIKE '%[.XYZ|.PQR|.JPEG]'

It returns .jpg too but it should not. I thought this was a generic solution, anyway we can include just any extension.

Thanks

--------------------
Rock n Roll with SQL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-11-30 : 02:05:42


DECLARE @TBL TABLE (NAME VARCHAR(100))

INSERT INTO @TBL VALUES('A.xyz'),('B.pqr'),('C.jpg'),('A.ABC'),('A.jpeg'),('A.wxyz')

SELECT t.* FROM @TBL t
CROSS APPLY (VALUES ('.XYZ'),('.PQR'),('.JPEG'))t1(pat)
WHERE NAME LIKE '%' + pat


output
-----------
NAME
-----------
A.xyz
B.pqr
A.jpeg



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

rocknpop
Posting Yak Master

201 Posts

Posted - 2013-11-30 : 10:27:44
perfect, thank you.

--------------------
Rock n Roll with SQL
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-01 : 03:58:59
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -