CASE WHEN THEN (Return Column) ELSE END - sql

I'm very new to SQL and mainly use Excel, I am using Oracle BI and need to add a column that does the following.
=IF(A1="EG123456",B1,"")
I've got this far.
CASE WHEN "table1"."clientnumber" = 'EG123456'
THEN "table1"."BookingID"
ELSE ''
END
it accepts the formula but when I go to view the results it says "View Display Error"

It seems likely that BookingID might be a type other than CHAR/VARCHAR; for example perhaps it's a numeric field? All possible returns form a CASE must be of compatible type.
Would it work to use ELSE NULL instead of ELSE ''? NULL will be compatible with all types and will come across as "blank"; but depending on how you further process the results, you may need to beware of the "strange" rules for handling NULL values.
The other option might be to cast BookingID to a CHAR type. e.g if you know that it's never more than 20 characters, you could say THEN CAST(BookingID AS CHAR(20))

Related

What is the meaning of having a variable="+" ? SAS (sql)

I'm new to SAS and I'm trying to understand a code:
if MAP_ID="+" then output WORK.0201_template;
else
do;
SHEET_ID=MAP_ID;
output WORK.0201_template_f;
end;
What does it mean the MAP_ID="+"? Does it mean that it search on the table for the values where MAP_ID=+, or does it have another menaing?
Thanks
The MAP_ID="+" is a boolean expression that compares the value the variable MAP_ID to the character string literal "+". It will be true when they are the same and false otherwise.
I suspect that the main purpose of this code is to split the data into two different output datasets based on the value of MAP_ID.
It also is changing the value of SHEET_ID. That type of code also looks like something that is designed to carry forward the value of MAP_ID in a retained field SHEET_ID. If I am right then the meaning of the value of + is to keep the same sheet_id. But we would need to seem more of the code and the data to really tell.

Use String for IF statement conditions

I'm hoping someone can help answer my question, perhaps with an idea of where to go or whether what I'm trying to do is not possible with the way I want to do it.
I've been asked to write a set of rules based on the data held by our ERP form components or variables.
Unfortunately, these components and variables cannot be accessed or used outside of the ERP, so I can't use SQL to query the values and then build some kind of SQL query.
They'd like the ability to put statements like these:
C(MyComponentName) = C(MyOtherComponentName)
V(MyVariableName) > 16
(C(MyComponentName) = "") AND V(MyVariableName) <> "")
((C(MyComponentName) = "") OR C(MyOtherComponentName) = "") AND V(MyVariableName) <> "")
This should be turned into some kind of query which gets the value of MyComponentName and MyOtherComponentName and (in this case) compares them for equality.
They don't necessarily want to just compare for equality, but to be able to determine whether a component / variable value is greaterthan or lessthan etc.
Basically it's a free-form statement that gets converted into something similar to an IF statement.
I've tried this:
Sub TestCondition()
Dim Condition as string = String.Format("{0} = {1}", _
Component("MyComponent").Value, Component("MyOtherComponent").Value)
If (Condition) Then
' Do Something
Else
' Do Something Else
End If
End Sub
Obviously, this does not work and I honestly didn't think it would be so simple.
Ignoring the fact that I'd have to parse the line, extract the required operators, the values from components or variables (denoted by a C or V) - how can I do this?
I've looked at Expression Trees but these were confusing, especially as I'd never heard of them, let alone used them. (Is it possible to create an expression tree for dynamic if statements? - This link provided some detail on expression trees in C#)
I know an easier way to solve this might be to simply populate the form with a multitude of drop-down lists, so users pick what they want from lists or fill in a text box for a specific search criteria.
This wouldn't be a simple matter as the ERP doesn't allow you to dynamically create controls on its forms. You have to drag each component manually and would be next to useless as we'd potentially want at least 1 rule for every form we have (100+).
I'm either looking for someone to say you cannot do this the way you want to do it (with a suitable reason or suggestion as to how I could do it) that I can take to my manager or some hints, perhaps a link or 2 pointing me in the right direction.
If (Condition) Then
This is not possible. There is no way to treat data stored in a string as code. While the above statement is valid, it won't and can't function the way you want it to. Instead, Condition will be evaluated as what it is: a string. (Anything that doesn't boil down to 0 is treated as True; see this question.)
What you are attempting borders on allowing the user to type code dynamically to get a result. I won't say this is impossible per se in VB.Net, but it is incredibly ambitious.
Instead, I would suggest clearly defining what your application can and can't do. Enumerate the operators your code will allow and build code to support each directly. For example:
Public Function TestCondition(value1 As Object, value2 As Object, op as string) As Boolean
Select Case op
Case "="
Return value1 = value2
Case "<"
Return value1 < value2
Case ">"
Return value1 > value2
Case Else
'Error handling
End Select
End Function
Obviously you would need to tailor the above to the types of variables you will be handling and your other specific needs, but this approach should give you a workable solution.
For my particular requirements, using the NCalc library has enabled me to do most of what I was looking to do. Easy to work with and the documentation is quite extensive - lots of examples too.

SSIS Derived Column nvarchar to bit

I have a .csv file that has a column called active which contains values of 'Yes' and 'No'.
I have used the derived component within a SSIS package, but I'm struggling to get my expression working. The field in Sql server has a data type of bit.
Active == "YES" ? "0" : "1"
Any ideas where I might be going wrong ?
You're nearly there. This will work:
(DT_BOOL)(UPPER(Active) == "YES" ? 1 : 0)
So, you just use 1 and 0 rather than "1" and "0" (as you want numbers, not strings) and then cast the whole thing to a DT_BOOL, which will map just fine to SQL Server's bit type.
I've also added the UPPER, as you seem unsure whether your value is "Yes" or "YES", and the string comparison will be case-sensitive otherwise.
(Also, note that I've assumed you want Yes to map to 1 and No to map to 0, which would be the usual way around...)
I couldnt get it to work like that either. But this works..
REPLACE(REPLACE([Column 1],"yes","1"),"no","0")

Netsuite CASE WHEN expression in custom search is returning "invalid expression"

I am new to writing CASE expressions in NetSuite. I have inserted the following expression in a formula field of a custom search.
CASE WHEN {item.custitem_custid}=05 OR {item.custitem_custid}=12 THEN
({item.custitem_margin}/2)
ELSE
({item.custitem_margin}/3)
END
and getting "invalid expression as a result. I am attempting to divide the margin field value by 2 if the WHEN case is true and divide by 3 if not true. Anybody have any idea as to what is wrong with my formula?
I think you need to add parentheses:
CASE WHEN ({item.custitem_custid}=05 OR {item.custitem_custid}=12) THEN
({item.custitem_margin}/2)
ELSE
({item.custitem_margin}/3)
END
I agree with egrubaugh360 that the syntax is fine and likely the joins are not working correctly. In your forumula, just double-check the "item." portion and see what you can do.
I also agree with the NVL() recommendation, but I can also see that your denominator is 2 and 3, respectively, therefore the NVL() function won't do anything to help. PLEASE DO USE NVL() every time your denominator is an unknown value, such as from a field on your record/transaction.

Why does bcp output null when the column contains an empty string and empty string when the column is null?

This struck me as really weird behaviour and I spent a while checking for bugs in my code before I found this
"out copies from the database table or view to a file. If you specify an existing file, the file is overwritten. When extracting data, note that the bcp utility represents an empty string as a null and a null string as an empty string." (from http://msdn.microsoft.com/en-us/library/ms162802.aspx)
Obviously this allowed me to fix my problem but can anybody think of or does anybody know a reason why this is the case?
It's been some time, but I'm sure it's a backwards compatibility/legacy back to SQL Server 6.5
SQL Server 6.5 could not store empty string: there was always one space. This changed with SQL 7
So '' -> NULL and ' ' -> '' is correct from an ancient history perspective.
SELECT ARTICULO as Articulo,
case when rtrim(CCOLOR) = '' then null else rtrim(CCOLOR) end as Color,
case when rtrim(TALLE) = '' then null else rtrim(TALLE) end as Talle,
from precios
Send null in place of empty.
I find the best solution here:
https://bytes.com/topic/sql-server/answers/143738-bcp-inserting-blank-space-empty-string
I found a work around, using a Case structure in the sql query to
change empty string to a null. The BCP in turn outputs the resulting
null as an empty!
Thanks for your assistance.
Eric
This is related to the "default values" section for BCP:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server
For example, if there is a null field in a data file, the default value for the column is loaded instead.
You have to think back to days where importing plain text files from other weird systems. BCP translates '' as "not defined"(=missing data) and sets a NULL in the database (=missing data). The other way around a NULL from database must be a '' for the other systems.
To get the 'real' data out of the database use the -k switch:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/keep-nulls-or-use-default-values-during-bulk-import-sql-server#keep_nulls
The following qualifiers specify that an empty field in the data file retains its null value during the bulk-import operation, rather than inheriting a default value (if any) for the table columns.
Then you have your ASCII 0x0 in your file/database.