current_timestamp redshift - sql

When I select current_timestamp from redshift, I am getting a list of values instead of one value.
Here is my SQL query
select current_timestamp
from stg.table;
Does anybody know why I am getting a list of values instead a single value?

This is your query:
select current_timestamp from stg.table
This produces as many rows as there are in table stg.table (since that's the from clause), with a single column that always contains the current date/time on each row. On the other hand, if the table contains no row, the query returns no rows.
If you want just one row, use a scalar subquery without a from clause:
select current_timestamp as my_timestamp

You will receive a row for each row in stg.table. According to the RedShift docs you should be using GETDATE() or SYSDATE() instead. Perhaps you want, e.g.:
select GETDATE() as my_timestamp

Related

AWS Athena query to find Date column has String or not satisfying timestamp format

I was testing Sprak 3 upgrades in AWS Athena and need to check date columns whether timestamp format is proper or not,Can any one please give me query to check whether date columns has any Values other than Timestamp format
Assuming that you have a varchar column you can try using date_parse wrapped in try:
select *
from table
where try(date_parse(string_column, 'your_expected_format')) is null -- assuming no original nulls in column
Or via try_cast for "standard" format:
select *
from table
where try_cast(string_column as timestamp) -- assuming no original nulls in column

How to split the column with a Timestamp and extract the hours, minutes and seconds into the separate column?

The timestamp in a column looks like this "2020-04-26 17:45:14".
Now I need to extract and insert the "17:45:14" part into another separate column.
How do I do that? I was thinking about using the following code sequence:
SELECT
EXTRACT(____ FROM column)
FROM table
Just cast it to a time value:
select the_column::time as time_only
from the_table
Or compliant with the SQL standard:
select cast(the_column as time) as time_only
from the_table

Postgres greater than or null

I am trying to efficiently use an index for the greater than or null query.
Example of a query would be:
select * from table where date > '2020-01-01' or date is null
What index if any do I need that postgres will be able to do this efficiently. I tried doing an index with
create index date on table (date asc nulls last)
but it does not seem to work and the best I am able to get is two bitmaps scans (one for greater than and one for null).
If you are able to rewrite your condition, you could replace the null value with a date that is guaranteed to be greater than the comparison value:
where coalesce(date, 'infinity') > date '2020-01-01'
Then create an index on that expression:
create index on the_table ( (coalesce(date, 'infinity')) )
See also PostgreSQL docs:
Date/Time Types, 8.5.1.4. Special Values for infinity value
Conditional Expressions, 9.18.2. COALESCE for coalesce function
Does Postgres use the index correctly when you use union all?
select *
from table
where date > '2020-01-01'
union all
select *
from table
where date is null;
The issue might be the inequality combined with the NULL comparison. If this is some sort of "end date", then you might consider using some far out future value such as 9999-01-01 or infinity.

Using an UDF to query a table in Hive

I have the following UDF available on Hive to convert a time bigint to date,
to_date(from_utc_timestamp(from_unixtime(cast(listed_time/1000 AS bigint)),'PST'))
I want to use this UDF to query a table on a specific date. Something like,
SELECT * FROM <table_name>
WHERE date = '2020-03-01'
ORDER BY <something>
LIMIT 10
I would suggest to change the logic: avoid applying the function to the column being filtered, because it is an inefficient approach. The function needs to be invoked for every row, which prevents the query from benefiting an index.
On the other hand, you can simply convert the input date to a unix timestamp (possibly with an UDF). This should look like;
SELECT * FROM <table_name>
WHERE date = to_utc_timestamp('2020-03-01', 'PST') * 1000
ORDER BY <something>
LIMIT 10

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