How to get update record based on given record in oracle? - sql

I want to get record from DB based on given date value. I used Timestamp as coulmn type. I used query as
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12')
Getting empty table. where as records were present in the table.
Please, sort out me from this issue.
Thanks in advance.

May be your table contains dates with time part so try to use:
SELECT *
FROM table1
WHERE Trunc(LAST_UPDT_S) = to_date('23-AUG-12')

Try this,
SELECT *
FROM table1
WHERE LAST_UPDT_S = to_date('23-AUG-12', 'DD-MON-YY')
SEE: TO_DATE Manual

Generally it's better to use a time range - this opens the possibility of using an index if one exists:
SELECT *
FROM table1
WHERE LAST_UPDT_S >= DATE '2012-08-23'
AND LAST_UPDT_S < DATE '2012-08-24'
(Also, it's better to not use TO_DATE without specifying the date format, otherwise it will use whatever the current session's NLS setting happens to be.)

Related

Getting not valid month in Oracle sql

I have a table called Transactions that has a column called trans_date. I am just trying to do a simple query in the SQL*Plus command window
The query is
SELECT * FROM transactions WHERE
trans_date BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
When I run this query I get not valid month and there is a little * under trans_date. Most of what I have read suggests the query is right but I am not sure what the problem is. The data type is varchar2(20).
Since trans_date is a varchar and you're trying to query whether it's between two dates, you need to convert it to a date too. Assuming it has the same format as the literals in your query:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyy') BETWEEN
to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
Seems like problem is columns data type, Try convert it to date,
SELECT * FROM transactions
WHERE to_date(trans_date,'mm/dd/yyyy') BETWEEN to_date('09/11/2021','mm/dd/yyyy') AND to_date('09/12/2021','mm/dd/yyyy');
You need to convert trans_date to a date. However, you can use date constants for the comparisons:
SELECT *
FROM transactions
WHERE to_date(trans_date, 'mm/dd/yyyy') BETWEEN DATE '2021-09-11' AND DATE '2021-09-12';
You should fix your data model so the dates are stored correctly, using Oracle's built-in data type.

Comparing text type date and select the greater than dates

I have a table with column(last_update_date) of data type is text.So I need to pass a date and select the greater than dates from the table.
I tried with below query,
select batch_uuid,result
from #this
where extract_status = 'success'
and last_update_date > '02/21/2019'
But above query is not working.
Any advice please.
You would need to convert both strings to dates to compare them:
select batch_uuid,result
from mytable
where
extract_status = 'success'
and to_date(last_update_date, 'mm/dd/yyyy') > to_date('02/21/2019', 'mm/dd/yyyy')
Note:
#this is not a valid table, I changed it to mytable
do consider storing dates in a date-like datatype; using a string datatype will bite you in many ways (to start with, using a function like to_date() defeats an existing index on the column)

how to select only by date from timestamp column in postgres?

I have a table.
as you can see created_date column is timestamp field. now on selection i want to consider only date value. for e.g if i want to make selection of rows from today i want to do something like:-
select * from audit_logs where created_date = '2018-11-28';
the above query returns null. is it possible to make selection this way ?
You can use date() function
select * from audit_logs where date(created_date) = '2018-11-28'
An alternative function is date_trunc(text, timestamp), if you want more flexibility (truncate to hour, for example). Check out the postgres documentation (which is great) for all the date/time functions: https://www.postgresql.org/docs/current/functions-datetime.html
You do get a timestamp back instead of date, mind.

SQL query doesnt bring out results when queried using date

I see that a table has the data value as 18-May-2012. But when I query looking for the same date using the below query, no results are available.
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = '18-May-2012'
Could you help me sort this issue?
You need to convert string date into date with TO_DATE() function.
Also you need to take into account that your date might contain hours/minutes/seconds. In order to handle this you need to truncate submit_dt column.
In your case it would look like this:
Select Submit_Dt From Siebel.S_Order_Dtl
where TRUNC(submit_dt) = TO_DATE('18-May-2012','dd-MON-yyyy')
Try to convert the date to date format using to_date as below
Select Submit_Dt From Siebel.S_Order_Dtl
where submit_dt = to_date('18-May-2012','DD-MON-YYYY')

Oracle query not displaying date properly

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')