This question already has answers here:
T-SQL and the WHERE LIKE %Parameter% clause
(3 answers)
Closed 5 years ago.
I am trying to learn more about procedures in SQL, and so i am trying to do this:
CREATE PROCEDURE GetCarInformation
(#ModelName Varchar(256))
AS
Begin
SELECT Car.LicensePlate, CarBrand.ManufacturerName, CarModel.ModelName, CarSpecs.ModelYear, CarSpecs.Doors, CarSpecs.Seats, CarSpecs.ModelYear
FROM Car
INNER JOIN CarModel ON Car.ModelID = CarModel.ID
INNER JOIN CarBrand ON CarModel.ManufacturerID = CarBrand.ID
INNER JOIN CarSpecs ON CarModel.CarSpecsID = CarSpecs.ID
WHERE CarModel.ModelName LIKE '%#ModelName%'
END
But it doesn't seem to work, on the LIKE part of it.
It wont search correctly.
Is it because i am using it incorrectly ?
Or can't i add % with the #ModelName ? and single quotes ?
You should use
LIKE '%'+#ModelName+'%'
It's the way MSSQL use to CONCAT string . You can use CONCAT() too.
Please remember to use LIKE '%xxx%' with caution. It can have really bad effects on performances.
Related
This question already has answers here:
Conditional WHERE statement SQL Server
(4 answers)
Closed 4 years ago.
I have a stored procedure that I'm trying to do a conditional WHERE clause on. I believe the solution is easy but for some reason it's escaping me.
What I'm trying to achieve is: if a ID (#pbid in this case) is passed to the stored procedure I only want to return a single record matching that parameter otherwise I'd like to return all results in the table.
Here is the what I have come up with and it's obviously not going to work because I'm trying to do an equals evaluation at PBID (PBID =) and I'm looking to conditionally make that
where pb.PBID =
CASE WHEN #pbid is not null THEN
#pbid --just return results with this ID
ELSE
is not null --return all results
END
If it was C# or something I'd write it like:
if(intPBID > 0) --parameter set, only return results with that param
{
pb.PBID = intPBID
}
else
{
pb.PBID > 0 --return everything
}
I hope this isn't too confusing and I'd appreciate any feedback.
Thanks
I'm not sure about performance implications, but I typically end up doing something like:
SELECT *
FROM table
WHERE (#pbid IS NULL OR pb.PBID = #pbid)
This question already has answers here:
alternatives to REPLACE on a text or ntext datatype
(2 answers)
Closed 5 years ago.
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(NotesColumn,',','')
WHERE NotesColumn LIKE '%,%'
when i try execute this code, i'm getting the below error.
Msg 8116, Level 16, State 1, Line 2
Argument data type text is invalid for argument 1 of replace function.
i managed to do similar code a week ago and removed all inverted commas from this column already but there is clearly something i'm missing out. (I changed the names purposely since we work for a government entity)
I'm using the code from this post but its still not working: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/12c2e9bf-a5c2-484a-a7b3-021890c61963/how-to-remove-comma-fro-sql-string?forum=transactsql
You cannot run a REPLACE on a text data type. Convert it to [n]varchar(max) first.
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(CONVERT(nvarchar(max), NotesColumn),',','')
WHERE NotesColumn LIKE '%,%'
Try it maybe problem depends on text datatype, convert it to varchar(max)
USE DBname
UPDATE notestable
SET NotesColumn= REPLACE(CONVERT(VARCHAR(MAX), fieldName),',','')
WHERE NotesColumn LIKE '%,%'
USE DBname
UPDATE notestable
SET NotesColumn= CAST(REPLACE(CAST(NotesColumn AS nvarchar(MAX)),',','')AS ntext)
WHERE NotesColumn LIKE '%,%'
This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 6 years ago.
This value is in a column in a table:
'962091','962092','962093'
I try to use this in a where. First I declare a variable:
DECLARE #KPLnr varchar(100)
SET #KPLnr = CONVERT(nvarchar(max), dbo.UF_GetOption('FastecKPL')) /* here I get the values in */
If I select, I get the correct values of #KPLnr: '962091', '962092','962093', but if I try to use it in a where statement, it seems like the value is set wrong.
I get 0 results, but if I set it manually with:
WHERE c.kpl IN ('962091', '962092','962093')
I got 414 results.
So why is WHERE c.kpl IN ('962091', '962092', '962093') not equal to
WHERE c.kpl IN (#KPLnr) in my code?
When an apostrophe is stored in a text column, you need to escape it by adding an extra apostrophe:
WHERE c.kpl IN ('962091'', ''962092'',''962093')
This question already has answers here:
Should I use != or <> for not equal in T-SQL?
(14 answers)
Closed 6 years ago.
Using SQL Server 2008 I have a query which resembles this:
SELECT *
FROM myTable mt
WHERE mt.ID !=0
What is the !=0 ? It looks like it's the same as saying <> 0.
I am unable to google this, is this in fact the same? A link to some documentation would be appreciated.
It is exactly the same operator as <>.
See MSDN for reference.
This is the C convention for "not equal". There is another C convention for "equal" that looks like ==.
I'm accessing a Firebird database through Microsoft Query in Excel.
I have a parameter field in Excel that contains a 4 digit number. One of my DB tables has a column (TP.PHASE_CODE) containing a 9 digit phase code, and I need to return any of those 9 digit codes that start with the 4 digit code specified as a parameter.
For example, if my parameter field contains '8000', I need to find and return any phase code in the other table/column that is LIKE '8000%'.
I am wondering how to accomplish this in SQL since it doesn't seem like the '?' representing the parameter can be included in a LIKE statement. (If I write in the 4 digits, the query works fine, but it won't let me use a parameter there.)
The problematic statements is this one: TP.PHASE_CODE like '?%'
Here is my full code:
SELECT C.COSTS_ID, C.AREA_ID, S.SUB_NUMBER, S.SUB_NAME, TP.PHASE_CODE, TP.PHASE_DESC, TI.ITEM_NUMBER, TI.ITEM_DESC,TI.ORDER_UNIT,
C.UNIT_COST, TI.TLPE_ITEMS_ID FROM TLPE_ITEMS TI
INNER JOIN TLPE_PHASES TP ON TI.TLPE_PHASES_ID = TP.TLPE_PHASES_ID
LEFT OUTER JOIN COSTS C ON C.TLPE_ITEMS_ID = TI.TLPE_ITEMS_ID
LEFT OUTER JOIN AREA A ON C.AREA_ID = A.AREA_ID
LEFT OUTER JOIN SUPPLIER S ON C.SUB_NUMBER = S.SUB_NUMBER
WHERE (C.AREA_ID = 1 OR C.AREA_ID = ?) and S.SUB_NUMBER = ? and TI.ITEM_NUMBER = ? and **TP.PHASE_CODE like '?%'**
ORDER BY TP.PHASE_CODE
Any ideas on alternate ways of accomplishing this query?
If you use `LIKE '?%', then the question mark is literal text, not a parameter placeholder.
You can use LIKE ? || '%', or alternatively if your parameter itself never contains a LIKE-pattern: STARTING WITH ? which might be more efficient if the field you're querying is indexed.
You can do
and TP.PHASE_CODE like ?
but when you pass your parameter 8000 to the SQL, you have to add the % behind it, so in this case, you would pass "8000%" to the SQL.
Try String Functions: Left?
WHERE (C.AREA_ID = 1 OR Left(C.AREA_ID,4) = "8000")