I've had this problem while trying to read a variable which gets a date, for example "14.07.2018" and compares it to a date column. How can I solve this? I want to show a schedule from a festival in a specific day(Orar.Data is the column which indicates the date).
The idea is to always have both sides of the comparison as equals, i.e. comparing oranges to oranges, so to speak.
To that end, it is a good practice to modify both sides, LHS and RHS, to the same format.
SQL provides the "convert" function that does this. More documentation here.
Your query should be:
SELECT
* --insert your columns
FROM
Scena
INNER JOIN
Orar ON Scena.Id_scena = Orar.Id_scena
INNER JOIN
Artist ON Orar.Id_artist = Artist.Id_artist
WHERE
Scena.Titlu_scena = #var
AND
convert(varchar, Orar.Data, 9) = convert(varchar, #var1, 9)
Related
so i try this
select sr.member,
sr.code_book,
bk.title_book,
sr.return_date,
(kg.borrowed_time - sr.borrow_date) as target back
from sirkulasi sr,
book bk,
kategori kg
where sr.code_book = bk.code_book
and sr.return_date = '';
but it say inconsistent datatypes: expected NUMBER got DATE
because the length of the loan is the number and the date of the loan is the date
the question is like this
Show circulation information that has not returned and when it is targeted to return
(The feature that has not returned is the data in the circulation table which is still dated
empty, the return target is calculated based on borrowed_time and borrowed_date according to
category)
Never use commas in the FROM clause. Always use proper, explicit, standard, readable JOIN syntax.
Second, the only part of the query that could generate the error is the -. I'm pretty sure you want:
select sr.member, sr.code_book, bk.title_book, sr.return_date,
(sr.borrow_date + kg.borrowed_time ) as target_back
from sirkulasi sr join
book bk
sr.code_book = bk.code_book
where sr.return_date is null;
Notes:
The target return date is (presumably) the borrowed day by the length of time. + is allowed between a date and a number, when first operand is a date and the second a number that represents a number of days.
return_date certainly should be a date. Non-returns should be NULL values not strings. A string is not even appropriate for a date comparison. And = '' never evaluates to "true" in Oracle because Oracle (mistakenly) treats an empty string as NULL.
The table kategori is not used in the query. Remove it.
JOIN. JOIN. JOIN.
I have a SQL join that looks like this
INNER JOIN UPLOAD PH on cast(p.PickTicket_Number as varchar(20))=PH.FIELD004
However, sometimes, the field I really want to join on is PH.FIELD003. This happens when PH.FIELD004 is a different field that has letters in it. How do I include a condition in the ON clause where if field004 does not have letters in it (is numeric), it joins just like that above, but if it does have letters in it, it instead joins on the pickticket number = field003?
You can express the logic like this:
UPLOAD PH
ON cast(p.PickTicket_Number as varchar(20)) = PH.FIELD004 OR
(TRY_CONVERT(int, PH.FIELD004) IS NULL AND
CAST(p.PickTicket_Number as varchar(20)) = PH.FIELD003
)
Notes:
This checks that FIELD004 cannot be converted to an INT. This is hopefully close enough to "has letters in it". If not, you can use a CASE expression with LIKE.
OR is a performance killer for JOIN conditions. Function calls (say for conversion) also impede the optimizer.
If performance is an issue, ask a new question.
I am guessing the "number" value is an INT; if not, change to the appropriate type.
Converting numbers to strings for comparison can be dangerous. I've had problems with leading 0's, for example.
Because of the last issue, I would recommend:
UPLOAD PH
ON TRY_CONVERT(INT, PH.FIELD004) = p.PickTicket_Number OR
(TRY_CONVERT(int, PH.FIELD004) IS NULL AND
p.PickTicket_Number= PH.FIELD003
)
Hi I have trouble executing the following query in HQL:
This is a very dangerous approach to running queries. It's the sort of thing that creates SQL injection risks. Never concatenate values into SQL or HQL strings like that. Always use the PreparedStatement approach of using placeholders in the SQL/HQL string and setting the values programmatically. This way the driver (and Hibernate in the case of HQL) can do the correct thing with the SQL that gets generated. Of course here the value is a date and not a user-submitted string, but the principle still holds.
What you need to do is run a query more like
'select stuff from bar b where b.dateCreated = ?'
In HQL you can also use named parameters, and those are usually a lot easier to read and self-documenting, e.g.
'select stuff from bar b where b.dateCreated = :date'
Then set the value as part of the call, not with string concatenation.
The problem here is that the Java/Groovy toString value of a date is nothing at all like what the date format should be in SQL (or HQL). Luckily you don't need to know what that format should be, because the JDBC driver does.
So your query should be
def co = Bid.executeQuery(
'select b.job from Bid b left join b.job j where j.dateCreated = :date',
[date: fromDates[i])
Here I'm using the name date but that's arbitrary, is just has to match the key name in the map with values. You can also use SQL-style ? chars and a list of values:
def co = Bid.executeQuery(
'select b.job from Bid b left join b.job j where j.dateCreated = ?',
[fromDates[i])
Once you get this working you'll find that comparing dates like this rarely works because the dates have to agree to the millisecond, but that's a problem for another question :)
I have the below query ....
SELECT NGPCostPosition.ProjectNo, NGPCostPosition.CostCat,
NGPCostPosition.DocumentNumber, NGPCostPosition.TransactionDate,
NGPCostPosition.UnitCost, NGPCostPosition.TotalCost,
NGPCostPosition.CreditorEmployeeName, NGPCostPosition.SummaryCostCat,
PurchaseNGP_PL.CalculatedCost,
CASE
WHEN
DATEPART(MONTH, NGPCostPosition.TransactionDate) = DATEPART(MONTH, GETDATE())
AND
DATEPART(YEAR, NGPCostPosition.TransactionDate) = DATEPART(YEAR, GETDATE())
THEN TotalCost
ELSE 0
END AS CurrentMonthCost2
FROM NGPCostPosition INNER JOIN
PurchaseNGP_PL
ON NGPCostPosition.ProjectNo = PurchaseNGP_PL.PAPROJNUMBER
AND NGPCostPosition.DocumentNumber = PurchaseNGP_PL.DocumentNumber
AND NGPCostPosition.SummaryCostCat = PurchaseNGP_PL.SummaryCostCat
WHERE NGPCostPosition.ProjectNo = #ProjectNumber
AND CostCat ='P070'
OR CostCat ='P080'
AND NGPCostPosition.ProjectNo = #ProjectNumber
AND NGPCostPosition.TotalCost = ABS(PurchaseNGP_PL.CalculatedCost)
GROUP BY NGPCostPosition.ProjectNo,
NGPCostPosition.CostCat,
NGPCostPosition.DocumentNumber,
NGPCostPosition.TransactionDate,
NGPCostPosition.UnitCost,
NGPCostPosition.TotalCost,
NGPCostPosition.CreditorEmployeeName,
NGPCostPosition.SummaryCostCat,
PurchaseNGP_PL.CalculatedCost
That gives me the below results ...
What I want to do is limit the column 'ProjectNo' to the first 5 numbers only. (eg. 12169)
Could someone advise if this is possible and what the best way to do this is?
You can do:
SELECT LEFT(NGPCostPosition.ProjectNo, 5) TruncatedProjectNumber, ....
Then change your grouping to use TruncatedProjectNumber
Well at the cost of space you can provide the first five digits into a separate column. If you don't want to use the extra space you can try something like this:
SELECT CAST(LEFT(CAST(first_five AS VARCHAR(5)), 5) AS INT)
What the above does is converts your numeric into a varchar, issues a substring function on that varchar than converts those 5 digits back into an int. It can be a costly operation depending on how often you execute it. That being said it may be in your best interest to store this value in a separate column, so you avoid recomputing it every invocation.
If this is a regular thing, then either create a view or use computed columns (documented here).
As pointed out in the comment, a good way to get the first five characters is using the left() function.
I'm trying to extract the number from a text string and join it to another table. Here's what I have so far:
SELECT sect.id,
sect.section_number,
sect.expression,
p.abbreviation
FROM sections sect
JOIN period p ON SUBSTR(sect.expression, 1, (INSTR(sect.expression,'(')-1)) = p.period_number
AND p.schoolid = 73253
AND p.year_id = 20
JOIN courses c ON sect.course_number = c.course_number
WHERE sect.schoolid = 73253
AND sect.termid >= 2000
I read some other threads and figured out how to strip out the number (which always comes before the left parenthesis). The problem is that this only accounts for two of the three styles of data that live in the sect.expression column-
9(A) - check
10(A) - check
but not
5-6(A)
5-6(A) would kick back an Oracle 01722 invalid number error.
Is there a way I could modify the substr... line so that for the 5-6(A) data type it would grab the first number (the 5) and join off of that?
It's worth mentioning that I only have read rights to this table so any solution that depends on creating some kind of helper table/column won't work.
Thanks!
You can use REGEXP_REPLACE
1) If you want to extract only numbers:
JOIN period p ON REGEXP_REPLACE(sect.expression, '[^0-9]', '') = p.period_number
2) If you want to match with the digits in the start of the string and ignore the ones that appear later:
JOIN period p ON REGEXP_REPLACE(sect.expression, '^(\d+)(.*)', '\1')
Being Oracle 10g, you could use a regex instead:
JOIN period p ON REGEXP_SUBSTR(sect.expression, '^\d+', 1, 1) = p.period_number
Admittedly, the regex I provided needs work - it will get the first number at the start of the string. If you need a more complicated regex, I recommend this site: http://www.regular-expressions.info/tutorial.html