Date Between (Start & Now) - sql

Not sure how to use the NOW() function in presto. Seems like it should be straight forward, but i'm getting no luck
SELECT DISTINCT field
FROM table
WHERE field BETWEEN '2019-01-01' and NOW()
field = varchar

You should match data types to avoid implicit conversions:
SELECT DISTINCT field
FROM table
WHERE CAST(field AS DATE) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;
SELECT DISTINCT field
FROM table
WHERE DATE(field) BETWEEN DATE '2019-01-01' AND CURRENT_DATE;

Related

SQL Server - sort timestamp date field in sub query then format for viewing

I have a query (SQL Server 2012) that combines UNIX timestamps from two different tables into one column, which on its own works fine. The query needs to be sorted (descending) by the combined date column AND converted into a readable format.
If I attempt to sort after the date has been converted, it doesn't work as it is now a VARCHAR and returns:
05/02/2018
06/01/2017
07/03/2016
First I tried including a date conversion in the ORDER BY clause but that clearly doesn't work.
I then thought I could use a subquery to perform the sort then do the conversion in the main query (see below for what I currently have), but this is returning the error:
Invalid usage of the option NEXT in the FETCH statement.
I'm not using a fetch statement, so I'm wondering if there's something in my nesting that SQL Server doesn't like.
Any clues would be terrific.
-- Convert Epoch timestamp format to readable (and unsortable) dd/MM/yyyy format
SELECT CONVERT(VARCHAR(10), DATEADD(second, OuterTable.thedate-DATEDIFF(second, GETDATE(), GETUTCDATE()), CONVERT(DATETIME, '1970-01-01', 103)), 103) AS FinalDate
FROM (
-- Subquery only exists to do a proper date sort
SELECT thedate
FROM
(
SELECT
-- Determine which date to use
CASE WHEN x.dateX IS NOT NULL
THEN x.dateX
ELSE y.dateY
END AS thedate
-- Get date from first table
FROM (
SELECT id, dateX -- date is in Epoch format (BIGINT)
FROM tableX
) AS x
-- Get date from second table
JOIN (
SELECT id, dateY -- date is in Epoch format (BIGINT)
FROM tableY
) AS y
ON x.id = y.id
)
-- Perform sort while date is still in epoch format
ORDER BY thedate DESC
) AS OuterTable
IMO this should work for you:
SELECT CONVERT(VARCHAR(10), DATEADD(second, COALESCE(x.dateX, y.dateY) - DATEDIFF(second, GETDATE(), GETUTCDATE()), CONVERT(DATETIME, '1970-01-01', 103)), 103) AS FinalDate
FROM tableX AS x
INNER JOIN tableY AS y
ON x.id = y.id
ORDER BY COALESCE(x.dateX, y.dateY) DESC;
Not sure when copy/paste the CONVERT function. Is it so complicated to convert epoch to dd/MM/yyyy in MSSQL?

How to search by to_date function?

I have table as below:
Table Temp:
ID MAX MIN DATE_C
1 34 24 21-APR-17 02.41.38.520000 PM
2 32 26 20-APR-17 02.42.44.569000 PM
I execute the below SQL query to get temperature details on respective date:
SELECT *
FROM Temp t
WHERE t.date_c = TO_DATE( '2017-04-21', 'YYYY-MM-DD')
order by t.id
But it's returning empty records. Whats wrong with my query?
You need to remove the time component on the column. Here is one way:
SELECT *
FROM Temp t
WHERE TRUNC(t.date_c) = DATE '2017-04-21'
ORDER BY t.id;
However, I usually recommend using inequalities, rather than a function on the column:
SELECT *
FROM Temp t
WHERE t.date_c >= DATE '2017-04-21' AND
t.date_c < DATE '2017-04-22'
ORDER BY t.id;
This allows the query to use an index on date_c. I should add that the original version can use an index on (trunc(date_c, id).
21-APR-17 02.41.38.520000 PM is not a DATE; it has a fractional seconds component so it is a TIMESTAMP.
So, if you want to find items that are on a particular day (inputting the TIMESTAMP using an ISO/ANSI timestamp literal):
SELECT *
FROM Temp
WHERE date_c >= TIMESTAMP '2017-04-21 00:00:00' AND
date_c < TIMESTAMP '2017-04-21 00:00:00' + INTERVAL '1' DAY;
or
SELECT *
FROM Temp
WHERE date_c >= TO_TIMESTAMP( :your_date_string, 'YYYY-MM-DD' ) AND
date_c < TO_TIMESTAMP( :your_date_string, 'YYYY-MM-DD' ) + INTERVAL '1' DAY;
it's returning empty records. Whats wrong with my query?
date_c = TO_DATE( '2017-04-21', 'YYYY-MM-DD') matches all rows where the date_c value is exactly 2017-04-21 00:00:00.000000 (including the time component); if you do not have any rows with exactly that date and time then, as you noticed, it will return nothing. If you want to get records matching that day then you need to get values within a range of times between the start and end of the day.
You need to pass date on the column. Here is a way...
SELECT *
FROM Temp t
WHERE CAST(t.CREATED_ON as date)= N'2017-04-22'
ORDER BY t.id

How to categorize data for each date in sql?

I am using Oracle sql developer.And I have to categorize some data into each Date from a table.
I used TO_CHAR function to change the date format.
select to_char(txn_date, 'dd-mon-rrrr') my_day, some_coloums
from mwt_wallet_transactions
order by my_day;
It's ok with upper one. But I want to get it with given date like this:
select to_char( txn_date, 'dd-mon-rrrr') my_day, some_coloums
from mwt_wallet_transactions
where my_day like '08-feb-2015'
order by my_day;
I searched this over the internet, most of them asked to use the CONVERT function like this:
select convert (VARCHAR(10), txn_date , 23 ) my_day, some_colomns
from mwt_wallet_transactions
where my_day like '08-feb-2015'
order by my_day;
But it doesn't work for me. Help me to solve this!
where my_day like '08-feb-2015'
I think you got confused between SELECTION and FILTER PREDICATE.
Also, remember, ''08-feb-2015' is NOT a DATE, it is a string.
You want to filter the rows based on a DATE value. So, convert the literal on the R.H.S. into DATE using TO_DATE or use ANSI Date literal if you don't have a time portion.
Now, remember, a DATE has both date and time elements, so you need to -
either use TRUNC on the date column to get rid off the time element
or, use a DATE range condition to for better performance as it would use any regular index on the date column.
I am assuming my_day as the date column. Modify the filter as:
Using ANSI Date literal: fixed format 'YYYY-MM-DD'
where my_day >= DATE '2015-02-08' and my_day < DATE '2015-02-09'
Or, TO_DATE with proper format model. Remember, TO_DATE is NLS dependent, so I have used NLS_DATE_LANGUAGE to make it NLS independent.
WHERE my_day >= TO_DATE('08-feb-2015','dd-mon-yyyy','NLS_DATE_LANGUAGE=american')
AND my_day < TO_DATE('09-feb-2015','dd-mon-yyyy','NLS_DATE_LANGUAGE=american')
Above. my_day is assumed as the static date column, and not the column alias.
You can't use select list column aliases in the WHERE clause. Use derived table to access it:
select * from
(
select *some_computed_value* as my_day, some_colomns
from mwt_wallet_transaction
)
where *my_day conditions*
order by my_day
My solution:
I checked this solutions and it's working well,
select * from
(select cast( TRUNC(txn_date) as date) my_day ,some_columns
from mwt_wallet_transactions
)
where my_day = '08-FEB-15'
order by my_day
;
or we can use:
select * from
(select to_char( txn_date, 'dd-mon-rrrr') my_day
from mwt_wallet_transactions
)
where my_day = '08-feb-2015'
order by my_day;

Select rows where day is less than today

How do I select rows in the past starting from yesterday in Oracle DB where a field like created_date is a timestamp(6)?
I don't want to compare time, just date.
If you want exactly one day prior to the current time:
select *
from table t
where created_date < sysdate - 1;
If you want times before today:
select *
from table t
where created_date <= trunc(sysdate);
From the Oracle documentation on SELECT :
SELECT * FROM orders
WHERE created_date < TO_DATE('2014-04-28', 'YYYY-MM-DD');
I can pass this date format from my application, worked like a charm.
As you want to compare just date:
select *
from table t
where date(created_date) < DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY);
you can use cast function to deal with timestamp as date:
SELECT cast(SYSTIMESTAMP(6) as date)
FROM dual;
so you can select rows with "yesterdate" date by:
select ....
where cast(SYSTIMESTAMP(6) as date) like sysdate - 1
note: replace SYSTIMESTAMP(6) with column name which has timestamp type.

Postgresql - select something where date = "01/01/11"

I have a datetime field in my Postgresql, named "dt".
I'd like to do something like
SELECT * FROM myTable WHERE extract (date from dt) = '01/01/11'
What is the right syntax to do that?
Thanks!
I think you want to cast your dt to a date and fix the format of your date literal:
SELECT *
FROM table
WHERE dt::date = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
Or the standard version:
SELECT *
FROM table
WHERE CAST(dt AS DATE) = '2011-01-01' -- This should be ISO-8601 format, YYYY-MM-DD
The extract function doesn't understand "date" and it returns a number.
With PostgreSQL there are a number of date/time functions available, see here.
In your example, you could use:
SELECT * FROM myTable WHERE date_trunc('day', dt) = 'YYYY-MM-DD';
If you are running this query regularly, it is possible to create an index using the date_trunc function as well:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt) );
One advantage of this is there is some more flexibility with timezones if required, for example:
CREATE INDEX date_trunc_dt_idx ON myTable ( date_trunc('day', dt at time zone 'Australia/Sydney') );
SELECT * FROM myTable WHERE date_trunc('day', dt at time zone 'Australia/Sydney') = 'YYYY-MM-DD';