BigQuery: select rows where column value contains string - sql

I would like to know how to know how to filter a table by a specific column, when this column contains a specific subtring.
Here's an example of my table:
I would like to obtain those rows where the column tsBegin contains 2020-08-04, maybe with something like:
SELECT * FROM mytable
where '2020-08-04' in tsBegin

Date operations:
where date(tsBegin) = date '2020-08-04'
A column named tsBegin should not be a string column, so you just want the date.
If tsBegin is a string, I would suggest that you convert it to a timestamp.

Use date functions, and half-open intervals:
select *
from mytable
where tsbegin >= date '2020-08-04'
and tsbegin < date_add(date '2020-08-04', interval 1 day)
Although a bit lenghtier to type, direct filtering against literal values is usually much faster than applying a date function on the column being filtered, as in where date(tsbegin) = date '2020-08-04'

Related

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)

I can't filter by date in SQLite

I cannot filter by date on SQL, did I do wrong syntax?
But if I use >= operator, it did the job. When I wanted to find the id that was created_at at a specific date, in this case, is '2021-03-01', nothing appears.
The expected output is Id that created at that specific date.
Preview of the data
and here is my syntax
select
id
from job_post
where
created_at = '2021-03-01';
Created_at is of datetime type when you are comparing this column with a date value it's returning false.
You need to cast datetime to date format to compare with = operator.
Instead of Created_at use date(Created_at). It will only select the date part of the value which you will be able to compare with any date value with = operator.
select
id
from job_post
where
date(created_at) = '2021-03-01';
As #forpas pointed out above query will not work since the datetime format is not ok. You can first extract the first 10 character from that string then convert it into date type.
select
id
from job_post
where
date(substr(created_at,1,10)) = '2021-03-01';

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)

SELECT matching dates from a timestamp with time zone column

I have a timestamp with time zone column within which I'd like to run a query returning all matching dates. eg. I want all rows which have a timestamp with date 2019-09-30. I'm trying something like this but haven't been able to figure it out:
SELECT * FROM table WHERE
x='1277' AND
date='2019-09-30 21:40:01.316240 +00:00'::DATE;
There are two options:
range search:
WHERE timestampcol >= TIMESTAMPTZ '2019-09-30'
AND timestampcol < (TIMESTAMPTZ '2019-09-30' + INTERVAL '1 day')
The proper index to make this fast is
CREATE INDEX ON atable (timestampcol);
conversion to date:
WHERE CAST(timestampcol AS date) = '2019-09-30'
The proper index to make this fast is
CREATE INDEX ON atable ((CAST(timestampcol AS date)));
Both methods work equally well. The second method has a shorter WHERE clause, but a specialized index that maybe no other query can benefit from.
You can use such a collation among your date converted column value and fixed date value :
with tab( x, date ) as
(
select 1277, '2019-09-30 21:40:01.316240 +00:00'::timestamp with time zone union all
select 1278, '2019-09-29 21:40:01.316240 +00:00'::timestamp with time zone
)
select *
from tab
where date::date = date'2019-09-30';
Demo
I am not familiar with postgresql, but the following page https://www.postgresql.org/docs/9.1/datatype-datetime.html indicates that there is an optional precision value p that is used to specify the number of fractional digits retained in the seconds field. How was the data type configured for your date field? Is the timestamp column being stored as an eight byte integer or a floating-point number? If the later, the effective limit of precision might be less than 6. All of this info is on the above linked page.
You have several options, as they have been mentioned, and may vary depending on the data type of your field named "date".
For example,
Turn your field to date, returning format 'yyyy-mm-dd':
SELECT * FROM table WHERE x='1277' AND date::DATE='2019-09-30';
"date" just after field name.
Convert it to char and retrieve 10 characters:
SELECT * FROM table WHERE x='1277' AND LEFT(date::varchar,10)='2019-09-30';
Like the previous:
SELECT * FROM table WHERE x='1277' AND to_char(date,'yyyymmdd')='20190930';
And there are many others. For more specific info, you have to check PostgreSQL documentation to check which one is best for you or post more information in order we can guess more about your problem.
https://www.postgresql.org/docs/9.4/functions-datetime.html

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.