Select where date without time - sql

I am using SQLite, now how can I Select a query if I have a timestamp column that has this value for example 07:00 06/03/13 but I want only to select where timestamp 06/03/13.
This is my example of my query..
select
timestamp, pname
from
attendance
where
timestamp between `06/01/13` and `06/10/13`

Use date
select timestamp, pname
from attendance
where date(timestamp) = '2013-03-06'

When filtering on a timestamp column, you want this logic.
where YourField >= TheStartOfYourRange
and YourField < TheDayAfterTheEndOfYourRange
This is equivalent to the two answers that use the date() function, but will generally perform faster. Using functions in the where clause usually slows down production, especially on indexed fields.

SQLite support function date, which can be used to get the date part of a timestamp. For example:
SELECT * FROM mytable WHERE date(timestamp_col) = '2013-05-20'
See SQLite date and time functions documentation for more info.

Related

Oracle: efficiently where clause to filter timestamp column to get all records of a specific day

I have a monthly partitioned table in Oracle by a timestamp column. This table contains > 1 billion rows from a 2019 history data. Now I want to filter this table to get all results for a specific day, regarding the HH24:MI:SS part.
The Problem (#1) I'm facing is that when using TO_CHAR(TIMESTAMPCOLUMN, 'YYYY-MM-DD'), the running time of my queries increases for more recent months. Example:
SELECT * FROM BIG_PART_TABLE WHERE TO_CHAR(TIMESTAMPCOLUMN, 'YYYY-MM-DD') = '2019-01-01' -- 3 sec
SELECT * FROM BIG_PART_TABLE WHERE TO_CHAR(TIMESTAMPCOLUMN, 'YYYY-MM-DD') = '2019-02-01' -- 6 sec
SELECT * FROM BIG_PART_TABLE WHERE TO_CHAR(TIMESTAMPCOLUMN, 'YYYY-MM-DD') = '2019-12-01' -- 36 sec
So I got rid of the TO_CHAR and started to filter like this:
SELECT * FROM BIG_PART_TABLE WHERE TIMESTAMPCOLUMN BETWEEN DATE '2019-01-01' AND DATE '2019-01-02' -- 0.032 sec
SELECT * FROM BIG_PART_TABLE WHERE TIMESTAMPCOLUMN BETWEEN DATE '2019-12-01' AND DATE '2019-12-02' -- 0.031 sec
The Problem (#2) is that I'm to lazy to write a BETWEEN clause, apart from the fact that it increases the chance of errors.
Finally, what I really want is a efficient single where clause to filter my table, like:
SELECT * FROM BIG_PART_TABLE WHERE TIMESTAMPCOLUMN = DATE '2019-01-01'
Thanks for all.
The correct approach is not to use date functions on the date column - using functions like this makes the query non SARGable, meaning that it cannot take advantage of an index on the date column.
There is no syntactical sugar that would make the expression shorter to write.
I would also suggest using half-open intervals instead of between:
WHERE
TIMESTAMPCOLUMN >= DATE '2019-01-01'
AND TIMESTAMPCOLUMN < DATE '2019-01-02'
BETWEEN is inclusive on both ends, so your expression implies that timestamps on 2019-01-02 00:00:00 would be filtered in, while this is most likely not what you want.
Use the partition_extension_clause syntax:
SELECT *
FROM BIG_PART_TABLE PARTITION FOR (DATE '2019-12-01')
WHERE TRUNC(TIMESTAMPCOLUMN) = DATE '2019-12-01' ;
This code is still a bit messy. But at least this syntax allows you to use the same date literal instead of having to create a brand new date expression. And although the code has duplication, the duplication is a bit self-documenting: the first expression is to use partition-pruning to find the nearest segment, the second expression is to get the exact rows.
In order to use the partitioning, Oracle has to recognize the partitioning key. If it is using the full timestamp, then you might have a problem.
There is a reasonable chance it is using trunc(TIMESTAMPCOLUMN) or trunc(TIMESTAMPCOLUMN, 'DD'). If so, then you can use that
WHERE TRUNC(TIMESTAMPCOLUMN) = DATE '2019-01-01'
Once you figure it out, you can add a computed column to the table, so you have:
alter table big_part_table add column timestampcolumn_date as trunc(timestampcolumn);
Then you can use timestampcolumn_date in the WHERE clause.
In the fastest way to in Orace access data is to use the partition-name.
Like in this example:
select * from BIG_PART_TABLE partition(ParititonName);

Is it possible to get the timestamp of a SQL datetime column?

I am using Microsoft SQL Server and I have a table with a datetime column.
When I run a query SELECT END_DATE FROM CUSTOMER I get 2007-12-03 10:15:30.000
I would like to run a query that returns a unix timestamp like 1543357818 representing the date and time. Is this possible through SQL?
Just use datediff():
SELECT DATEDIFF(second, '1970-01-01', END_DATE)
FROM CUSTOMER
As an alternative, convert to float and do a subtract, then convert to seconds. Obviously you can change some of this to constants, but this illustrates what I'm doing:
select floor((convert(float,getdate())-convert(float,convert(datetime,'1970-01-01')))*24*60*60)

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.

how to skip sql timestamp to get records for specific date

I want to compare the date in my sql query and get the records for a specific date, but it is stored in timestamp format in postgres.
What kind of query could compare only date parts, since I want and skip the time part.
This Query will give you date like '2012/11/22' which you can use for date comparison.
SELECT CONVERT(VARCHAR(10),GETDATE(),111)
//OR simply
select CAST(GETDATE() as DATE)
You can see more formats here :- CAST and CONVERT (Transact-SQL)
Try this:
SELECT *
FROM tab
where date_col::DATE BETWEEN '2012-01-01'::DATE AND '2012-01-05'::DATE
SQL Fiddle DEMO

pgsql time diffrence?

How to write a query to find the time difference ?
time format is like this
2009-08-12 02:59:59
i want to compare
this time with
2009-08-12 02:59:10
how to check these two
i want to return some row having the time difference is 30sec
how to write a SQL statement ??
select date_part('second',date1) - date_part('second',date2)
In SQL you can do like this which give you output in seconds
SELECT * FROM your_table WHERE time1_column - time2_column = interval '30s'
Sorry this is the best I can do, given your description of the problem...
if both the times are columns in database table, you can directly use
relational operators (>, < and =)
Ex. if you have a table like
Test
(
id bigint,
starttime timestamp,
endtime timestamp
);
then you can have queries like
select * from test where starttime > end time
etc..
If you want to compare two dates in query, you can first convert text to time and then compare them
you can use: datetime function of pgsql
see: http://www.postgresql.org/docs/6.3/static/c10.htm
for more details