|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
LIKE and variablesDear all,
I can't work out with this query: declare @var char(20) set @var = 'look%' select name from table where field like @var No return rows when there is a lot Any input or advice would be very appreciated. -- Current location: Alicante (ES) my goodness i was using char type when the field waits for varchar.
-- Show quoteCurrent location: Alicante (ES) "Enric" wrote: > Dear all, > I can't work out with this query: > declare @var char(20) > set @var = 'look%' > select name from table where field like @var > No return rows when there is a lot > Any input or advice would be very appreciated. > -- > Current location: Alicante (ES) make it varchar(20) instead of char(20)
-- Show quoteTibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://www.solidqualitylearning.com/ "Enric" <vta***@terra.es.(donotspam)> wrote in message news:4A0A2628-779B-4C51-B324-F9E6ADC59555@microsoft.com... > Dear all, > I can't work out with this query: > declare @var char(20) > set @var = 'look%' > select name from table where field like @var > No return rows when there is a lot > Any input or advice would be very appreciated. > -- > Current location: Alicante (ES) The reason you should use Varchar instead of char is that char fields pad
with spaces at the end... So when you though you were searching for field like 'look%' you were really asking for field like 'look% ' and of course, none were found. -- Show quoteWayne Snyder MCDBA, SQL Server MVP Mariner, Charlotte, NC I support the Professional Association for SQL Server ( PASS) and it''s community of SQL Professionals. "Enric" wrote: > Dear all, > I can't work out with this query: > declare @var char(20) > set @var = 'look%' > select name from table where field like @var > No return rows when there is a lot > Any input or advice would be very appreciated. > -- > Current location: Alicante (ES) Thanks to both,
so that now the question is, if you have to choose between char(1) and varchar(1) what most expensive is? which will requires more space? -- Show quoteCurrent location: Alicante (ES) "Wayne Snyder" wrote: > The reason you should use Varchar instead of char is that char fields pad > with spaces at the end... So when you though you were searching for field > like 'look%' you were really asking for field like 'look% ' > and of course, none were found. > -- > Wayne Snyder MCDBA, SQL Server MVP > Mariner, Charlotte, NC > > I support the Professional Association for SQL Server ( PASS) and it''s > community of SQL Professionals. > > > "Enric" wrote: > > > Dear all, > > I can't work out with this query: > > declare @var char(20) > > set @var = 'look%' > > select name from table where field like @var > > No return rows when there is a lot > > Any input or advice would be very appreciated. > > -- > > Current location: Alicante (ES) Varchar has an overhead of 2 bytes.
-- Show quoteTibor Karaszi, SQL Server MVP http://www.karaszi.com/sqlserver/default.asp http://www.solidqualitylearning.com/ "Enric" <vta***@terra.es.(donotspam)> wrote in message news:14570AE8-82C7-4205-9B54-16B0E395CA4D@microsoft.com... > Thanks to both, > so that now the question is, if you have to choose between char(1) and > varchar(1) what most expensive is? which will requires more space? > -- > Current location: Alicante (ES) > > > "Wayne Snyder" wrote: > >> The reason you should use Varchar instead of char is that char fields pad >> with spaces at the end... So when you though you were searching for field >> like 'look%' you were really asking for field like 'look% ' >> and of course, none were found. >> -- >> Wayne Snyder MCDBA, SQL Server MVP >> Mariner, Charlotte, NC >> >> I support the Professional Association for SQL Server ( PASS) and it''s >> community of SQL Professionals. >> >> >> "Enric" wrote: >> >> > Dear all, >> > I can't work out with this query: >> > declare @var char(20) >> > set @var = 'look%' >> > select name from table where field like @var >> > No return rows when there is a lot >> > Any input or advice would be very appreciated. >> > -- >> > Current location: Alicante (ES) |
|||||||||||||||||||||||