Casting a text to varchar - sql

I'm attempting to cast a text to a varchar due to a previous issue I had regarding this error message:
The data types text and varchar are incompatible in the equal to operator RSS
With a quick search, I found that I need to cast the value which is text to that of a varchar. I have done so:
#Query("SELECT e FROM DeStudent e " +
"WHERE CAST(e.studentExpression as varchar) = :studentExpression" +
"AND e.deactivationTime = '9999-12-31 00:00:00.000' " +
"AND (:studentName is null OR e.name = :studentName)" +
"AND (:studentDescription is null OR e.description = :studentDescription)" +
"AND (:studentExpression is null OR e.studentExpression = :studentExpression)"
)
However I seem to be getting this error:
2020-07-08 14:42:39.947 ERROR o.h.hql.internal.ast.ErrorCounter: line 1:142: unexpected token: e
And it's not allowing me to run the application due to this modification. I'm unsure what I have done, any help? Thank you. I've tried different variations as well, such as moving the cast to the select section but it's still the same error.

The error message is telling you that the issue is with the letter 'e' not being understood. Your select clause is where the issue is. You wrote SELECT e FROM DeStudent e, which aliased the DeStudent table as 'e', but the select clause should specify columns, not just 'e'. To select everything, use *.
SELECT * FROM DeStudent e

Related

"Error when converting the nvarchar value "--" to the int-data type."

I haven't been working with SQL for that long because I'm still relatively new to the world of IT specialists. Currently I'm working on a report for an automated documentation and get the following error
Error when converting the nvarchar value "--" to the int-data type.
select tAccounts.*
from tAccounts, tDomains, tADSDocu
where tAccounts.AccountID = tDomains.AccountID
and tDomains.DomainID = tADSDocu.DomainID and tAccounts.AccountName = {PrimaryKey}
I've only recently started with SQL.
Probably the AccountName field is nvarchar and your PrimaryKey is int, that's why this may occur, maybe something like this might work;
select tAccounts.*
from tAccounts, tDomains, tADSDocu
where tAccounts.AccountID = tDomains.AccountID
and tDomains.DomainID = tADSDocu.DomainID and tAccounts.AccountName = '{PrimaryKey}'

Postgres SQL state: 22P02 - invalid input syntax for integer

I'm using a sql query to export a database from my company's program.
Everything seems to be fine till I change the date on the "where" statement with a previous one.
Please find below the code:
SELECT p."Index", p."PSN" || CAST(p."PNR"as int) AS ID,
p."PSN" AS Serie, cast(p."PNR"as int) AS Numar,
pr."PINDate" AS r_gdate,
CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM" ELSE
pr."PrimSUM"*valuta1."EXCValue" END AS r_prima_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."PrimSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."PrimSUM"/valuta2."EXCValue"
ELSE pr."PrimSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_prima_eur,
CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM" ELSE
pr."AsigSUM"*valuta1."EXCValue" END as r_sa_lei,
CASE WHEN pr."AsigEID"='2'
THEN pr."AsigSUM"
ELSE CASE WHEN pr."AsigEID"='10' THEN pr."AsigSUM"/valuta2."EXCValue"
ELSE pr."AsigSUM"*valuta1."EXCValue"/valuta2."EXCValue"
END
END AS r_sa_eur,
pr."AsigStart", pr."AsigEnd", risc."Code", plink."Index"
FROM "PolsRisc" AS pr
LEFT JOIN "Pols" as p ON p."Index" = pr."PID"
LEFT JOIN "Riscs" as risc ON pr."RID" = risc."Index"
LEFT JOIN "PRLNK" plink ON plink."PTID" = p."PTID" AND plink."RID" = risc."Index"
LEFT JOIN "EXCValues" valuta1 ON valuta1."AtDate" = pr."AsigStart" AND valuta1."EID" = pr."AsigEID"
LEFT JOIN "EXCValues" valuta2 ON valuta2."AtDate" = pr."AsigStart" AND valuta2."EID"='2'
WHERE pr."PINDate" > '2020-08-01' AND pr."IsRezil" = 'false';
When I'm using '2020-08-01' the query works well. When I try to change it to a previous one eg. '2010-01-01' a get an error:
ERROR: invalid input syntax for integer: ""
SQL state: 22P02
I was looking for a solution on the previous posts but I didn't manage to solve this issue.
It looks like it is returning "" or a null value into one of the columns you are using integer logic for. The date change is just filtering out the data that would crash it.
You may need to use coalesce to reassign the nulls as 0 and then cast it back into being an int
select
cast(coalesce(table.column, 0) as int) as result
from table
I would advice to read the chapter http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS
. It's a brief and informative read.The cause for the error message is that '' is an empty string that has no representation in a numeric type like integer

How can this SQL query be improved?

This code actually works, but I guess it can be done in an easier way, or even more efficient (because I guess comparing strings like that is not a problem but may be). I tried to convert everything into datetime and not string but failed.
This code takes the date and time row from one table, CONCATing them and compares it to the datetime row from another table. The result is: 2019-12-02 09:00:00
It is just a regular Date, Time and Datetime parameters in the table. Like this Date 2019-11-17, Time 09:00:00, Datetime 2019-01-15 16:00:00
SELECT
MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work
WHERE
fk_id_asigned = 100573 AND
(SELECT MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Apart from the fact that you force concat() to be called twice when you can use it with 3 arguments (or more), one additional problem would be this condition:
(SELECT MAX(CONCAT(f_ini, CONCAT( " ", h_ini)))
FROM posible_work) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Why not use in the left side of the inequality just concat(...) and you repeat SELECT...MAX(..)...?
I don't think that this breaks your code's logic:
SELECT
MAX(CONCAT(f_ini, ' ', h_ini))
FROM posible_work
WHERE
fk_id_asigned = 100573
AND
CONCAT(f_ini, ' ', h_ini) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = 100573)
Also it could be better if you did not hardcode 100573 twice.
Just use aliases properly:
SELECT
MAX(CONCAT(p.f_ini, ' ', p.h_ini))
FROM posible_work p
WHERE
p.fk_id_asigned = 100573
AND
CONCAT(p.f_ini, ' ', p.h_ini) >
(SELECT MAX(RIGHT(f_fin,19)) AS FechaAVI FROM next_work WHERE fk_id_worker = p.fk_id_asigned)

Why is my case statement not working with a calculation while using signed over punch?

I'm trying to write a query to go against "Signed Over Punch" using the following query:
SELECT
CASE when substring(MyField,16,1)='C' then cast(substring(MyField,9,7)+'0' AS decimal(20,2)*-1 FROM MyTable
Here's some sample data:
0000069A0000006C00000000#0000000#
From the above data, the position starts at 16 ("C") with a length of 1
And the other starts at position 9 (0) with a length of 7
But I keep getting this error:
Msg 102, Level 15, State 1, Line 139
Incorrect syntax near '*'.
Desired Output:
00000063 (The C = 3)
What am I doing wrong?
Please refer to this page for reference for signed over punch:
https://en.wikipedia.org/wiki/Signed_overpunch
You need to learn about operator precedence. This code:
[..snip..] then cast(substring(MyField,9,7)+'0' AS decimal(20,2)*-1
is executing as if it had been written
[..snip..] then cast(...) AS (decimal(20,2) * -1)
^------------------^
You're not multiplying the result of the cast, you're trying to mutiply the decimal(20,2), which is NOT a multiplicable value.
Try
then (cast(substring(MyField,9,7)+'0' AS decimal(20,2)) * -1
^------------------------------------------------^
instead.
Not sure if this will fix it or not, but you are missing a closing parenthesis. You are also missing the END statement I recommend indenting things like this to make it easier to spot these problems.
SELECT CASE
WHEN substring(MyField,16,1)='C'
THEN cast(substring(MyField,9,7)+'0' AS decimal(20,2))*-1
END
FROM MyTable
try so:
SELECT CASE when substring(MyField,16,1)='C' then cast(substring(MyField,9,7)+'0' AS decimal(20,2)) *-1 end FROM MyTable
The problems were a missing " ) " at the defore " *-1 " and a missing "end" to terminate the case when condition.
With these 2 fix the result for your example '0000069A0000006C00000000#0000000#' is -60.00.

MyBatis data querying issues

Here's a sample code:
#Results(
id = "displayData", value = {
#Result(property = "Caller", column = "c.CALLER"),
#Result(property = "Event", column = "t.RECORD_TEXT"),
#Result(property = "Receiver", column = "c.RECEIVER"),
#Result(property = "Timestamp", column = "e.RECORD_DATE")
}
)
#Select("SELECT c.CALLER, c.RECEIVER, e.RECORD_DATE, t.RECORD_TEXT FROM T_CALL c " +
"INNER JOIN T_EVENT e ON e.CALL_ID=c.RECORD_ID INNER JOIN T_EVENT_TYPE t " +
"ON e.RECORD_EVENT_ID=t.RECORD_ID WHERE c.CALLER LIKE '%${searchTerm}%' OR " +
"t.RECORD_TEXT LIKE '%${searchTerm}%' OR c.RECEIVER LIKE '%${searchTerm}%' " +
"ORDER BY ${ordering} LIMIT ${limit} OFFSET ${offset}")
List<DisplayData> findAndSortDataToDisplay(#Param("searchTerm") String searchTerm, #Param("ordering") String ordering,
#Param("limit") int limit, #Param("offset") int offset);
The problem with is that "Event" and "Timestamp" are set to NULL at runtime (other two are set properly)!
Can anyone help with some advice on how to fix this?
(The whole code is available here: https://github.com/arthurmarkus2013/SampleWebsite)
EDIT:
I'm not 100% sure, but it seems like this code does work:
#Select("SELECT c.CALLER, e.EVENTS_GROUP_ID FROM T_CALL c INNER JOIN T_EVENT e " +
"ON e.CALL_ID=c.RECORD_ID WHERE c.CALLER = ${callerId}")
int fetchEventsGroupId(#Param("callerId") int callerId);
The difference is, that this definition contains only one JOIN clause, whereas the first one contains two of them. In other words, I'm pretty much sure, that the issue is caused by those JOINs!
But the problem is, that I am provided with an absolute requirement, that both definitions work as intended!
To help you, I need more details about DisplayData type.
If Event and Timestamp has a one-to-many/one-to-one relation with T_CALL you need read about association and collection.
refer to Mybatis Docs
using annotations, refer to #Many #one Mybatis annotations
I hope this can help you, good luck !