I don't understand how the Like operator works on non-string data types in Access? - vba

I am reading conflicting explanations of the Like operator.
I understand that this is a String operator that compares two string expressions. By that definition, should it not only work on fields with text data types, and not on numeric or date/time fields?
However, when I test the Like operator in a query (in Query Design view), it is able to compare non-string data types. This is confusing.
Can someone please explain if this is a string operator or not?

This is OT here. However, the answer is very simple:
When applying Like to a field or variable that is not text, its value will first be casted to localised text - like what you would see if you from CStr(SomeValue).
Thus, here where the decimal separator is comma, this will filter out all values for a decimal field:
Like "*.*"
and this will filter out all integer values:
Like "*,*"

Related

Checking SQLite value type - numeric vs. textual

Is it possible to filter SQLite column values in SQL based on whether the value is numeric or textual? I have seen references to using CAST for this purpose. However, it appears to be useless as SELECT CAST('1a' AS NUMERIC) passes the check for a numeric type.
The typeof() SQL function is designated for type checking. However, its result depends on both column type definition (according to the official docs) and the format used during insertion. For example, when a number is inserted as a text literal into a NUMERIC column, it is converted into a number if possible, and typeof() will return an appropriate numeric type or text, if conversion did not occur. The TEXT column, on the other hand, stores all numeric literals as text. BLOB column stores textual and numeric literals without interpretation. Therefore, a mixed-type column should be probably declared as BLOB or NUMERIC (depending on whether textual literals needs to be converted to numbers, if possible). With this behavior in mind, typeof() is well suitable for type checking.
Thats just an idea:
SELECT [FilterColumn] FROM [Table] WHERE [FilterColumn]='0' OR (ceiling(log([FilterColumn],10)) =LENGTH([FilterColumn]) AND CAST([FilterColumn] AS INTEGER)>0)
This works for integer numbers where number of digits=log([FilterColumn],10). To distinguish a single letter from casting to 0, [FilterColumn]='0' OR [FilterColumn]>0 included.
I suppose there are more elegant solutions

Lucene String and Numeric range queries

I'm just curious why Lucene doesn't distinguish string and numeric values in a standard way.. for example ['2' TO '6'] and [2 TO 6] for range queries and treat all of them by default as String.
Is there any particular reason to treat both of these cases as the string values?
Your range query example is based on lucene query syntax. In this definition it's not defined in what kind of field type you execute this query.
Basically if you apply this query to a TextField the evaluation will be based on String. If you apply this to a IntPoint the number will be interpreted as integer. Responsible for this is the QueryParser in which you add your query and your field you like to search.
In your case using an IntPoint would make sense because you want to search for an numeric range.
More details about the query parser see QueryParser Javadoc

Determine if substring corresponds to specific code (character types) in SQL

I have a collection of strings and want to filter out those where the last four characters are: (alpha)(alpha)(number)(number).
I know I can make a substring of each of these and separately, but what is the method to determine the types of the characters in the sequence?
This is for SQL in Hive.
You can use regular expressions. Something like:
where col regexp '[a-zA-Z]{2}[0-9]{2}$'

How to have different type of variable in report(.rdlc)

I have a table in MS-access like this:
the format of this column is ShortText
when I want to use it in a report designer I want to convert it's number to have only 2 decimal number and I do that using following expression:
=IIF(isNumeric(Fields!Val.Value),FormatNumber(Fields!Val.Value,2),(Fields!Val.Value))
this code do what I want in number variable but instead of getting > 305
I get #Error
where is the problem?
In Access, all of the expressions in an IIf() must be able to be evaluated even if not the one returning the result. FormatNumber() errors on the string '>305' and causes the IIf() to return error. The real issue here is bad design. Numeric data should be in a number type field.
How many variations of comparison operators or other non-number characters are used? Are there <=, >=, < comparison operators? Your code will have to extract the prefix characters if they are present. Consistency in structure is essential when manipulating strings. If the prefix is always only '>':
=IIf(IsNumeric([Val]), FormatNumber(Replace([Val],">",""), 2), [Val])
Otherwise, might require a custom function in VBA.
NOTE: Val is a reserved word in Access (it is a function) - should avoid reserved words as names for anything.

How to use BETWEEN Operator with Text Value in SQL?

How am I going to use BETWEEN Operator with Text Value or what is the right syntax when you will select all products with a ProductName for example ending with any of the letter BETWEEN 'C' and 'M'?
Most SQL dialects provide the RIGHT() function. This allows you to do:
WHERE RIGHT(TextValue, 1) BETWEEN 'C' AND 'M'
If your database doesn't have this function, you can do something similar with the built-in functions. Also, the exact comparison might depend on the collation of the column/table/database/server. Sometimes comparisons are independent of case and sometimes they are dependent on case.
In case you are interested in an alternative method (which does work with the w3schools SQL editor), you can also use the LIKE operator:
WHERE ProductName LIKE '%[c-m]'
This will get you all Product Names ending on any character between C and M.
(It does work with the w3schools SQL Editor.)
In this case, the LIKE operator is using two wildcard characters:
1.%
Any string of zero or more characters.
2.[c-m]
Any single character within the specified range ([a-f]) or set
([abcdef]).
You can find more information about the LIKE operator here:
https://msdn.microsoft.com/en-us/library/ms179859.aspx