For example, if the front end can give a Date, then add Date='someDate' with necessary AND keyword, and also show up the Date column by SELECT. Otherwise that Date column do not show either in the condition string nor in the SELECT
It is like
if the Date is not null
then
Select .... Date as Date01 from TableName where ....AND Date01='someDate';
if the Date is null
then
Select .... from TableName where ..;
How to achieve such goal? Thank you.
If you want to return two separate select lists then you would need two queries to perform this.
You cannot hide a column in a SELECT list based on whether or not a date has been provided.
If you want to include the column and the condition, then you can use a case expression to provide a different value to the records that don't have the condition. Similar to this:
select
case when Date01='someDate'
then Date
else null end as Date01
from TableName
where yourFilters
or Date01='someDate'
Related
From a query I want to replace the null values of the "cod_account" field, the condition to fill this field is that it must take the previous value only if it satisfies that the previous records of other fields (cod_agen, cod_sub, no_prod) are equal.
Currently it is like this:
enter image description here
what is desired is
enter image description here
thanks for your advice. i solve it
use with to separate queries.
select
case when cod_account is null and class ='Primary'
then null
when cod_account is null and class='Secundary'
then (select cod_account from principals P--principals is table create in a "with"
where fa.cod_agen=P.cod_agen
and fa.cod_sub=P.cod_sub
and fa.no_prod=P.no_prod
and p.class='secundary'
)
...
from signs fa
I have data in the date column as below.
reportDate
21-Jan-17
02-FEB-17
I want to write a query to fetch data for 01/21/2017?
Below query not working in Oracle.
SELECT * FROM tablename where reportDate=to_date('01/21/2017','mm/dd/yyyy')
What is the data type of reportDate? It may be DATE or VARCHAR2 and there is no way to know by just looking at it.
Run describe table_name (where table_name is the name of the table that contains this column) and see what it says.
If it's a VARCHAR2 then you need to convert it to a date as well. Use the proper format model: 'dd-Mon-rr'.
If it's DATE, it is possible it has time-of-day component; you could apply trunc() to it, but it is better to avoid calling functions on your columns if you can avoid it, for speed. In this case (if it's really DATE data type) the where condition should be
where report_date >= to_date('01/21/2017','mm/dd/yyyy')
and report_date < to_date('01/21/2017','mm/dd/yyyy') + 1
Note that the date on the right-hand side can also be written, better, as
date '2017-01-21'
(this is the ANSI standard date literal, which requires the key word date and exactly the format shown, since it doesn't use a format model; use - as separator and the format yyyy-mm-dd.)
The query should be something like this
SELECT *
FROM table_name
WHERE TRUNC(column_name) = TO_DATE('21-JAN-17', 'DD-MON-RR');
The TRUNC function returns a date value specific to that column.
The o/p which I got when I executed in sqldeveloper
https://i.stack.imgur.com/blDCw.png
I would like convert the column name to date.
for example the column name is today, i want to convert it dynamically to today's date like MM/DD/YYYY .
as of now the column name is "Today" i want it to be current date
You can't configure a column to change its name automagically. To reflect the current day or whatever else.
But, you can change the column name by using an alias when doing a query. In order to make the things the more transparent as possible, you might want to create a view. Here is an example:
-- Some table with a column named "TODAY"
CREATE TABLE T AS (SELECT LEVEL today FROM DUAL CONNECT BY LEVEL < 5);
-- Use PL/SQL to create a view on the given table
-- with a dynamic column name
DECLARE
today varchar(10) := TO_CHAR(SYSDATE,'DD/MM/YYYY');
query varchar(200) := 'CREATE OR REPLACE VIEW V'
|| ' AS SELECT today "' || today || '"'
|| ' FROM T';
BEGIN
execute immediate query;
END;
Then, to query the "table" with the right column name, you will simply need to query V instead of T:
SELECT * FROM V;
12/12/2014
1
2
3
4
If you recreate your view daily, say by calling the above PL/SQL code from a job, you will see each day a view with the current date as the column name. But, as the underlying table is left unchanged, you will still be able to query it using the canonical name today. Which is important for example if you need to perform join on that table.
That being said, I'm not sure I will push toward such a solution. Use at your own risks!
If you want the column name heading to appear as something different than what the column name is defined in the table, you simply use the as "DisplayColumnName" clause for that column:
select user_name, today as "12/12/2014" from some_table;
But you would need to programatically generate the SQL statement for that to work. What coding environment you are using would dictate how to dynamically create a select statement.
Is there any way to change the format of a date column output based on condition?
For example, i have a table TEST with 1 Column test_date (DATE type) with two values - 20.03.2013 and NULL.
I want to do a query like below:
Select
CASE when test_date is NULL then 'NA' ELSE test_date end from TEST;
But i get an error like inconsistent char and date formats?
All WHEN parts of a CASE statement must return the same data type.
So you need to change the date to a string if you also want the value NA displayed:
case
when test_date is null then 'NA'
else to_char(test_date, 'dd.mm.yyyy')
end
It is working for me the query what you asked......
if u still getting the problem you can use the TO_CHAR() function
ex: CASE when test_date is NULL then 'NA' ELSE TO_CHAR(test_date,'MM/DD/YYYY') end from TEST;
Yes it is.
But for your case you should format your date on your case statement, like this:
Select
CASE when test_date is NULL then 'NA'
ELSE to_char(test_date, 'dd.mm.yyyy') end as date
from TEST
Edit:
As you said in your comments that you need the output format to be a date if it is not null You can't do this. As the output of a case statement should be the same type of the WHEN part.
Best option to you was pointed on the comments by #MaheswaranRavisankar. You have to use your specific date format for your excel sheet e.g if in your sheet the date format is DD/MM/YYYY you should put this format on the to_char function that way the excel will automatically convert this data to date if the column is formated as date.
I have specifiec requirement .Actually this is my query. here amount is a column in my table.but i did not mention the amount column in select statement.here can i use this column in oreder by clause.
SELECT stud_name, stud_roll, stud_prg
FROM programcl
ORDER BY 3, amount, 1;
Yes, you can mix both positional and named assignments in your ORDER BY clause.
The positional assignments must appear in your SELECT list. The named assignments do not have to.
can i use this column in oreder by clause.
Yes of course you can use a different column in order by clause that wasn't selected from your select statement.
For example
select col1 from tab1
order by col2;
by this way you get results from col1 which will be displayed on order of col2.
Its Worth trying