SQL Select Where date record created - sql

Is there a way in SQL to filter a query based on the date a specific record was added if I don't have a field that has the date it was created on.
Something like this:
Select * from Table where Table.recorddate = '2013-01-01'

I think that what you are trying to achieve is not possible. If it's really necessary, just add the field. It'd be easier for you that way.

Use UTC format:
SELECT * FROM Table WHERE recorddate = '2014-01-01'
Note that UTC is YYYY-MM-DD. So Today's date would be:
SELECT * FROM Table WHERE recorddate = '2014-05-19'
If your date field also includes a time stamp you can use a between:
SELECT * FROM Table
WHERE recorddate BETWEEN '2014-05-19 00:00:00' AND '2014-05-19 23:59:59'

Related

postgresql count all entries created on a particular date?

I want to count all entries created on a particular date in postgresql. The table has a created_date field which of type time stamp without time zone , One of the entries in created_date looks like this 2020-08-18 12:26:22.641.
select count(*) from table where created_date='2020-08-18*'
This is what i try but that does not work.
How can i count is there something like contain or regex match for this scenario??
Thanks!!
The most efficient way is to use a range condition:
select count(*)
from the_table
where created_date >= date '2020-08-18'
and created_date < date '2020-08-19';
Another option is to cast the timestamp to a date value:
select count(*)
from the_table
where created_date::date = date '2020-08-18'

SELECT query with LIKE fails

I'm using a SQL SELECT query to bring back all rows from a specific date.
The column I'm using is called TimeStamp (datetime)
(An example of data from this column = 01/02/2018 07:55:55)
What I would like is to return all rows from a specific date eg 24/06/2019
I have tried
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02/01/2018 07:55:55'
which returns one row (which is correct as there is only one row containing this data)
If I then try
SELECT top 20 TimeStamp
from Report
where TimeStamp LIKE '02/01/2018%'
I get no results, I have also tried escaping the forward slashes
SELECT top 20 TimeStamp
from Report
where TimeStamp = '02\/01\/2018%'
Most databases support a string function called left(). If I assume that your "timestamp" is a string, then:
where left(timestamp, 10) = '01/02/2018'
However, it should be stored as a date or date/time. If so, then you can do:
where timestamp >= '2018-02-01' and
timestamp < '2018-02-02'
Note the use of standard formatted dates (YYYY-MM-DD). That is the way most databases implement date literals.
In SQL Server, you can also use:
where convert(date, timestamp) = '2018-02-01'
Both this and the previous version will use an index on timestamp, so both are reasonable solutions.
this should work
SELECT TimeStamp FROM report where convert(Date, TimeStamp) = '2019-06-24'
or select timestamp from report where timestamp between '2019-06-24' and '2019-06-25'. This will get you everything between 2019-06-24 00:00:00 and 2019-06-25 00:00:00 thus all records with date 2019-06-24
Convert timestamp value to date.
SELECT TimeStamp
FROM report
WHERE CAST(TimeStamp AS DATE) = '2019-06-24'

Result empty when date variable

I'm using an Oracle 10g Database. I have a table with a lot of information. One of the columns has a type of DATE declared as follows DATE_INSERT DATE. I'm trying to make a query and have it filtered by a specific Date. When I use the function TO_CHAR() in the where clause I get the information as expected
SELECT * FROM TABLE WHERE TO_CHAR(DATE_INSERT, 'DD/MM/YYYY') = '05/10/2018'
But when I use a DATE variable or the TO_DATE() function I get an empty result set.
SELECT * FROM TABLE WHERE DATE_INSERT = TO_DATE('05/10/2018','DD/MM/YYYY')
OR
SELECT * FROM TABLE WHERE DATE_INSERT = date '2018-10-05';
OR (date_var defined as a date previously)
SELECT * FROM TABLE WHERE DATE_INSERT = date_var;
By a DB department requirement I need to get rid of all functions TO_DATE() / TO_CHAR(). Can you help me know why is the filtering not working when DATE types are used in the query?
This is due to the time component. I would recommend either:
WHERE DATE_INSERT >= date '2018-10-05' AND
DATE_INSERT < date '2018-10-06'
Or:
WHERE TRUNC(DATE_INSERT) = date '2018-10-05'
Oracle supports function-based indexes. If you want an index to be used, you can create an index to support either of these queries.
In Oracle, a DATE column always stores the date and the time information. So the query you mention:
SELECT * FROM TABLE WHERE TO_CHAR(DATE_INSERT, 'DD/MM/YYYY') = '05/10/2018'
doesn't use equality, but queries for a range. Which range? The whole day, that is 24 hours.
For the other query to you'll need to use a range, as in:
SELECT * FROM TABLE
WHERE DATE_INSERT >= TO_DATE('05/10/2018','DD/MM/YYYY')
AND DATE_INSERT < TO_DATE('06/10/2018','DD/MM/YYYY')
More clearly, to show the date & time info:
SELECT * FROM TABLE
WHERE DATE_INSERT >= TO_DATE('05/10/2018 00:00:00','DD/MM/YYYY HH24:MI:SS')
AND DATE_INSERT < TO_DATE('06/10/2018 00:00:00','DD/MM/YYYY HH24:MI:SS')

SQL query with operator between and date in a strange format

I have a column in my table that stores a date in format (DD-MM-YY HH:MM:SS). For e.g.:
05-06-15 01:02:03
I need to output for instance all the records that have a date between the 4th and the 5th of June, so i tried:
SELECT * from table where date BETWEEN '04-06-15 00:00:00' AND '05-06-15 23:59:59'
But it also output results with a different month, as:
05-07-15 14:52:34
Is there a way to use a single query for solving this issue or I have to change all my database date format?
SELECT *
from table
where
STR_TO_DATE(date,'%d-%m-%Y %T') between
'2015-06-05 00:00:00' and '2015-06-5 23:59:59';

Check only date from datetime field

I have a MySql database that holds datetime in one field, in this format:
'2011-01-04 16:32:49'
How can I filter out results based on this datetime such that it shows results only from Today. basically, I only want to compare against the date part of the field.
Can I do something like this?
Select * from table where timestamp.date() = now().date
Use DATE:
SELECT * FROM table
WHERE DATE(timestamp) = DATE(now())
Another alternative that will be able to use the index on timestamp if you have one:
SELECT * FROM table
WHERE timestamp >= DATE(now())
AND timestamp < DATE(now()) + interval 1 day
Yes you can but the column that stores the dateTime field in your database simply needs to be referenced, you should not be calling a .date() function on it.
For mySQL, this would be:
Select * from table where DATEDIFF(NOW(),timestamp) = 0
This would give you only records in which the Date is today.
Select * from table where DATE_FORMAT(timestamp, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')