Comparing text type date and select the greater than dates - sql

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)

Related

SQL Oracle table blank when trying to query date data stored as varchar

I have a column called received_dt_key in Varchar in the format DD-MM-YYYY (e.g. 30-07-2021).
I would like to select all from the table for dates between 31-12-2021 and 01-01-2022. I have tried version of the below query and a blank table is the output.
SELECT *
FROM SD_BDAY
WHERE to_char(to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY')) > to_char(to_date('31-12-2021', 'DD-MM-YYYY'))
and to_char(to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY')) < to_char(to_date('01-01-2022', 'DD-MM-YYYY'));
Don't compare dates as strings. Compare them as dates:
SELECT *
FROM SD_BDAY
WHERE to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY') > to_date('31-12-2021', 'DD-MM-YYYY')
and to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY') < to_date('01-01-2022', 'DD-MM-YYYY');
If you try to compare them as strings then you are looking for string that is greater than '31-12-2021' and less than '01-01-2022' and the string comparison will look at the first character and see if it can find a match which is greater than '3' and less than '0'; there can never be such a match so it is quite correct that when comparing as strings nothing is returned.
As pointed out by #AlexPoole in comments, even if you compare the values as dates (rather than strings) you will still never return a result as finding values that are greater than DATE '2021-12-31' and less than DATE '2022-01-01' would return all dates from 2021-12-31 00:00:01 to 2021-12-31 23:59:59; however, your values will always be converted with a midnight time component and, therefore, will never fall into that range so cannot be returned.
What you probably want is to use >= rather than > and then it would match values on 2021-12-31.
The best thing would be to store calendar dates in date data type column. Why else do you think Oracle designed that data type? This way you may create normal indexes on data data type columns, or, if needed, partition the table by that date column.
Still, if you insist in having the calendar dates stored like that, I think the below should work:
SELECT *
FROM SD_BDAY
WHERE to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY') >
to_date('31-12-2021', 'DD-MM-YYYY')
and to_date(RECEIVED_DT_KEY, 'DD-MM-YYYY') <
to_date('01-01-2022', 'DD-MM-YYYY');
Thus you compare calandar dates with calendar dates, not varchar with varchar, as it results from the code you have written.
And what if in the varchar2 column there is somethibng that can't be converted to date? That is why it is best to use the date data type.

Date comparison between same data type in sql query

I have a column in the format -
MAX_DATE PROCESS_DATE
2022-10-14T16:09:09.272+00:00 2022-11-08T15:43:29.027+00:00
WHEN I AM WRITING THE CONDITION
SELECT * FROM ANC
WHERE MAX_DATE > PROCESS_DATE
Even then a lot of rows are coming although they do not fulfill the condition. The date format of both is the same, i am not sure how to change both of them in the same date format, i tried the below but still I am getting all rows so the above condition is not working
SELECT * FROM ANC
WHERE TO_DATE(to_char(MAX_DATE,'DD-MM-YYY'),'DD-MM-YYY') > TO_DATE(to_char(PROCESS_DATE,'DD-MM-YYY'),'DD-MM-YYY')
Also this is giving me the output like
MAX_DATE PROCESS_DATE
2022-10-14T00:00:00.000+00:00 2022-11-08T00:00:00.000+00:00
How to compare both the dates ?
If your columns have a TIMESTAMP WITH TIME ZONE data type then just compare the columns directly:
SELECT *
FROM ANC
WHERE MAX_DATE > PROCESS_DATE
If your columns have the VARCHAR2 data type then, firstly, that is bad practice and you should alter the table and change the data type to TIMESTAMP WITH TIME ZONE, and secondly, if you are going to keep them as strings then you need to convert them to timestamps using the TO_TIMESTAMP_TZ function before comparing them:
SELECT *
FROM ANC
WHERE TO_TIMESTAMP_TZ(MAX_DATE, 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
> TO_TIMESTAMP_TZ(PROCESS_DATE, 'YYYY-MM-DD"T"HH24:MI:SS.FFTZH:TZM')
please try with below query
where
convert(datetime, MAX_DATE) >= convert(datetime,PROCESS_DATE)

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

Select Varchar as Date

I want to select a varchar field as a date field
For example a field has this value "30.12.2011 21:15:03"
and when i select this
select DATE from TABLE where DATE = '30.12.2011'
i get no result.
You ask about getting the date part of a timestamp field, but what your question is actually about is filtering on the date of a timestamp field. There is a much simpler method of accomplishing that: you can use the knowledge that all the possible timestamps on a specific date won't have any timestamps for different dates between them.
select DATE
from TABLE
where DATE >= '30.12.2011' and DATE < '31.12.2011'
Your edit explains that you haven't got a timestamp field at all. Nevertheless, a similar approach may still work:
select DATE
from TABLE
where DATE LIKE '30.12.2011 %'
Or the Firebird-specific
select DATE
from TABLE
where DATE starting with '30.12.2011 '
Assuming the field is a date field, use the DATE introducer combined with yyyy-mm-dd (or TIMESTAMP with time as well).
So use:
select datefield from sometable where datefield = DATE '2011-12-30'
Technically you can leave off the introducer, but it is 'correcter' in the light of the SQL standard.
Assuming a TIMESTAMP field, you won't get results unless the timestamp is (always) at 00:00:00.0000 (in which case it should have been a DATE instead).
For the comparison to work, you need to use either BETWEEN, eg:
select timestampfield from sometable
where timestampfield BETWEEN '2011-12-30 00:00:00.0000' AND '2011-12-30 23:59:59.9999'
or truncate the timestamp to a date (this may adversely effect performance if the timestamp is indexed, because then the index can no longer be used), eg:
select timestampfield from sometable
where CAST(timestampfield AS DATE) = '2011-12-30'
If the date is stored in a VARCHAR field (which in itself is a bad idea), there are several solutions, first is to handle it as date manipulation:
select varcharfield from sometable
where CAST(CAST(varcharfield AS TIMESTAMP) AS DATE) = '2011-12-30'
The double cast is required if you have a time-component in VARCHARFIELD as well. This assumes dates in the supported format listed below. If you use BETWEEN as above, you can use a single cast to timestamp)
The other solution (as suggested by hvd) is to treat it purely as string manipulation, for example:
select varcharfield from sometable
where varcharfield STARTING WITH '30.12.2011'
This has its own set of problems if you want to select ranges. Bottomline: use a real TIMESTAMP field!
Note that Firebird supports multiple formats:
yyyy-mm-dd, eg 2014-05-25 (ISO-8601 format, probably best to use as it reduces confusion)
dd.mm.yyyy, eg 25.05.2014
mm/dd/yyyy, eg 05/25/2014
mm-dd-yyyy, eg 05-25-2014
dd mmm yyyy, eg 25 MAY 2014 (+ variations with a -, . or / as separator)
mmm dd yyyy, eg MAY 25 2014 (+ variations with a -, . or / as separator)
select DATE from TABLE where cast(DATE as date) = '30.12.2011'
Date field is a timestamp
Here is the answere to my question:
CAST
(
SUBSTRING
(field FROM 1 FOR 2)
||'.'||
SUBSTRING
(field FROM 4 FOR 2)
||'.'||
SUBSTRING
(field FFROM 7 FOR 4)
AS DATE)
This took me 5 hours to find this out, maybe there should be a "-" instead of "." but it works.

oracle sql date comparison doesn't work as expected

I have a table X with 'insert_date' column. This column is od type DATE and contains only one value for all records: "17-JAN-13". I would expect that following query return no results at all:
SELECT insert_date
FROM X
WHERE ("X"."INSERT_DATE" IS NOT NULL
AND NOT (("X"."INSERT_DATE" = to_date('2013-01-17', 'yyyy-mm-dd')
)))
But what I'm getting instead is many "17-JAN-13" records.
What's wrong with my query?
Oracle DATE columns contain a time as well (despite their name). Your existing rows probably have a time different than 00:00:00 (which is "assigned" to the date you create with the to_date() function).
You need to "remove" the time part of the column using trunc()
AND NOT (trunc(X.INSERT_DATE) = to_date('2013-01-17', 'yyyy-mm-dd'))
although I'd prefer to use <> instead of the NOT operator:
AND (trunc(X.INSERT_DATE) <> to_date('2013-01-17', 'yyyy-mm-dd'))
(but that is just a personal preference. I think it makes the condition easier to read).
So your complete statement would be:
SELECT insert_date
FROM X
WHERE trunc(X.INSERT_DATE) <> to_date('2013-01-17', 'yyyy-mm-dd')
You can either trunc the time part while comparing or else extract only the required part i.e DD-MON-YYYY like
SELECT insert_date FROM X
WHERE X.INSERT_DATE IS NOT NULL and to_char(x.INSERT_DATE,'DD-MON-YYYY') <> '17-JAN-2013';