Access data from database between two dates using sql - sql

I want to access data from database between two dates, Date format is yyyy/mm/dd. I tried a lot of queries but not succeed.

Try following query to get data between the range:
SELECT *
FROM Table_Name
WHERE From_date >= '2016-08-01' AND
To_date <= '2016-08-30'

IF you are using just vanilla sql it would just be something alone the lines of
select x from table where datecolumn between 'xdate' and 'ydate'.
Do you have a date column in the table you are looking at correct?

Related

How to create a function that can operate with the current date and a value from my table in sql

I have a Clients table with the inscriptiondate which is a date. I want to create a function that I give the value inscriptiondate(from my table) and do the value-currdate(). Is it possible?
I'm using SQL server, But I would also like to know how to do in Mysql if its different.
In MySQL you would typically do:
select datediff(curdate(), inscriptiondate) as days_between
In MS SQL, you would typically do:
select datediff(day, inscriptiondate, getdate())

ORACLE SQL create date from table colum parts

I'm trying to create a select statement that returns one row (date) combining 3 column entries
My idea that doesn't work:
SELECT
TO_DATE( d_date.day_varchar +
d_date.month_varchar +
d_date.year_varchar , 'DD/MM/YYYY')
FROM d_date
Is this possible in Oracle SQL and how can I get this result?
In SQL Server there is a DATEFROMPARTS() function but I don't know that well Oracle specifics ...
Your approach seems fine. You just need to update it for Oracle syntax:
select to_date(d_date.year_varchar || d_date.month_varchar || d_date.day_varchar, 'YYYYMMDD')
It is a curious that you would have a table with date parts, but not the actual dates.

Date functions in temporal tables

Since SQL Server 2016 it is possible to automatically create Temporal Tables. I wanted to create a simple query that retrieves the data from a specified date. However, when I try to specify a date in the query like so, it gives a syntax error:
SELECT * FROM Person FOR SYSTEM_TIME AS OF GETDATE()
I even tried to convert the datatype to a datetime2, since the dates a stored like that, but it still wouldn't work:
SELECT * FROM Person FOR SYSTEM_TIME AS OF CONVERT(datetime2,GETDATE())
This problem occurrs, but when I first execute SELECT GETDATE() and then copy the text and paste it into the query, it works fine.
How can I specify a datetime with the AS OF keyword?
Thanks in advance.
Try this:
DECLARE #Date DATETIME2 = GETUTCDATE()
SELECT *
FROM Person
FOR SYSTEM_TIME AS OF #Date
Also, take a look at this article for more examples of querying temporal tables.

How to compare dates or filter data based on Date in sybase?

I want to fetch data for a particular date, datatype for this column is DateTime.
Tried below query:-
SELECT * from table_name where transaction_date=convert(DATETIME,'02/21/2017',101)
But above query is not working and returning no result, please could someone point out what I am doing wrong.
If you need compare dates with day precision you can use datediff function as below.
SELECT *
from table_name
where datediff(dd,transaction_date,convert(DATETIME,'02/21/2017',101))=0
More information about DateDiff

How to fetch data between two dates in oracle database without date field in the table

I want to get the data inserted in the last week. But there is no date/time field in the table. Is there any way to fetch the data.
Can sysdate or timestamp method help for fetching the data.
Thank you
you can try to analyze result of the query below:
select *
from v$sql vs join v$session vses
on vs.sql_id=vses.sql_id
where vses.last_load_time < vses.date_1 and last_load_date < date_2
and upper(sql_fulltext) like 'INSERT INTO%TABLE_NAME' or 'upper(sql_fulltext) like 'UPDATE%TABLE_NAME';