Laravel, query where created_at format - sql

In PostgreSQL and Laravel 5.1
I have this output query from Query Builder (raw):
select *
from mytable
// some joins and stuff
where to_char(mytable.created_at, 'DD-MM-YYYY') = '20-06-2019'
I'm getting 0 values because that last line where to_char(mytable.created_at, 'DD-MM-YYYY') = '2019-06-20' is wrong.
created_at has this format: '2019-06-20 21:00:12'
How can I query with that time '20-06-2019' in my table?

You can convert your char date into DATE and truncate date which you are getting from table can use below query
select *
from mytable
// some joins and stuff
where trunc(mytable.created_at) = to_date('20-06-2019','dd-mm-yyyy')

Related

Unable to apply "where" clause to a date column in BigQuery

I have a date column (in YYYY-MM-DD format) in a big query table. I am unable to apply a where clause to the date column. I am using the following queries:
SELECT * FROM [dataSet_Id.TableName] where CR_DT=DATE("2016-01-01")
SELECT * FROM [dataSet_Id.TableName] where CR_DT=DATE("2016-01-01") where CR_DT=20160101
So how do I do it?
I got it work, If I use Standard SQL Dialect instead of Legacy SQL
Sample queries to handle date in where clause:
SELECT * from demoschema.demotable where dob = date('2016-08-10');
SELECT * from demoschema.demotable where dob = '2016-08-11';
If you want to use Standard SQL Dialect, just go to Show options then you will find SQL Version field which is use for enabling Standard SQL. .Dialect.
If the type of your CR_DT column is String then:
SELECT * FROM [dataSet_Id.TableName] where CR_DT = '2016-01-01'
If the type of your CR_DT column is TIMESTAMP then:
SELECT * FROM [dataSet_Id.TableName] where DATE(CR_DT) = DATE(timestamp('2016-01-01'))

Query using date datatype in Oracle 11g

I'm trying to do simple select and Update in Oracle 11g using a date where clause and I can't produce the result I expect.
The date field is datemodified and currently has values like 12-JUL-14 and of data type date.
I'm doing the following and don't get result:
select *
from table
where datemodified = to_date(datemodified, '12-JUL-14')
I tried the following and still did not produce result:
select * from table
where to_date(datemodified, 'DD-MON-YY') = to_date('12-JUL-15',
'DD-MON-YY')
or
Update table
set column = 'some value'
where datemodified = to_date('12-JUL-15', 'DD-MON-YY')
What am I doing wrong?
You can try this query
select * from table
where to_char(datemodified, 'dd-MON-yy') = '12-JUL-15'
Oracle date fields include the time of day. Also, filtering on function results such as to_char() slows down production. This method works:
where dateModified >= the date you want
and dateModified < the day after the date you want.
It's ok to use functions for the values, this for example
where dateModified >= trunc(sysdate)
or
where dateModified >= to_date('12-JUL-15','DD-MON-YY')
but not on the field itself

Postgresql querying timestamp data with date only

I have this data.
this is my query
SELECT transaction_date
FROM
tenant1.txn_transaction_record where '2015-04-14'
The said query renders empty result. Is it possible to query timestamp field using only date?
When you do:
transaction_date = '2015-04-14'
PG will convert string '2015-04-14' to timestamp '2015-04-14 00:00:00' value.
If you do:
transaction_date::date = '2015-04-14'
PG will convert both values to date (wich is only date part, without time part), and it'll work.
BUT... BE CAREFUL WHEN CASTING COLUMNS IN WHERE CLAUSE, because PG will not be able to take advantage of an index that contains that column, unless you've created the index with same cast on the column.
If you create only this index:
create index i_foo_1 on foo ( timestamp_field );
This query WILL NOT use that index:
select *
from foo
where timestamp_field::date = '2015-04-15';
So, or you'll need to create an aditional index:
create index i_foo_2 on foo ( timestamp_field::date );
Or you'll have to change your original "where clause":
select *
from foo
where timestamp_field >= ('2015-04-15'::timestamp)
and timestamp_field < (('2015-04-15'::date)+1)::timestamp;
No, If you do like transaction_date = '2015-04-14' It will automatically search for transaction_date = '2015-04-14T00:00:00' So you wont yield any result. Therefore if you want to search the date try transaction_date::date = '2015-04-14'
So the final query is,
SELECT transaction_date
FROM
tenant1.txn_transaction_record where transaction_date::date = '2015-04-14'
SELECT transaction_date
FROM
tenant1.txn_transaction_record where date_trunc('day', transaction_date) = '2015-04-14 00:00:00'
I don't have a postgres database up to try it :-)

SQL Query taking between specific sysdate?

I am using Oracle DB 10g, I am trying to take data between 2 dates, in the database format of data is :
10/4/2013 9:04:38 AM
I try some sql queries but it gives error...
select * from test_table where test_execution_date between '9/2/2012' and '7/2/2013'
select * from test_table where test_execution_date between '9/2/2012 9:04:38 AM' and '7/2/2013 9:04:38 AM'
It gives always same error : ORA-01843: not a valid month
Try using
TO_DATE(thedatevalue,'MM/DD/YYYY')
You need to convert string to date
select * from test_table where test_execution_date between
TO_DATE('9/2/2012', 'DD/MM/YYYY') and TO_DATE('7/2/2013', 'DD/MM/YYYY')

Optimize SQL Query on calculated value?

I have a query that requires a where statement on a calculated value:
select * from table where date( timestamp ) = ?
An explain on this query yields the expected ALL select type, which is not ideal. Using MySQL, what's the best way to optimize this?
Another option might be to rewrite the query such that the calculations are all done on the other side of the equation. For example:
timestamp >= <some date> AND timestamp < <some date + 1>
In this query, "some date" would be midnight of that date.
select * from table where timestamp = UNIX_TIMESTAMP('?');