PetaPoco: How to use the SQL Like keyword ( WHERE Name LIKE '%@0%')
Asked Answered
T

6

23

What is the correct syntax for this query?

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE '%@0%'", 'something');

Or should I use CHARINDEX?

Tav answered 30/8, 2011 at 3:39 Comment(0)
P
33

May be

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%something%");

Plafker answered 30/8, 2011 at 3:43 Comment(0)
F
5

I haven't tried this, but I think it is worth trying:

var l=db.Fetch<article>("SELECT * FROM articles WHERE title LIKE @0", "%" + "something" + "%");
Forestation answered 30/8, 2011 at 3:43 Comment(0)
S
5

If you have done your mappings (wich the T4 will do for you) then you could infact do it like so:

var l=db.Fetch<article>("WHERE title LIKE @0", "%something%");

Saves some typing :)

Spitz answered 30/8, 2011 at 7:50 Comment(0)
B
1

Can try like this also

var l=db.Fetch<article>("WHERE title LIKE @0", "%" + "something" + "%");
Barr answered 16/4, 2014 at 2:36 Comment(0)
Z
0

No need to write Select * From article, when you want to get all the fields. You are better off to use string.format to include the wildcards

var l=db.Fetch<article>("WHERE title LIKE @0", string.Format("%{0}%", yourVariable));
Zacarias answered 21/10, 2022 at 13:58 Comment(0)
G
-2
Articulo articulo = new Articulo();

articulo = db.SingleOrDefault<Articulo>("SELECT TOP (1) * FROM [Articulos] WHERE [CodigoEmpresa] = @0 and [CodigoArticulo] LIKE @1 ", CodigoEmpresa, codigoArticulo + "%");
Gamb answered 11/10, 2016 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.