How to group data by distinct date in H2 [duplicate] - sql

Table
Objective
I want to select only those record which matches a specific date (yyyy-MM-dd)
Like : SELECT * FROM WEATHER WHERE CREATED_AT = "2018-11-28"

For your request:
SELECT * FROM WEATHER WHERE FORMATDATETIME(CREATED_AT,'yyyy-MM-dd') = '2018-11-28'
It is a harder to find info about converting timestamp to date without formatting. Sample for finding all tomorrow scheduling tasks:
SELECT * FROM schedule WHERE CAST(date_time AS DATE)=DATEADD(DAY, 1, TODAY);

Related

How can we search the json file based on date field using S3 select

I am trying to query data on JSON file using S3-Select. I am unable to filter based on the date field. I have tried using current_date, sysdate and a few CAST options too. I am planning to compute the curent_date and send it from API. Before that, I wanted to check if there was any other way to get the current date in the query.
Sample Json :
S3 Select Queries I have tried :
select * from S3Object[*].stations[*] as station where station.iataCd = 'NCL' and station.expiryDt >= sysdate();
select * from S3Object[*].stations[*] as station where station.iataCd = 'NCL' and station.expiryDt >= current_date;
select * from S3Object[*].stations[*] as station where station.iataCd = 'NCL' and station.expiryDt >= '2023-01-23'

Custom ORDER BY in SQLite for string date

I have a column "date_time" and many other.
date_time is a string date (because in SQLite there is no date format), e.g. "2020.2.1" means 1st of February of 2020th.
I want to sort by this date 2020.1.1-2020.1.2-...-2020.12.31
I am using:
SELECT *
FROM Table
ORDER BY date_time
And I am getting sorted as a string:
here is an example:
2020.1.10
...
2020.1.29
2020.1.3
...
How I can sort it as a date if I am using SQLite and my date is string?
select *
from Table
order by cast(date_time as datetime)

Finding a Specific Date with SQL

I'm trying to search in an database for records with a specific date. I've tried to search in the following ways:
SELECT *
FROM TABLE_1
WHERE CAL_DATE=01/01/2015
and
SELECT *
FROM TABLE_1
WHERE CAL_DATE='01/01/2015'
I'm working with an Access database, and in the table, the dates are showing in the same format (01/01/2015). Is there something I'm missing in the SQL statement?
Any of the options below should work:
Format the date directly in your query.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=#01/01/2015#;
The DateValue function will convert a string to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateValue('01/01/2015');
The CDate function will convert a value to a date.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=CDate('01/01/2015');
The DateSerial function will return a date given the year, month, and day.
SELECT *
FROM TABLE_1
WHERE CAL_DATE=DateSerial(2015, 1, 1);
See the following page for more information on the above functions: techonthenet.com
Try using CDATE function on your filter:
WHERE CAL_DATE = CDATE('01/01/2015')
This will ensure that your input is of date datatype, not a string.
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..
SELECT * from Table1 WHERE (CDATE(ColumnDate) BETWEEN #03/26/2015# AND #03/19/2015#)
if this works .. vote for it.. we use above query for searching records from 26th march to 19 the march.. change the dates accordingly..

Merging two columns in hive and use between operator

I have a login details table in hive with columns
(DATE, TIME, USER)
I am trying to write a query which can select users who have logged in between two dates with time also taking in consideration. For example: I want to know the users who have logged in between 10-12-2012 02:30:00 and 28-12-2012 16:20:00. Dates in DD-MM-YYYY and Time in HH:MM:SS format.
I am able to execute
select * from test_table where time between "02:30:00" and "16:20:00" ;
select * from test_table where date between "10-12-2012" and "28-12-2012" ;
But i am not getting how to take both date and time columns into consideration while fetching the required result. Please guide me. Thanks in advance
SELECT * FROM test_table
WHERE date BETWEEN "11-12-2012" AND "27-12-2012"
OR (date = '10-12-2012' and time >= '02:30:00')
OR (date = '28-12-2012' and time <= '16:20:00');

sqlite select with condition on date

I have an sqlite table with Date of Birth. I would like to execute a query to select those records where the age is more than 30.
I have tried the following but it doesn't work:
select * from mytable where dob > '1/Jan/1980'
select * from mytable where dob > '1980-01-01'
Some thing like this could be used:
select dateofbirth from customer Where DateofBirth BETWEEN date('1004-01-01') AND date('1980-12-31');
select dateofbirth from customer where date(dateofbirth)>date('1980-12-01');
select * from customer where date(dateofbirth) < date('now','-30 years');
If you are using Sqlite V3.7.12 or greater
Dont use date(dateofbirth) just use dateofbirth. So your query would look like this:
select * from customer where dateofbirth < date('now','-30 years');
Using the magic docs at the sqlite website:
select * from mytable where dob < date('now','-30 years');
Try writing using the date format 'YYYY-MM-DD'
select * from mytable where dob > '1980-01-01'
A more programic way would be something like this:
select * from mytable where datediff(dob, now()) > 30
You'll Need to find specific syntax for sqlite.
select * from mytable where date(dob) > date('1980-01-10')
QLite3 has some cool new date functions. Per the docs site you can use date(), time(), datetime(), julianday(), unixepoch(), or strftime() depending on how your column data is formatted.
If you use strftime(), like my suggestion below, then you have to make sure that your column data is formatted the same way as your strftime string.
You would probably want something like:
SELECT * FROM mytable WHERE dob < strftime("%m/%d/%Y", 'now', '-30 year');
Note that you might have to change the format string here to match your own.
And here's some code that I use personally to give you a better idea of how powerful it is. It lets me get all the orders from the previous 3 months, not including this month.
SELECT * FROM orders WHERE SHIPPEDDATE > strftime('%m/%d/%Y', 'now', 'start of month', '-3 month');
The modifiers are very powerful with sqlite. The first string inside strftime() is the format, the 2nd string is when you want the date to start. 'Start of month' puts the day to 1, and '-3 month' goes back 3 months. So if I ran that today (08/03/2022), the date it uses would be 05/01/2022.