sql query condition on temporary column - sql

I pondered on this one a little bit. Found a few question on SO but none of them addressed my problem.
What I am trying to do is compare two dates in a table, I am returning the greater of the two using a case statement
select <-- similar code but not actual code
...
case
when date1 > date2 then date1 else date2 end as lastDate
Everything fine upto this point. I get the greater date in my result. The problem is I want to apply a WHERE clause on LastDate. Since the lastDate is a temporary column, it won't be accept in WHERE clause I came up with this syntax
if #cur_date != null <---- this is parameter to my store procedure, if not null then
case when date1 > date then date1 like '2011-%' else date2 like '2011-%'
But I get an error for the misplaced 'like' keyword. I think a statement can not be returned after the 'then' keyword in case statement. How do I do this? Do I have to use temporary table for this? I want to find the best approach.

Use your same syntax in the WHERE clause:
WHERE case when date1 > date2 then date1
else date2 END
like '2011-%'
EDIT:
Sample code for Date comparison:
WHERE case when date1 > date2 then CAST(date1 as varchar)
else CAST(date2 as varchar) END
like '2011-%'

Related

Get all rows with a list of date between two date columns in SQL

I have a table named tableA which has two date columns. Currently, I am using the below query to fetch data.
"select * from tableA where IN_Date between date1 and date2"
IN_DATE is input param from the proc
Now instead of one date IN_DATE, I want to pass a list of dates but I am not sure how to update the query. Please help.
TableA
id date1 date2
The solution to your problem
select * from tableA where (
(IN_Date between date1 and date2) or
(IN_Date between date3 and date4) or
(IN_Date between date5 and date6)
)
What you are trying to do simply is not possible.
The syntax of the between clause is:
... expression1 BETWEEN expression2 AND expression3 ...
Each expression must resolve to a single value (not a list of values). Furthermore expression2 be < expression3, otherwise results are undefined.
Where expressionN is a column name, then the single value is the value in the row currently being evaluated.
This suggests that you may be approaching this incorrectly. Please provide some sample data, and expected results. This will allow a better understanding of what you are trying to do. A description of what you are wanting to achieve would also be helpful, rather than a description of how you are trying to achieve it.
You may use a string tokenization approach like below, where the IN_DATE string parameter has comma separated list of dates in the form of YYYY-MM-DD.
select *
from tableA t
where exists
(
select 1
from xmltable
(
'for $id in tokenize($s, ",") return <i>{normalize-space ($id)}</i>'
passing IN_DATE as "s"
columns
tok char(10) path '.'
) v
where date (to_date (v.tok, 'YYYY-MM-DD')) between t.date1 and t.date2
)

oracle sql - get difference between 2 dates (sysdate minus the datevalue of a other table)

I want to know the difference betweeen 2 dates.
I need something like this:
"sysdate minus the datevalue of a other table"
Currently I have something like this, but it's not working
SELECT something FROM myTable1 WHERE mydate >= sysdate-(SELECT latestExport FROM myTable2 WHERE id=1);
The 2nd select statement prints the date of the latest export. Example "27-JUL-21".
thanks for your help.
It can not work. Because the result of
sysdate-(SELECT latestExport FROM myTable2 WHERE id=1)
is a decimal value. For example 5121.2 days difference between SYSDATE and latestExport and you can not compare your Date value mydate with a decimal. This is the logic you used:
Is "25-10-2020" >= 5121.2 ?
You need to think again about the result you want to get from this query.

How can I use the time function in a query without writing the where condition?

dExpiryDate column is the date column of my table. How can I write ExpiryDate with alias without writing the values ​​less than today in the where condition? I'm sharing an example
Select 'CustomerName' = dCustomerName,
'ExpiryDate' = CAST (dExpiryDate as date) <CAST (getdate () as date)
from dMyTable
Thank you for your help.
You need a case statement like this, I am not clear on the logic you are trying to accomplish as the question was not clear to me on that, but you can fill in the parts of your code below:
Select 'CustomerName' = dCustomerName,
--This does your check for dExpirtyDate needs to be < GetDate(), then set value to dExpiryDate, else put some other value in the column (this part was unclear, so you just replace 'somethinghereDependsOnLogic' with your logic
'ExpiryDate' = CASE WHEN CAST(dExpiryDate as date) < CAST(GETDATE() AS DATE) THEN CAST(dExpiryDate as date) ELSE 'somethinghereDependsOnLogic' END
from dMyTable

Oracle SQL Case with Condition

I have a question regarding Oracle SQL case statement.
in the where condition I would like to apply the following condition.
if salary_date is null then effective_date should be greater than 01-Jan-2016
I have the tried as
case when salary_date is null then
trunc(effective_date) >= '01-JAN-2016' else
null end
However the above resulted in
ORA-00933: SQL command not properly ended
How can I resolve this?
The problem with your code is that SQL interpreter expects to see the value after 'then' keyword, whereas you have a condition clause.
Try something like this maybe:
case when salary_date is null and trunc(effective_date) >= '01-JAN-2016'
then <value you need>
else null
You can try this without CASE statement
SELECT
..
..
WHERE ( trunc(effective_date) >= '01-JAN-2016' AND salary_date is null )
OR ( <some other condition> )
...where salary_date is not null or effective_date >= date '2016-01-01'
Since Oracle SQL does not have "IF... THEN", use basic logic to transform your boolean expression. IF a THEN b is the same thing as (NON a) OR b. This is what I did above.
DO...NOT... compare dates to strings. '01-JAN-2016' is a string, not a date. You MUST convert it to a date, for example with to_date('01-JAN-2016', 'dd-MON-yyyy').
Or, as an alternative, note how I input a "date literal" (a fixed date). If I don't need to input a time of day, I can use the expression date '2016-01-01', which is a SQL Standard (ANSI) "date literal". Then you don't need to give a date format model; it MUST ALWAYS be in the exact format yyyy-mm-dd.

Updating table column date with case expression?

I need to update column8 with data depending on two other columns with dates (date1 and date2) - if date1 is later than date2 then column8 is YES otherwise its NO.
I wrote this:
update table1
set delay = case when date1 > date2 then 'YES'
when date2 is NULL then 'YES'
else 'NO'
end
The problem is that it is probably not comparing the dates but length of the expression, because I have no in every column except for null columns... It tells me that it probably does not know I want it compare as a date But the columns in database are in date format YYYY-MM-DD.
Is there any way to update my code or in adding something to make compare the dates and not the string lengths?
Thanks!
You can try CAST function:
WHEN CAST(date1 AS DATE) > CAST(date2 AS DATE)