Produce a list of films which have a date of release before August 2016.
Hello trying to produce the following SQL query above with this:
SELECT *
FROM FILM
WHERE DATE_OF_RELEASE < '2016-08-01';
but I cannot seem to figure out why I am getting errors?
Trying to answer this as well: Write a SQL statement to count the number of films in the database released before August 2016.
SELECT COUNT(*) AS "Number of films released before August 2016 "
FROM FILM;
WHERE date_of_release < date '2016-08-01';
however get this error:
Error starting at line : 18 in command -
WHERE date_of_release < date '2016-08-01'
Error report -
Unknown Command
Don't do it the wrong way - do it the right way.
If DATE_OF_RELEASE column's datatype is DATE (it should be), then compare it to DATE, not a string. '01-aug-16' or '2016-08-01' are strings, not dates. Don't rely on Oracle and its capabilities to implicitly convert them to dates, because sooner or later it'll fail, as soon as NLS settings change.
So, use
where date_of_release < date '2016-08-01' -- date literal
or
where date_of_release < to_date('01.08.2016', 'dd.mm.yyyy') -- TO_DATE function
Leave strings alone.
Try this
SELECT * FROM FILM WHERE DATE_OF_RELEASE < '01-AUG-16';
Related
I am trying to place data corresponding to a certain month into a temp table from an SQL database.
DROP TABLE
#ComPAIR_Alliance_Table
SELECT
IMOno
,period
,[service]
,alliances
INTO
#ComPAIR_Alliance_Table
FROM
com_COMPAIR.dbo.Data_BlueWaterCapacity_US_2
WHERE
LEFT(period, 7) = '2015-03'
SELECT
*
FROM #ComPAIR_Alliance_Table
The period field is in the following format: 2015-03-29 00:00:00.000
However, my code just returns an empty temp table with the right column names but no rows (even though I know for sure rows with that date exist in my table). I have already made sure that the period column is indeed a string by using is.numeric.
Could someone please help me out with what the problem could be?
Thanks!
If it is a date/datetime/datetime2 then you can compare it with 2015-03 like:
WHERE period >= '2015-03-01'
AND preiod < DATEADD(MONTH, 1, '2015-03-01')
In case there is confusion:
The above will match all March 2015 dates such as 2015-03-31, 2015-03-31 23:59:59 and 2015-03-31 23:59:59.9999999
The above is sargable: the DATEADD part does not depend on the table rows
Guessing Period is a date. If it is, stop treating it like a varchar, it isn't one. If you want values from March 2015 then do:
WHERE period >= '20150301'
AND period < '20150401'
LEFT is doing some weird stuff, because LEFT causes an implicit cast to String from Date. You can see this question for more information, but you're getting exactly what you told SQL to get - a join on rows where the left 7 characters of period equal '2015-03' which will not happen, since you're liking comparing against something like 'Jan 01'
The LEFT function needs to implicitly convert your datetime column to a varchar value to do it's work. SQL Server is choosing the varchar format of the date based on it's internationalization settings. On my server, its Mar 29 2015 12:00AM, and LEFT yields Mar 29. That's why it's not equal to 2015-03.
You should treat your column as a datetime and then perform the comparison using a valid datetime comparison, like this :
WHERE period BETWEEN '1/1/2015' AND '1/31/2015'
the date is stored as a date type. You may want to try
where convert(varchar(20), period,121)
which would convert it to string...
I appologize for this simple question but cannot find a straight and simple answer and am going crazy.
I am running on DB2 and I want to
Select date_column from my_table
but I want to select date_column as number the way Excel does it. So for example: 2017-02-26 would show up as 43157.
date_column is a DATE type in my table.
Assuming you are using the Excel 1900 date system, where the number of days is measured since January 1, 1900, you may try the following query which uses the DAYS function to find a date difference:
SELECT
DAYS(date_column) - DAYS('1900-01-01') AS excel_date
FROM my_table;
I'm trying to make a query dynamic that looks for all the dates between two days of the year (July 1 of the last year and June 30th of this year). Below is the where clause of the query I've been attempting:
and pbh.batch_date between
('30-JUN-'||extract(year from trunc(sysdate))-1))
and ('01-JUL-'||extract(year from trunc(sysdate))))
This returns an inconsistent datatypes error: expected DATE got NUMBER. I then tried casting them to dates like so:
and pbh.batch_date between
to_date(( '01-JUL-'||extract(year from trunc(sysdate))-1))
and to_date(( '30-JUN-'||extract(year from trunc(sysdate))))
But this now returns an invalid number error. I don't know if it makes a difference what format pbh.batch_date is stored as, but it's (m)m/(d)d/yyyy
There's no need to cast to a string and back:
and ph.batch_date >= add_months(trunc(sysdate,'YEAR'), -6)
and ph.batch_date < add_months(trunc(sysdate,'YEAR'), 6)
I understand that querying a date will fail as its comparing a string to date and that can cause an issue.
Oracle 11.2 G
Unicode DB
NLS_DATE_FORMAT DD-MON-RR
select * from table where Q_date='16-Mar-09';
It can be solved by
select * from table where trunc(Q_date) = TO_DATE('16-MAR-09', 'DD-MON-YY');
What I don't get is why this works.
select* from table where Q_date='07-JAN-08';
If anyone can please elaborate or correct my mindset.
Thanks
Oracle does allow date literals, but they depend on the installation (particularly the value of NLS_DATE_FORMAT as explained here). Hence, there is not a universal format for interpreting a single string as a date (unless you use the DATE keyword).
The default format is DD-MM-YY, which seems to be the format for your server. So, your statement:
where Q_date = '07-JAN-08'
is interpreted using this format.
I prefer to use the DATE keyword with the ISO standard YYYY-MM-DD format:
where Q_Date = DATE '2008-01-07'
If this gets no rows returned:
select * from table where Q_date='16-Mar-09';
but this does see data:
select * from table where trunc(Q_date) = TO_DATE('16-MAR-09', 'DD-MON-YY');
then you have rows which have a time other than midnight. At this point in the century DD-MON-RR and DD-MON-YY are equivalent, and both will see 09 as 2009, so the date part is right. But the first will only find rows where the time is midnight, while the second is stripping the time off via the trunc, meaning the dates on both sides are at midnight, and therefore equal.
And since this also finds data:
select* from table where Q_date='07-JAN-08';
... then you have rows at midnight on that date. You might also have rows with other times, so checking the count with the trunc version might be useful.
You can check the times you actually have with:
select to_char(q_date, 'YYYY-MM-DD HH24:MI:SS') from table;
If you do want to make sure you catch all times within the day you can use a range:
select * from table where
q_date >= date '2009-03-16'
and q_date < date '2009-03-17';
Quick SQL Fiddle demo.
Although it sounds like you're expecting all the times to be midnight, which might indicate a data problem.
In Oracle, there is a column(dateColumn) in a table with column type = DATE and the value for a particular record is showing as '10/03/2010' when I do a select * from table.
Now, when I do:
SELECT *
FROM table
WHERE dateColumn < '01-JAN-11'
Nothing shows up. When I do:
SELECT *
FROM table
WHERE dateColumn > '01-JAN-11'
The record shows up.
Why is this behaving this way? "10/03/2010" is 10th MArch 2010 so clearly that is < 01 Jan 2011.
There is no definitive date format -- it can be different by region or even business. That's the reality without even considering SQL...
In Oracle, the means of evaluating a string as a date requires you to use the TO_DATE function:
SELECT *
FROM table
WHERE dateColumn > TO_DATE('01-JAN-11', 'DD-MON-YY')
You need to supply the format mask to instruct Oracle how to interpret the date you're supplying it.
The function is often different on other databases, but the experience is otherwise the same. Most databases accept 'YYYY-MM-DD' for implicit conversion of a string into a date. Explicit conversion is when you use a function, like TO_DATE to explicitly change the data type.
Try to use to_date:
select * from table where dateColumn < to_date('01-JAN-11', 'dd-MON-YY')