I want to filter a query in the Where Statement. I want to compare IDs with 'IN' but only if an input has value.
If this text comes empty I don't want to compare. Prevent the error query
EXAMPLE:
StringProductIDs = ''
WHERE
({Issue}.[ServiceRequestId] IN (#StringServiceRequestIDs))
AND ({ServiceRequest}.[Id] IN (#StringServiceRequestIDs))
AND ({Product}.[Id] IN (#StringProductIDs) OR #StringProductIDs = '') /* Is this one that i want to prevent*/
AND EXTRACT(MONTH FROM {Sprint}.[CompleteDate]) = #Month
AND EXTRACT(YEAR FROM {Sprint}.[CompleteDate]) = #Year
In this case, I just want to filter things.id if the value passed as a parameter has value. If it comes empty I don't want to compare and avoid the 'AND' line
Or try another approach:). There is a way to run a query only if this input has value? If it has no value, it returns nothing. If the input has value run normally –
Many thanks
You need OR :
WHERE (things.active = 1 AND things.state = 2) AND
(things.id IN (#INPUT) OR #INPUT IS NULL);
Related
I want to remove the first character returning from the cvdl.C statement in the below SQL query and that value should be match up with the ccp.B value.
For an example if the real value return by the cvdl.C statement is 4500, I want to remove 4 and take only the 500 part to match with the value in the ccp.B value. Also I need to pass a input parameter value for the query.
How can I modify below SQL query to achieve this objective?
SELECT ccp.A
FROM ccp, cvdl
WHERE cvdl.J = 'Example' and ccp.B = cvdl.C
you should use the ansi-syntax to join
select ccp.A,
from ccp
inner join cvdl on ccp.B = substr(cvdl.C, 2)
where cvdl.J = 'Example'
I've got a query pulling data from a table. In one particular field, there are several cases where it is a zero, but I need the four digit location number. Here is where I'm running into a problem. I've got
SELECT REPLACE(locationNbr, '0', '1035') AS LOCATION...
Two issues -
Whoever put the table together made all fields VARCHAR, hence the single quotes.
In the cases where there already is the number 1035, I get 1103535 as the location number because it's replacing the zero in the middle of 1035.
How do I select the locationNbr field and leave it alone if it's anything other than zero (as a VARCHAR), but if it is zero, change it to 1035? Is there a way to somehow use TO_NUMBER within the REPLACE?
SELECT CASE WHEN locationNbr='0' THEN '1035' ELSE locationNbr END AS LOCATION...
REPLACE( string, string_to_replace , replacement_string )
REPLACE looks for a string_to_replace inside a string and replaces it with a replacent_string. That is why you get the undesired behaviour - you are using the wrong function.
CASE WHEN condition THEN result1 ELSE result2 END
CASE checks a condition and if it is true it returns result1 and if it is not it will return result2. This is a simple example, you can write a case statement with more than one condition check.
Don't use replace(). Use case:
(case when locationNbr = '0' then '1035' else locationNbr end)
You can make use of length in Oracle:
select case when length(loacation) = 1 then REPLACE(loacation, '0', '1035') else loacation end as location
from location_test;
We have a class that replaces SQL parameters with their actual HashMap values. For example, select * from x where date = %processingDate% will substitute the value of processingDate and then retrieve the corresponding records. However, it seems to not do the replacement when the parameter name is the same name as the column; for example, select * from x where date = %date% does not substitute date and then retrieves all the records because it's acting like an always true boolean. Is this expected SQL behavior?
Thanks for the help.
Given date is a reserved keyword in T-SQL I would recommend wrapping the column with [] to qualify the column.
Link to list of reserved kewords
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql
You mentioned SQL Paramater in your question. I would expect your parameter to look like #date if it is a sql parameter. If it does not have the # prefix your query may be evaluating the column and not the parameter that you expect.
I believe something like this would work for you:
--Assuming #date is being evaluated as a string
SELECT *
FROM x
WHERE [date] LIKE '%' + #date + '%'
--Evaluates the specific value (Research differences between like and = operator)
SELECT *
FROM x
WHERE [date] = #date
After looking at similar issues I'm no wiser. What I have is a value returning that has two sets of numbers then a name i.e. (xxxx;xxxx;name). I'm trying to just return the name. The original code I've written (below) works if a name value/name is present.
SELECT
SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE))
FROM
[RWADMIN].[RV_ACTIVITY_FIELDS] P
JOIN
[RWADMIN].[RW_ASSOCIATION] A ON P.activity_ID = A.activityA_id
However it breaks the report if the value is blank as it returns a "NULL",
So I've thought this would work, but I get the above error.
SELECT
ISNULL(SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE)))
FROM
[RWADMIN].[RV_ACTIVITY_FIELDS] P
JOIN
[RWADMIN].[RW_ASSOCIATION] A ON P.activity_ID = A.activityA_id
Help please.
IsNull takes an expression and a value to replace the null values. It check the expression value, if it is null returns the value that we provided for nulls and if not just returns the expression value, so if you want to return a blank when it is null you should use it this way:
ISNULL(SUBSTRING(FIELD_VALUE,75, len (FIELD_VALUE)), '')
I have a query that returns a result set which contains a certain column that needs some tweaking. Basically, in the result set, there are certain rows that contain a blank value for the applicable column. What I need to do is set all instances of that blank value to a specific string. I have tried declaring a variable and setting the variable equal to the column name (using a SELECT statement) and then using an IF statement to set the value to a specific string if it is blank (' '). My code thus far is as follows:
declare #sourceNode varchar(30)
set #sourceNode = (select sn_name from pt_cust)
if #sourceNode = '' begin
set #sourceNode = 'None'
end
This code returns an error stating that the sub-query returns more than 1 value. This seems like an easy task but I am stuck at the moment. How can this be accomplished?
This is a case (heh) for CASE:
SELECT CASE WHEN sn_name = '' THEN 'None' ELSE sn_name END
FROM pt_cust
You are getting the error message you mentioned because your SELECT statement can return more than one row, in which case it can not be assigned to a variable.