Suppose that I have:
case
when #ID ='2386002' then ISNULL(nullif(i.call,''),i.standingOrderNumber)
when nullif(rtrim(i.call),'') is null then
nullif(rtrim(i.standingOrderNumber), '')
else case when nullif(rtrim(i.standingOrderNumber),'') is null then rtrim(i.call)
else
rtrim(i.call)
This is just a part of the procedure which does synchronization between two apps, the problem is that standingOrderNumber is not synchronized and I assume it has to to do with this code portion.
Scenario:
After entering call and standingOrderNumber like this:
call: '' (leave it empty)
standingOrderNumber: 777777
Data is stored in a table and procedure takes the data from that table and displays it on the app form, problem is, everything is displayed correctly, except for this standingOrderNumber.
Can you tell if something went wrong in the logic I submitted above ?
I think problem with the below line,
when #ID ='2386002' then ISNULL(nullif(i.call,''),i.standingOrderNumber)
here you are trying to check i.call for null value and replacing it with empty string and that makes ISNULL function useless.
Solution is not to use nullif in above line
Related
I'll try and make this clear...
Let's say I have a table with 2 columns. issue_number and issue_text. I need to grab 2 strings out of the issue_text column. The first string is something that can be hard coded with case statements since there are only so many types of issues that can be logged (note, i know this isn't the best way)
case
when issue_text like '%error%' then 'error'
else 'not found'
end as error_type
the issue_text is a string that will be formatted mostly the same, it'll have an error, more info, then an incident number, and that is the end of the string.
i.e. "Can't add address. Ref Number: 9999999"
the problem I'm having is the number will not always be the same amount of characters away from the error message.
I was wondering if there is a way to access the substring that causes a match from the like clause. like another case statement using a regex(which i know aren't supported well in sql)
case
when issue_text like '%[0-9 .]%' then (the substring match from like '%[0-9 .]%')
else 00000
end as issue_number
I am restricted to solving this issue and parsing these strings from SQL Server Management Studio or yes, I'd use .net or something to leverage.
Declare #YourTable table (ID int,issue_text varchar(150))
Insert Into #YourTable values
(1,'Can''t add address. Ref Number: 9999999'),
(2,'error')
Select ID
,Issue = Left(issue_text,PatIndex('%:%',issue_text+':')-1)
,IssueNo = substring(issue_text,PatIndex('%:%',issue_text+':')+2,25)
From #YourTable
Returns
ID Issue IssueNo
1 Can't add address. Ref Number 9999999
2 error
If there's always a space just before the number and the number is the last part of the string you can do
RIGHT(issue_text, CHARINDEX(' ', REVERSE(issue_text)) - 1)
I always have trouble attempting such. I found this as a solution, but I was curious if their was a better way of doing such.
declare #CountByZip as int
select #CountByZip = convert(decimal(18,4),count(HouseNumbers))
from zipcodeDB
Where zip is not null
And using this as the statement executes no problem!
Convert(decimal(18,4),Count(case when zip IN ('123', '456', '789') then zip else null end))/#CountByZip
However, I was curious if there was a way to convert to int in my case statement and allow that to remove the creation of the variable and select statement at the top of my example.
Convert(Bigint,Convert(decimal(18,4),Count(case when zip IN ('123', '456', '789') then zip else null end))/Count(Zip)
However, this returns the dreaded SQL error of
'Error converting data type varchar to numeric'
which is what made me switch to my declare statement at the top.
Is it possible to perform my 2nd example?
Try using something like this
cast(count(case when zip in ('123', '456', '789') then zip else null end)*1/whateveryourdividingby as decimal(10,4))
I want to use if else in where condition for Date Coulmns. Actually what i want to do is:
I have a table, which having two columns, CreatedDate and LastModifiedDate. Now what i want to check in Stored Proc is:
if LastUpdateDate is null then it will check for CreatedDate.
Below is my query:
SELECT isnull(SSA.UpdateDateTime,
isnull(SSA.CreateDateTime,'')) as LastUpdateAnswerDateTime
from SI_SurveySiteAnswer SSA
WHERE SSA.UpdateDateTime IS NOT NULL
There are other number of table in joins i am just pasting the required query only. how can i go for the check i am totally confused.
Please help me..
UPDATED:
I have write the below code, please confirm if it is the correct way to use If in where
SELECT isnull(SSA.UpdateDateTime,
isnull(SSA.CreateDateTime,'')) as LastUpdateAnswerDateTime
from SI_SurveySiteAnswer SSA
WHERE SSA.UpdateDateTime = ISNULL(SSA.UpdateDateTime,SSA.CreateDateTime)
I think i was not able to make my requirement very clear in first go, let me explain here..
i have a survey question answer table, i want to send an email notification if question has been answered, now answer can answered in one go, in that case createddate will have value not the updateddatetime,
Second case is:
answer is being updated in second go, then i need to check for the LastUpdateDateTime..
That's what i want to make in query.
You don't need any if / else functionality, just use the or operator to check that either is not null:
WHERE SSA.CreateDateTime is not null OR SSA.UpdateDateTime is not null
You can use COALESCE if you want this functionality, here are some sites for your reference.
http://www.mssqltips.com/sqlservertip/1521/the-many-uses-of-coalesce-in-sql-server/
http://sqlmag.com/t-sql/coalesce-vs-isnull
you can use case
SELECT Isnull(SSA.updatedatetime, Isnull(SSA.createdatetime, '')) AS
LastUpdateAnswerDateTime
FROM si_surveysiteanswer SSA
WHERE SSA.updatedatetime = CASE
WHEN SSA.updatedatetime IS NULL THEN
#SSA.createdatetime
ELSE SSA.updatedatetime
END
Try case statement.
SELECT isnull(SSA.UpdateDateTime,
isnull(SSA.CreateDateTime,'')) as LastUpdateAnswerDateTime
from SI_SurveySiteAnswer SSA
WHERE CASE WHEN SSA.UpdateDateTime IS NULL THEN SSA.CreateDateTime
ELSE SSA.UpdateDateTime END IS NOT NULL
I want to use case in sql statement where clause but I have a problem as I want to create a where clause condition on the basis of some value and I want to set a not in clause values on the basis of it
here is the query where am facing an issue
WHERE CODE = 'x' and
ID not in (
case
when 'app'='A' then '570','592'
when 'Q' then ID 592,90
else 592,90
END
but its not syntax
You could embed it into the SQL where clauses like this:
WHERE CODE='x' and (('app' = 'A' AND ID not in ('570', '592')) OR
('app' = 'Q' AND ID not in ('592','90')) OR ('app' != 'A' AND 'app' != 'Q' AND ID not in ('592','90'))
Or something like that. Creepy code though, so I'd suggest using different queries for different types of 'app' parameter or create a stored procedure to handle your needs.
Consider that you may be trying to combine what should actually be separate statements.
You may want to consider using a test condition (perhaps via a simple IF statement) to determine which specific T-SQL statement to actually execute.
Pseudo Code:
IF (/*Conditions are True*/)
BEGIN
--SQL Statement
END
ELSE IF (/*Some other conditions are True)
BEGIN
--SQL Statement
END
ELSE
BEGIN
--Failsafe SQL statement
END
The logical intent of the code produced will be much easier to understand and maintain going forward.
As 592 is always part of the set, and the "Q" case is the same as the default, just do like this:
where CODE = 'x' and ID not in (592, case app when 'A' then 570 else 90 end)
Excuse my language or SQL/Reporting wording
I am generating a report using Reporting Services where I have 2 drop down lists in order to show only by period and by category. I want it so if I select and I am shown all entries and if selected from any of the 2 drop downs then filter by those selections. I have a stored procedure which states the following on it's WHERE clause:
WHERE (dbo.PERIOD_LIST.PERIOD_DESC = #period) OR (dbo.CATEGORY.CATEGORY_DESC = #category)
However I cannot get this to work on Reporting Services/Visual Studio. I am shown ALL the entries instead of the filtered ones. I initialize #period and #category as NULL. So how can I make it so the report shows all rows when both attributes are null and still be able to filter by each or both of them?
How can I achieve this?
Edit: I used the suggestion Filip Popović gave me. If you're having this problem modify your prepared statement and make sure you refresh fields on your data sets since there's additional clauses on your prepared statement!
Note that in SQL, NULL = NULL is never evaluated to true. You can take it as NULL is not nothing it is something but unspecified. And You can't compare two unspecified values. You have to test COL1 IS NULL to get true if column has null value.
Try this:
WHERE ((dbo.PERIOD_LIST.PERIOD_DESC = #period) OR (#period IS NULL))
AND ((dbo.CATEGORY.CATEGORY_DESC = #category) OR (#category IS NULL))
Check the parameter for NULL as part of the condition:
WHERE ((#period IS NULL) OR (dbo.PERIOD_LIST.PERIOD_DESC = #period))
AND ((#category IS NULL) OR (dbo.CATEGORY.CATEGORY_DESC = #category))