Nhibernate: using Expression - nhibernate

Using nHibernate, I would like to query on an integer datatype, but its always returning the exact match.
How could I write an expression that returns a list starting with the number entered?
right now I am using it as:
(clientNum is a long)
crit.Add(Expression.Like("ClientNumber", clientNum)); //this always gives me exact matches only
so I tried the following, but its complainging of a wroing type (its only expecting a string)
crit.Add(Expression.Like("ClientNumber", clientNum, MatchMode.Start));
Update: Also I tried the clientNum.ToString() but I get a db exception saying invalid type.
I can use the sql as follows to get what I want, but how do I do this in nHibernate??
SELECT * FROM ClientTable
WHERE clientNum LIKE '3%' --incase I wanted a list that starts with 3...

I am not sure about the .NET version, but hybernate supported limited casting (e.g. cast (... as ...)) in HQL. I think you would need to cast the value to a string and then apply your 'Like' clause. Should be able to do this all in HQL. Alternatively, you can do it using the SQL Expressions (ex. Expression.Sql(...)) and do the same thing using T-SQL.

Related

What is the purpose of using `timestamp(nullif('',''))`

Folks
I am in the process of moving a decade old back-end from DB2 9.5 to Oracle 19c.
I frequently see in SQL queries and veiw definitions bizarre timestamp(nullif('','')) constructs used instead of a plain null.
What is the point of doing so? Why would anyone in their same mind would want to do so?
Disclaimer: my SQL skills are fairly mediocre. I might well miss something obvious.
It appears to create a NULL value with a TIMESTAMP data type.
The TIMESTAMP DB2 documentation states:
TIMESTAMP scalar function
The TIMESTAMP function returns a timestamp from a value or a pair of values.
TIMESTAMP(expression1, [expression2])
expression1 and expression2
The rules for the arguments depend on whether expression2 is specified and the data type of expression2.
If only one argument is specified it must be an expression that returns a value of one of the following built-in data types: a DATE, a TIMESTAMP, or a character string that is not a CLOB.
If you try to pass an untyped NULL to the TIMESTAMP function:
TIMESTAMP(NULL)
Then you get the error:
The invocation of routine "TIMESTAMP" is ambiguous. The argument in position "1" does not have a best fit.
To invoke the function, you need to pass one of the required DATE, TIMESTAMP or a non-CLOB string to the function which means that you need to coerce the NULL to have one of those types.
This could be:
TIMESTAMP(CAST(NULL AS VARCHAR(14)))
TIMESTAMP(NULLIF('',''))
Using NULLIF is more confusing but, if I have to try to make an excuse for using it, is slightly less to type than casting a NULL to a string.
The equivalent in Oracle would be:
CAST(NULL AS TIMESTAMP)
This also works in DB2 (and is even less to type).
It is not clear why - in any SQL dialect, no matter how old - one would use an argument like nullif('',''). Regardless of the result, that is a constant that can be calculated once and for all, and given as argument to timestamp(). Very likely, it should be null in any dialect and any version. So that should be the same as timestamp(null). The code you found suggests that whoever wrote it didn't know what they were doing.
One might need to write something like that - rather than a plain null - to get null of a specific data type. Even though "theoretical" SQL says null does not have a data type, you may need something like that, for example in a view, to define the data type of the column defined by an expression like that.
In Oracle you can use the cast() function, as MT0 demonstrated already - that is by far the most common and most elegant equivalent.
If you want something much closer in spirit to what you saw in that old code, to_timestamp(null) will have the same effect. No reason to write something more complicated for null given as argument, though - along the lines of that nullif() call.

Firebird SQL test if string contains DATE in WHERE clause

Is there any way to test if a string contains a DATE like 31.12.2018 ?
Like
SELECT date_string FROM table where date_string LIKE '%.*.*%'
Or something like that?
You can try to use the SIMILAR TO comparison predicate, which uses SQL regular expressions. However depending on the size of the text, this might not be very efficient.
The following predicate will work:
date_string similar to '%[[:DIGIT:]]{2}.[[:DIGIT:]]{2}.[[:DIGIT:]]{4}%'
See the language reference on SIMILAR TO for details on the syntax.
You may want to rethink your design and use explicit columns with an actual SQL DATE if you need to apply query logic on them, instead of relying on parsing things within text columns.

VB.NET LINQ Method Syntax disallows implicit conversions from 'Boolean?' to 'Boolean'

I run into trouble when learning more about the Entity Framework and LINQ.
I am trying to find values from a database that match a specific date.
I have 1.250.000 entries and 36300 of them are from a specific date.
I am using plain old SQL until now and want to practice LINQ to refactor my application to work with the EF.
Can you tell where my mistake is?
The second way works but takes a lot of time ~15 sec
Try:
Where(Function(d) d.ExportedDate.HasValue AndAlso d.ExportedDate.Value = exportDate)
On this way you select only rows where the nullable column ExportedDate is not null and where it equals the given date. ExportedDate.Value returns Date as opposed to d.ExportedDate which is a Date?. If you compare a Date? with a Date you get a Boolean? as result(not a Boolean), because Nothing means undefined.
This is an interesting difference to C#, related: Why is there a difference in checking null against a value in VB.NET and C#?
To fix the next error: append ToList to create a list.

How should date-based queries in Oracle be parameterised

ResultSet rs=st.executeQuery(
"select j.vc_jo_no,
j.dt_jo_date,
p.vc_product_name
from mst_jobcard j,
mst_prod p
where j.vc_product_code=p.vc_product_code
and j.dt_jo_date=to_char("+tdate+","+"'"+dd-mm-yy+"'"+")
");
In my specified query it should display the records based on the date parameter that is being passed in the above query.
the vc_jo_no,dt_jo_date are taken from mst_jobcard table and vc_product_name is taken from mst_prod table.
i have joined the tables.
please help me in how to use the to_char function for date.
when i specify the format i.e dd-mm-yy in the to_char function it gives error.
please help..
If you're going to pass the date as a string Oracle needs it surrounded by single quotes. Also, the "dd-mm-yy" doesn't look right to me. Try this:
ResultSet rs=st.executeQuery(
"select j.vc_jo_no,
j.dt_jo_date,
p.vc_product_name
from mst_jobcard j,
mst_prod p
where j.vc_product_code=p.vc_product_code
and j.dt_jo_date=to_char('"+tdate+"','dd-mm-yy')
");
That said, this method of passing parameters is effectively NOT passing the parameter - it will cause the database to parse a different query for every different date requested, which is likely to cause a scalability issue.
The better approach would be to bind the parameter, using whatever method is provided by ResultSet to bind a variable. You may find you can even bind a date variable natively without having to convert it to a string.
So you're running:
executeQuery("select … where … j.dt_jo_date=to_char("+tdate+","+"'"+dd-mm-yy+"'"+")");
First thing, try put that string in a variable, output it and see if it's in the correct format.
Have you tried:
"… to_char("+tdate+",'dd-mm-yy'")
it seems you're doing unnecessary acrobatics with that string.
And finally, take a look at "Oracle to_char usage" by Burleson Consulting. It was the first link off of Google. Some say Google's a good place to look first.

Should I tell NHibernate/FNH to explicitly use a string data type for a param mapped to a string column?

A cohort of mine is building a somewhat long search query based on various input from the user. We've got NHibernate mapped up using Fluent NHibernate, and aside from some noob mistakes, all seems to be going well.
One issue we can't resolve in the case of this search is that for a particular parameter, NHibernate is creating sql that treats the input as int when we explicitly need it to be a string. We have a string property mapped to an nvarchar(255) column which mostly contains integer numbers, excluding some arbitrary inputs like "temporary" or long numbers like 4444444444444444 which is beyond the int limit.
In the course of testing, I've seen a couple things: 1) If I prepend a 0 to the incoming value, NH generates the sql param as a string, appropriately so; 2) If the value can realistically be converted to an int, the resulting sql treats it as so. In case #2, if I run the generated sql directly through sql server, I get an exception when the query comes across an non-integer value (such as the examples I listed above). For some reason, when I just let NH do it's thing, I'm getting appropriate records back, but it doesn't make sense; I would expect it to fail or at least tell me that something is wrong with some records that can't be evaluated by SqlServer.
The mapping is simple, the data store is simple; I would be ok leaving well enough alone if I at least understood why/how NHibernate is making this work when running the same state manually fails... Any thoughts?
Are you running the exact same code directly into SQL Server?
NHibernate parameterises all of its queries, and will in doing so define what value is passed through to SQL in the parameters. Which is probably what you're asking about, the reason SQL my fail, is that by default it will only know the difference if you input:
select * from table_name
where col_name = 5
in comparison with
select * from table_name
where col_name = '5'
If you do not define it as a string with the 's it will search for an int, and try to convert all the varchar's to ints, which will obviously fail in some cases with strings.