I am doing a check for new GUID '00000000-0000-0000-0000-000000000000' in a Conditional Split step, but the expression is not recognized.
[WorkOrder.msdyn_workorderid] == "00000000-0000-0000-0000-000000000000"
The data types "DT_GUID" and "DT_WSTR" are incompatible for binary
operator "==". The operand types could not be implicitly cast into
compatible types for the operation. To perform this operation, one or
both operands need to be explicitly cast with a cast operator.
May I know how can a GUID be converted to string, or the right method to conduct this check? Thank you.
Related
This legal(!) CASE construct returns a JSON datatype:
SELECT CASE WHEN true THEN to_json(1) ELSE to_json('hello') END;
but:
ERROR: could not determine polymorphic type because input has type "unknown"
It is not "polymorphic", it is JSON.
... So, as bad workaround (lost number/string JSON representations),
SELECT to_json(CASE WHEN true THEN 1::text ELSE 'hello' END);
Is there a better way to do this SQL-to-JSON cast?
Do it the other way round:
SELECT CASE WHEN true THEN to_json(1) ELSE to_json(text 'hello') END;
Declare 'hello' as type text.
This way you retain 1 as number and 'hello' as string.
The explicit cast 'hello'::text is equivalent.
The reason is the Postgres type system. An unquoted 1 is a legal numeric constant and defaults to the Postgres data type integer. But 'hello' is just a string literal that starts out as type unknown. The function to_json() is polymorphic, that means it's input parameter is defined as ANYELEMENT. What it actually does depends on the input data type. And it does not know what to do with data type unknown. Hence the error message.
The result data type is json in either case (which is a regular Postgres data type), but that is orthogonal to the problem.
Related:
No function matches the given name and argument types
Is there a way to disable function overloading in Postgres
In mysql, if I do something like
round((amount * '0.75'),2)
it seem to work just fine like without single quotes for 0.75. Is there a difference in how mysql process this?
In the hope to close out this question, here's a link that explains type conversion in expression evaluation: https://dev.mysql.com/doc/refman/5.5/en/type-conversion.html
When an operator is used with operands of different types, type
conversion occurs to make the operands compatible. Some conversions
occur implicitly. For example, MySQL automatically converts numbers to
strings as necessary, and vice versa.
mysql> SELECT 1+'1';
-> 2
In your case, MySQL sees arithmetic and performs implicit conversion on any string contained in the expression. There is going to be an overheard in converting a string to number, but it's negligible. My preference is to explicitly type out a number instead of quoting it. That method has helped me in code clarity and maintainability.
There are many people who think that the concept of the special value null (as it is used in lanuages like C, Java, C#, Perl, Javascript, SQL etc.) is a bad idea. There are several questions about this on SO and P.SE, such as Best explanation for languages without null and Are null references really a bad thing? .
However, I could not find any language that does without them. All the languages I'm familiar with have null, or something similar (e.g. "undefined" in Perl).
I realize that proably every language needs some way to express "absence of a value". However, instead of having "null" or "undefined", this can also be made explicit by using something like Maybe (Haskell) or Optional (Guava). The principal difference to having "null" or "undefined" is that an object can only have "no value" if it has a specific type (Maybe, Optional...). In contrast, "null"/"undefined" is typically a valid value possible for every type.
Are there any languages that do not have nullor a similar concept in this sense?
Here's an incomplete list of languages that are null-safe in the sense that they don't have any non-nonnullable types:
Dart (2021): Has optional types with ? syntax.
C# 8 (2019): Has opt-in "nullable reference types".
Kotlin (2015): Has optional types with ? syntax.
Pony (2015). Uses union type where one of the types is None.
Swift (2014): Has optional types with ? syntax.
Crystal (2014): Does have nil, but prevents all null pointer exceptions at compile-time.
Hack (2014): Has optional types with ? syntax.
TypeScript (2012): Has union types that can have undefined or null as a variant.
Elm (2012): Has union type Maybe.
Ceylon (2011): Has optional types with ? syntax.
Rust (2010): Has optional type Option.
Fantom (2005): Has optional types with ? syntax.
F# (2005): Has union type Option.
Nice (2003): Has optional types with ? syntax.
Netlogo (1999) has no type null
OCaml (1996): Has union type option.
Haskell (1990): Has union type Maybe.
Standard ML (1990): Has union type option.
Tcl (1988)
Erlang (1986)
Prolog (1972): A logical variable stands for "anything at all". There is no concept of "null" or "undefined".
Feel free to complement the list. The years represent the first public release.
Tcl has no concept of null whatsoever. Everything is a value and all values have a string representation (typically summarized as "Everything is a String").
The closest thing to null is the empty string.
To convey the concept of "no value" requires some creativity.
Of course, as mentioned above, some people use the empty string to signify no value. For this to work, empty strings cannot be valid in the data set you're processing. Surprisingly, a lot of real world data falls into this category.
Another way to indicate absence of value is to simply throw an error. In some cases this is exactly what should have been done instead of returning some null or error value (an anti-pattern learned from C and a habit that's hard to get rid of).
Yet another way is to return an empty list (a list is Tcl's equivalent of arrays in other languages). The string representation of an empty list is the empty string. But fortunately the string representation of a list containing an empty string is two double quotes: "\"\"". This difference allows one to differentiate between a list that contains "nothing" and a list that contains a string that has no characters in it.
Finally some people simply indicate the absence of values by simply not declaring the variable (or undeclaring it, which is a thing in tcl). This may sound odd because variables seem to be a compile-time construct (while values are run-time construct) but in tcl everything is run-time. Thus it's possible for code to use non existence of the variable as a signal. Trying to read an undeclared variable results in an error which you can catch. In addition, Tcl also allows you to use introspection to check the state of the interpreter. So you can use [info exist x] to check if a variable called x exists.
V is a newer language with Golang-like syntax that has no nulls.
You already mention Haskell as an example of a language without "null". There are also the languages in the ML family like Standard ML, OCaml or F#. Many dynamically typed languages also do not feature null pointers, scheme would be a good example.
I'm trying to understand a simple basic concept regarding JSON strings. I'm running a simple test that looks like this:
$(document).ready(function() {
var last = 9;
var json1 = $.parseJSON('{"id":"10"}');
var json2 = $.parseJSON('{"id":10}');
if(json1.id > last)
alert("json1.id is greater than last");
if(json2.id > last)
alert("json2.id is greater than last");
});
Since the variable "last" is type int I'm trying to make a comparison between it and the "id" from two different JSON strings. json1 denotes the ten value as a string, whereas json2 denotes it as an integer value. When this is run, both alerts are executed. I did not expect that. I expected that the second alert would execute, but not the first one since ten is presented as a string.
I believe that the correct way to format an integer value in JSON is in json2, right?
Why is the first test executing the alert?
I'm trying to troubleshoot a larger project and thought the problem might be in the way the JSON string is formatted.
The documentation of Javascript's operators holds all the answers:
Strings are compared based on standard lexicographical ordering, using
Unicode values. In most cases, if the two operands are not of the same
type, JavaScript attempts to convert them to an appropriate type for
the comparison. This behavior generally results in comparing the
operands numerically. The sole exceptions to type conversion within
comparisons involve the === and !== operators, which perform strict
equality and inequality comparisons. These operators do not attempt to
convert the operands to compatible types before checking equality.
Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators
Using SQL Server Integration Services (SSIS) to perform incremental data load, comparing a hash of to-be-imported and existing row data. I am using this:
http://ssismhash.codeplex.com/
to create the SHA512 hash for comparison. When trying to compare data import hash and existing hash from database using a Conditional Split task (expression is NEW_HASH == OLD_HASH) I get the following error upon entering the expression:
The data type "DT_BYTES" cannot be used with binary operator "==". The type of one or both of the operands is not supported for the operation. To perform this operation, one or both operands need to be explicitly cast with a cast operator.
Attempts at casting each column to a string (DT_WSTR, 64) before comparison have resulted in a truncation error.
Is there a better way to do this, or am I missing some small detail?
Thanks
Have you tried expanding the length beyond 64? I believe DT_BYTES is valid up to 8000 characters. I verified the following are legal cast destinations for DT_BYTES based on the books online article:
DT_I4
DT_UI4
DT_I8
DT_UI8
DT_STR
DT_WSTR
DT_GUID
DT_IMAGE
I also ran a test in BIDS and verified it had no problem comparing the values once I cast them to a sufficiently long data type.
SHA512 is a bit much as your chances of actually colliding are 1 in 2^256. SHA512 always outputs 512 bits which is 64 bytes. I have a similar situation where I check the hash of an incoming binary file. I use a Lookup Transformation instead of a Conditional Split.
This post is older but in order to help other users...
The answer is that in SSIS you cannot compare binary data using the == operator.
What I've seen is that people will most often convert (and store) the hashed value as varchar or nvarchar which can be compared in SSIS.
I believe the other users have answered your issue with "truncation" correctly.