MSSQL Stored Procedure: Compare Parameter with Column if ISNUMERIC - sql-server

I have following table "Managers" (simplified):
ID, int
Name, nvarchar(100)
In a stored procedure that has one argument ("Search", type nvarchar), I want to select every row where
The ID-Column is exactly #Search OR
The Name-Column contains #Search.
At the moment, my select in the stored procedure looks something like this:
SELECT ID, Name FROM Managers WHERE
(ISNUMERIC(#Search) = 1 AND [ID] = CAST(#Search AS INT)) Or
Contains([Name], #Search)
If I call the stored procedure with #Search = 1321 (example), the select works.
But if I have a #Search - parameter that is not numeric (example "HES"), I get the following error:
Conversion failed when converting the nvarchar value 'HES' to data type int.
How can I fix this?
Thanks in advance
Raphi

SELECT ID, Name FROM Managers
WHERE [ID] = CASE
WHEN ISNUMERIC(#Search) = 1 AND #Search NOT LIKE '%.%' THEN CAST(#Search AS INT)
ELSE -1
END
OR Contains([Name], #Search)

Related

Testing condition before inserting records SQL server

I have a stored procedure in SQL Server that inserts records for actual expenses into a table. When the procedure is invoked the month in question is specified as part of of a variable. For example:
exec dbo.upsert_actuals_load_01_load_data 4
When the code runs it's supposed to insert the records into the column that corresponds to the month. '1' inserts values into jan_amt, '2' inserts values into feb_amt, etc.
I have written this code:
IF #month = 1
INSERT INTO #actuals_b
([forecast_yr_id]
,[entry_type]
,[unit_cd]
,[proj_nbr]
,[jan_amt]
,[feb_amt]
,[mar_amt]
...])
SELECT forecast_yr_id
, entry_type
, unit_cd
, proj_nbr
, month_amt AS jan_amt
, 0 AS feb_amt
, 0 AS mar_amt
....
FROM #actuals;
It seems inefficient to have to write the INSERT INTO statement for each IF #month = condition. Is there a better way to do this?
To expand on my comment, the correct design of your table should be something along the lines of:
--All data types are complete guesses
CREATE TABLE actuals_b ([forecast_yr_id] int,
[entry_type] varchar(10),
[unit_cd] varchar(10),
[proj_nbr] int,
MonthNum int,
Amount decimal(12,2)
...)
Then, instead of an IF...ELSE or CASE expressions, your INSERT becomes a much simpler:
INSERT INTO actuals_b([forecast_yr_id],[entry_type],[unit_cd],[proj_nbr],MonthNum,Amount,...)
SELECT forecast_yr_id,
entry_type,
unit_cd,
proj_nbr,
#month,
month_amt,
...
FROM actuals;
(Note this is pseudo-SQL in the absence of a full table definition).
I agree with Larnu here... but you could build this out dynamically if you use a global temp table in both cases (or real tables)... something like:
declare #column varchar(64) =
case
when #month = 1 then '[jan_amt]'
when #month = 2 then '[feb_amt]'
...
end
create table ##actuals_b (...your table definition...)
declare #sql varchar(max) = '
INSERT INTO ##actuals_b
([forecast_yr_id]
,[entry_type]
,[unit_cd]
,[proj_nbr]
,' + #column = ') select * from ##actuals'
print(#sql)
This assumes ##actuals only has a single amt column, which seems to be the case based off your static values for the other months.

Conversion failed when converting the varchar value '%'

I need to search the ffg table using the parameter ID(int) to the column Id(int), if ID is not supplied, I would like to return all data from that table. I have tried this query below:
SELECT *
FROM TableName
WHERE Id LIKE ISNULL (#id, '%')
But this query returns the error:
Conversion failed when converting the varchar value '%' to data type
int.
You are comparing an integer with "%"and you also using LIKE which both are wrong.
You need to use this
SELECT *
FROM TableName
WHERE (Id = #id) or (#id is null)
This is what you have to do inside ISNULL:
SELECT *
FROM TableName
WHERE ID = ISNULL(#id, ID))
Here, if #id is null, the ID (the column value itself) is passed for comparison, which will always match and results in returning all data in the table.
This is because of the ISNULL rule:
replacement_value must be of a type that is implicitly convertible to the type of check_expresssion.
In your condition:
ISNULL(#id, '%')
When #id is NULL, SQL Server tries to convert '%' to INT which causes the conversion error.
To return all rows if #id is NULL, you can do this:
SELECT *
FROM TableName
WHERE
(Id = #id OR #id IS NULL)
you need to caste Id because it is integer so do like:
Where CAST(Id AS TEXT) LIKE '123%')

t-sql view conversion failed when filtering on converted column

I am trying to create view by filtering some table, and include some converted to different type column into select list. View filter excludes from result set rows in which this column can not be converted to that type. Then I select rows from this view and filter rows using this converted column. And I always get error Conversion failed when converting the nvarchar value '2aaa' to data type int
SQL Fiddle
MS SQL Server 2008 Schema Setup:
create table _tmp_aaa (id int identity(1, 1), value nvarchar(max) not null)
go
insert _tmp_aaa (value) values ('1111'), ('11'), ('2aaa')
go
create view _tmp_v_aaa
as
select id, cast(value as int) as value from _tmp_aaa where value like '1%'
go
Query 1:
select * from _tmp_v_aaa where value = 11
Are there any workarounds?
Add to your view ISNUMERIC to check if string is numeric value:
CREATE VIEW _tmp_v_aaa
AS
SELECT
id,
[value] = CAST((CASE WHEN ISNUMERIC([value]) = 1 THEN [value] ELSE NULL END) AS INT)
FROM _tmp_aaa
WHERE [value] LIKE '1%'
AND ISNUMERIC([value]) = 1
I tried some tricks... Obviously the optimizer tries to hand down your where criterium where it is not yet tranformed. This is one problem to be solved with a. multi-statement function. Their biggest disadvantage is the advantage in this case: the optimizer will not look into it, but just take their result "as is":
create function fn_tmp_v_aaa()
returns #tbl table(id INT, value INT)
as
BEGIN
INSERT INTO #tbl
select id, cast(value as int) as value from _tmp_aaa where value like '1%'
RETURN;
END
select * from dbo.fn_tmp_v_aaa() where value=11;
If you look at the execution plan , predicates are passed down to the table something like....
And your query gets translated to something like .....
select id, cast(value as int) as value
from tmp_aaa
where CONVERT(INT, value,0) like '1%'
AND CONVERT(INT, value,0) = CONVERT(INT, 11,0)
Now if you run this query you will get the same error you get when you query against the view.
Conversion failed when converting the nvarchar value '2aaa' to data type int.
When the predicate CONVERT(INT, value,0) like '1%' is converted , you have INT on one side of the expressions and varchar on another, INT being the higher precedence, sql server tries to convert whole expression to INT and fails hence the error message.

Converting nvarchar to numeric

I have variable called #prmclientcode which is nvarchar. The input to this variable can be a single client code or multiple client codes separated by comma. For e.g.
#prmclientcode='1'
or
#prmclientcode='1,2,3'.
I am comparing this variable to a client code column in of the tables. The data type of this column is numeric(6,0). I tried converting the variable data type like below
SNCA_CLIENT_CODE IN ('''+convert(numeric(6,0),#prmclientcode+''')) (The query is inside a dynamic sql).
But when I try executing this I get the error
Arithmetic overflow error converting nvarchar to data type numeric.
Can anyone please help me here!
Thanks!
You need to convert the numeric(6,0) column to nvarchar data type. You can use below scrip to convert it to nvarchar, before processing:
SNCA_CLIENT_CODE IN ('''+convert(cast( numeric(6,0) as nvarchar(max) ),#prmclientcode+'''))
Please try with the below code snippet.
DECLARE #ProductTotals TABLE
(
ProductID int
)
INSERT INTO #ProductTotals VALUES(1)
INSERT INTO #ProductTotals VALUES(11)
INSERT INTO #ProductTotals VALUES(3)
DECLARE #prmclientcode VARCHAR(MAX)='1'
SELECT * FROM #ProductTotals
SELECT * FROM #ProductTotals WHERE CHARINDEX(',' + CAST(ProductID AS VARCHAR(MAX)) + ',' , ',' + ISNULL(#prmclientcode,ProductID) + ',') > 0
Let me know if any concern.
use following code in order to separate your variable:
DECLARE
#T VARCHAR(100) = '1,2,3,23,342',
#I int = 1
;WITH x(I, num) AS (
SELECT 1, CHARINDEX(',',#T,#I)
UNION ALL
SELECT num+1,CHARINDEX(',',#T,num+1)
FROM x
WHERE num+1<LEN(#T)
AND num<>0
)
SELECT SUBSTRING(#T,I,CASE WHEN num=0 THEN LEN(#T)+1 ELSE num END -I)
FROM x
Use can use either table function or dynamic sql query, both options will work.
Let me know if you need more help

Using variable in SQL LIKE statement

I've got a sproc (MSSQL 2k5) that will take a variable for a LIKE claus like so:
DECLARE #SearchLetter2 char(1)
SET #SearchLetter = 't'
SET #SearchLetter2 = #SearchLetter + '%'
SELECT *
FROM BrandNames
WHERE [Name] LIKE #SearchLetter2 and IsVisible = 1
--WHERE [Name] LIKE 't%' and IsVisible = 1
ORDER BY [Name]
Unfortunately, the line currently running throws a syntax error, while the commented where clause runs just fine. Can anyone help me get the un-commented line working?
If you are using a Stored Procedure:
ALTER PROCEDURE <Name>
(
#PartialName VARCHAR(50) = NULL
)
SELECT Name
FROM <table>
WHERE Name LIKE '%' + #PartialName + '%'
Joel is it that #SearchLetter hasn't been declared yet? Also the length of #SearchLetter2 isn't long enough for 't%'. Try a varchar of a longer length.
As Andrew Brower says, but adding a trim
ALTER PROCEDURE <Name>
(
#PartialName VARCHAR(50) = NULL
)
SELECT Name
FROM <table>
WHERE Name LIKE '%' + LTRIM(RTRIM(#PartialName)) + '%'
But in my opinion one important thing.
The "char(number)" it's lenght of variable.
If we've got table with "Names" like for example [Test1..Test200] and we declare char(5) in SELECT like:
DECLARE #variable char(5)
SET #variable = 'Test1%'
SELECT * FROM table WHERE Name like #variable
the result will be only - "Test1"! (char(5) - 5 chars in lenght; Test11 is 6 )
The rest of potential interested data like [Test11..Test200] will not be returned in the result.
It's ok if we want to limit the SELECT by this way.
But if it's not intentional way of doing it could return incorrect results from planned
( Like "all Names begining with Test1..." ).
In my opinion if we don't know the precise lenght of a SELECTed value, a better solution could be something like this one:
DECLARE #variable varchar(max)
SET #variable = 'Test1%'
SELECT * FROM <table> WHERE variable1 like #variable
This returns (Test1 but also Test11..Test19 and Test100..Test199).
This works for me on the Northwind sample DB, note that SearchLetter has 2 characters to it and SearchLetter also has to be declared for this to run:
declare #SearchLetter2 char(2)
declare #SearchLetter char(1)
Set #SearchLetter = 'A'
Set #SearchLetter2 = #SearchLetter+'%'
select * from Customers where ContactName like #SearchLetter2 and Region='WY'
DECLARE #SearchLetter2 char(1)
Set this to a longer char.
We can write directly too...
DECLARE #SearchLetter CHAR(1)
SET #SearchLetter = 'A'
SELECT *
FROM CUSTOMERS
WHERE CONTACTNAME LIKE #SearchLetter + '%'
AND REGION = 'WY'
or the following way as well if we have to append all the search characters then,
DECLARE #SearchLetter CHAR(1)
SET #SearchLetter = 'A' + '%'
SELECT *
FROM CUSTOMERS
WHERE CONTACTNAME LIKE #SearchLetter
AND REGION = 'WY'
Both these will work
I had also problem using local variables in LIKE.
Important is to know: how long is variable.
Below, ORDER_NO is 50 characters long, so You can not use: LIKE #ORDER_NO, because in the end will be spaces.
You need to trim right side of the variable first.
Like this:
DECLARE #ORDER_NO char(50)
SELECT #ORDER_NO = 'OR/201910/0012%'
SELECT * FROM orders WHERE ord_no LIKE RTRIM(#ORDER_NO)
It may be as simple as LIKE '%%[%3]%%' being [%3] the input variable.
This works for me with SAP B1 9.1
I ran into a similar problem. I needed to use just a small piece of a URL saved in my database where the front and ends were irrelevant.
I first attempted to use:
DECLARE #variable VARCHAR(250) = %x%;
SELECT * FROM tblone WHERE column1 LIKE '#variable'
However, this returned the error:
Arithmetic overflow error converting numeric to data type varchar
My working query was formatted:
DECLARE #variable VARCHAR(1000) = x;
SELECT * FROM tblone WHERE column1 LIKE '%'+#variable+'%'

Resources