I'm trying to figure out how to use a YEAR entered via a prompt in a date selection criteria... so the user enters 2018 for the Balance_Year... and I want to look for the TAXFORM_DED record with an EFFDT between 01/01/YEAR and 31/12/YEAR. Where YEAR = Prompt value?
using the extract syntax might work.
e.g.
select effdt
from taxform_ded
where extract(year from effdt) between :1 AND :2
Here's a demonstration:
https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=d33f938fbb1428058cad54f74134bd28
select * from V$VERSION;
CREATE TABLE TAXFORM_DED
("EFFDT" timestamp);
INSERT ALL
INTO TAXFORM_DED ("EFFDT")
VALUES ('01-Jan-2010 12:00:00 AM')
INTO TAXFORM_DED ("EFFDT")
VALUES ('06-Jun-2013 12:00:00 AM')
INTO TAXFORM_DED ("EFFDT")
VALUES ('09-Sep-2019 12:00:00 AM')
SELECT * FROM dual;
select effdt
from taxform_ded
where extract(year from effdt) between 2012 AND 2018;
references:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions050.htm
https://www.oracletutorial.com/oracle-date-functions/oracle-extract/
Related
This is my value in the table : FY20 JAN
And i am looking for 'FY20 (M01) JAN'. How can convert like this in Oracle 11g SQL query ?
First you convert your string to a value of DATE type. Anything enclosed in double quotes is somewhat hard coded and TO_DATE function ignores them as long as they match the characters in the input in their specific locations. Here FY are in location (index) 1 and 2.
alter session set nls_date_format = 'yyyy-mm-dd';
select to_date('FY20 JAN', '"FY"yy MON') d from dual;
D
----------
2020-01-01
Then, you apply another function TO_CHAR to the date value we got above to get the desired output.
select to_char(
to_date('FY20 JAN', '"FY"YY MON')
, '"FY"yy "(M"mm")" MON'
) c from dual;
C
-----------------------
FY20 (M01) JAN
I know that you can cast a datetime to date using the date() function:
sqlite> select date('2000-01-01 10:00:00');
2000-01-01
But why does SQLite3's cast expression such as in
sqlite> select cast('2000-01-01 10:00:00' as date);
2000
only return the year?
Even using an explicit datetime() setup solely returns the year:
sqlite> select cast(datetime('2000-01-01 10:00:00') as date);
2000
Or:
sqlite> select cast(datetime('now') as date);
2019
Looking at Postgresql, it resolves both properly:
postgresql> select date('2000-01-01 10:00:00');
2000-01-01
postgresql> select cast('2000-01-01 10:00:00' as date);
2000-01-01
What's the technical explanation for SQLite3's – to me unexpected – behavior?
For SQLite there is no Date datatype. As mentioned in their documentation here: https://www.sqlite.org/datatype3.html
What you use as Date is actually TEXT.
You can check that:
select typeof(datetime('now'));
returns:
text
And:
select typeof(cast(datetime('now') as date));
returns:
integer
So the result of cast('2000-01-01 10:00:00' as date) is an integer and it's the same
integer that you get by:
select '2000-01-01 10:00:00' + 0
when SQLite implicitly converts '2000-01-01 10:00:00' to 2000 in order to use it in a mathematical operation.
In the case of dates it happens to be the numeric value of the year, but in general SQLite returns the longest substring of the TEXT, starting from the 1st character, that can be represented as an integer.
So for '2000-01-01 10:00:00' it's the substring until the 1st -, which is the year.
I want to put filter on an Informix query:
WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY
but it fails ...
Please tell where it goes wrong.
As noted in a comment, the solution is to ensure that the string is interpreted as a DATETIME value. The simple way to do that is to use the DATETIME literal notation:
DATETIME(1753-01-01 00:00:00) YEAR TO SECOND
To demonstrate:
CREATE TABLE agentstatedetail
(
eventdatetime DATETIME YEAR TO SECOND NOT NULL PRIMARY KEY,
eventname VARCHAR(64) NOT NULL
);
INSERT INTO agentstatedetail VALUES('1752-12-25 12:00:00', 'Christmas Day, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1752-12-31 12:00:00', 'New Year''s Eve, Noon, 1752');
INSERT INTO agentstatedetail VALUES('1753-01-01 12:00:00', 'New Year''s Day, Noon, 1753');
SELECT * FROM agentstatedetail WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00' - INTERVAL(3) DAY TO DAY;
This is your original WHERE clause embedded into a minimal SELECT statement. It yields the error:
SQL -1261: Too many digits in the first field of datetime or interval.
(NB: It would have been helpful to include the error message in the question.)
Here's an alternative version of the query, with the DATETIME literal in place:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < DATETIME(1753-01-01 00:00:00) YEAR TO SECOND -
INTERVAL(3) DAY TO DAY
;
Output from the sample data:
1752-12-25 12:00:00|Christmas DAY, Noon, 1752
I observe that the value calculated is a constant; you could rewrite the code as:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < DATETIME(1752-12-29 00:00:00) YEAR TO SECOND
I suspect that the value is passed as a parameter somewhere along the line.
Alternatively, you can cast the string to a DATETIME value and you'd get the same result:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < CAST('1753-01-01 00:00:00' AS DATETIME YEAR TO SECOND) -
INTERVAL(3) DAY TO DAY
;
or:
SELECT * FROM agentstatedetail
WHERE agentstatedetail.eventdatetime < '1753-01-01 00:00:00'::DATETIME YEAR TO SECOND -
INTERVAL(3) DAY TO DAY
When I try to insert datetime value 12:58 AM into the oracle table it gets inserted as 00:58. How can I insert datetime value as 12 in my oracle db? I've set my Oracle time format as 24 hr time. Any suggestions would help.
Insert statement :
INSERT INTO TABLE
(
DATE_CREATED,
PLANNED_START,
PLANNED_COMPLETION
)
VALUES
(
sysdate,
TO_CHAR(p_planned_Start_Date, 'DD-MM-YYYY HH24:MI:SS'),
TO_CHAR(end_date_, 'DD-MM-YYYY HH24:MI:SS')
);
For the 24-hour time, you need to use HH24 instead of HH.
For the 12-hour time, the AM/PM indicator is written as A.M. (if you want periods in the result) or AM (if you don't). For example:
INSERT INTO TEST (LD_DATE) Values (TO_DATE('08/30/2016', 'MM/DD/YYYY '));
And select it as below:
SELECT LD_DATE,
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH24:MI:SS') "Date 24Hr",
TO_CHAR(LD_DATE, 'DD-MM-YYYY HH:MI:SS AM') "Date 12Hr"
FROM test
;
How can I select data from a table based on weekday or weekend, like
if date is a weekday then select only historical weekday data from the table &
if date is a weekend then select only historical weekend data.
I have tried to do that in this way but no luck
DECLARE #MyDate DATE = '08/17/2013'
SELECT datename(dw,#MyDate)
SELECT * FROM MyTable
WHERE
datename(dw,DateColumnInTable) IN (
CASE WHEN (datename(dw,#MyDate) IN ('Saturday','Sunday')) THEN '''Saturday'',''Sunday'''
ELSE 'Monday'',''Tuesday'',''Wednesday'',''Thursday'',''Friday'
END )
Any I can see lots of data in my table for saturday and sunday but this query is giving me blank record set.
Here's one way:
DECLARE #MyDate DATE = '08/17/2013'
IF (DATEPART(weekday, #MyDate) IN (1,7))
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) IN (1,7)
ELSE
SELECT *
FROM MyTable
WHERE DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6
If you would like to do it in one clause you can do something like the following, but it may perform worse:
SELECT *
FROM MyTable
WHERE (DATEPART(weekday, #MyDate) IN (1,7) AND DATEPART(weekday, DateColumnInTable) IN (1,7))
OR (DATEPART(weekday, #MyDate) BETWEEN 2 AND 6 AND DATEPART(weekday, DateColumnInTable) BETWEEN 2 AND 6)