Convert mysql query to HQL - sql

select distinct gv.geoname,gv.GeoHierLevelCode,
SUBSTRING_INDEX(SUBSTRING_INDEX(gd.geohierpath,'/',4),'/',-1)
from HavellsUserSalesForceMapping hs,geohiervalue gv,geohierpathdetails gd
where hs.userCode = '00000001'
and hs.cmpCode = gv.cmpCode
and gv.geoCode IN (hs.geoCode)
and gd.geohierpath LIKE CONCAT('%/',gv.geoCode,'/%') ;
SUBSTRING_INDEX is getting issue for me.Tell me the actual way to convert to hql

Use #Formula annotation and define a dummy field for your expression. Then the field could be used in HQL query.

Related

How to convert Sql inner query to Linq for sum of column

How to Convert this sql query to Linq.
select sum(OutstandingAmt)from IvfReceiptDetails where IvfReceiptId IN(select IvfReceiptId from IvfReceipts where PatientId = 'SI-49650')
I think it is easier to translate SQL using query comprehension syntax instead of lambda syntax.
General rules:
Translate inner queries into separate query variables
Translate SQL phrases in LINQ phrase order
Use table aliases as range variables, or if none, create range variables from table names
Translate IN to Contains
Translate SQL functions such as DISTINCT or SUM into function calls on the entire query.
Here is the code:
var IvfReceiptIds = from IvfReceipt in IvfReceipts
where IvfReceipt.PatientId = "SI-49650"
select IvfReceipt.IvfReceiptId;
var OutstandingAmtSum = (from IvfReceiptDetail in IvfReceiptDetails
where IvfReciptIds.Contains(IvfReceiptDetail.IvfReceiptId)
select IvfReceiptDetail.OutstandingAmt).Sum();
Try this, First get all IvfReceiptId in array based on your inner query used in where condition then check contains. Change name of your _context if it's different.
var arrIvfReceiptId = _context.IvfReceiptDetails.Where(p=>p.PatientId == "SI-49650").ToArray();
var sum = (from ird in _context.IvfReceiptDetails.Where(p=> arrIvfReceiptId.Contains(p.IvfReceiptId))
select OutstandingAmt).Sum();

chain string expression linq

In traditional sql we can chain expression according to if statements.
for example lets say I have variable called "firstName" and I want to get from database all users according to the value in this variable(if empty get all users)
so I will chain the sql string like that
string sql="";
if(firstname!="")
sql=String.format(" And firstname='{0}',firstName)
.ExecuteReader(System.Data.CommandType.Text,"select * from users where 1=1" + sql)
Is there a way to copy this Technique to linq expression?
something like
from U in user
where 1=1 & sql
select U
Change to method syntax instead of query syntax, and chaining is easy.
var query = user.Select(u => u);
if(firstname!="")
query = query.Where(u => u.firstname = firstname);
queries in query syntax are converted at compile-time, so there's not a mechanism to "inject" sql at run time using query syntax.

Expressing IN sql statement in Linq with computed string in where clause

I'm trying to build a linq query for the following sql query :
SELECT *
FROM [QryFiles2]
WHERE (left(Demande,6) IN (select NoBVR from tblRequest))
Knowing that QryFiles2 is an entity and tblRequest too I've come to something like that :
from f in db.QryFiles2
where f.Demande.Substring(0,6) /* in (select NoBVR from tblRequest) */
select f
The thing is I don't know how I could express the in sql statement using linq. Do you know how I could do that without using raw sql queries ?
Nb : I've tried to use the .contains method but I couldn't work it out with the computed substring.
Finally I've done this using a join statement like that :
from f in db.QryFiles2
join r in db.tblRequest on f.Demande.Substring(0,6) equals r.NoBVR
Super clean super fast no need for subqueries should have thought about that before :-)

multiple parameter "IN" prepared statement

I was trying to figure out how can I set multiple parameters for the IN clause in my SQL query using PreparedStatement.
For example in this SQL statement, I'll be having indefinite number of ?.
select * from ifs_db where img_hub = ? and country IN (multiple ?)
I've read about this in
PreparedStatement IN clause alternatives?
However I can't figure it out how to apply it to my SQL statement above.
There's not a standard way to handle this.
In SQL Server, you can use a table-valued parameter in a stored procedure and pass the countries in a table and use it in a join.
I've also seen cases where a comma-separated list is passed in and then parsed into a table by a function and then used in a join.
If your countries are standard ISO codes in a delimited list like '#US#UK#DE#NL#', you can use a rather simplistic construct like:
select * from ifs_db where img_hub = ? and ? LIKE '%#' + country + '#%'
Sormula will work for any data type (even custom types). This example uses int's for simplicity.
ArrayList<Integer> partNumbers = new ArrayList<Integer>();
partNumbers.add(999);
partNumbers.add(777);
partNumbers.add(1234);
// set up
Database database = new Database(getConnection());
Table<Inventory> inventoryTable = database.getTable(Inventory.class);
ArrayListSelectOperation<Inventory> operation =
new ArrayListSelectOperation<Inventory>(inventoryTable, "partNumberIn");
// show results
for (Inventory inventory: operation.selectAll(partNumbers))
System.out.println(inventory.getPartNumber());
You could use setArray method as mentioned in the javadoc below:
http://docs.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setArray(int, java.sql.Array)
Code:
PreparedStatement statement = connection.prepareStatement("Select * from test where field in (?)");
Array array = statement.getConnection().createArrayOf("VARCHAR", new Object[]{"AA1", "BB2","CC3"});
statement.setArray(1, array);
ResultSet rs = statement.executeQuery();

zend_db_select with a hexadecimal query parameter against a binary column

How do you represent this query as a Zend_Db_Select?
select * from t where id = x'0cc175b9c0f1b6a831c399e269772661';
The database is MySQL, using either PDO or mysqli adapters.
You have probably to use Zend_Db_Expr:
$adapter->select()->from('t')->where('id = ?', new Zend_Db_Expr("x'0cc175b9c0f1b6a831c399e269772661'"));