How to run the postgres query with date as input on the column with timestamp in long format - postgresql

I want to query postgres database table which has the column with timestamp in long milliseconds. But I have the time in date format "yyyy-MM-dd HH:mm:ssZ" like this. How can I convert this date format to long milliseconds to run the query?

You can either convert your long value to a proper timestamp:
select *
from the_table
where to_timestamp(the_millisecond_column / 1000) = timestamp '2020-10-05 07:42'
Or extract the seconds from the timestamp value :
select *
from the_table
where the_millisecond_column = extract(epoch from timestamp '2020-10-05 07:42') * 1000
The better solution is however to convert that column to a proper timestamp column to avoid the constant conversion between (milliseconds) and proper timestamp values

Related

Unix timestamp granularity changed to hours instead of milliseconds

I have a Spark data frame with the column timestamp. I need to create event_hour in unix_timestamp format out of this column. The current issue is that the timestamp is in unix_timestamp format with a granularity of milliseconds while I need the granularity of hours.
Current values for timestamp:
1653192037
1653192026
1653192025
1653192024
1653192023
1653192022
Expected values:
1653192000
1653195600
1653199200
1653202800
How can I achieve that using Spark functions?
I've already tried to convert it to timestamp and then format it but I got null as the result:
inputDf
.withColumn("event_hour", unix_timestamp(date_format($"timestamp".cast(TimestampType), "MM-dd-yyyy HH")))
A (not very explicit but) efficient way would be to use modulus operation with 3600 (as 3600 seconds = 1 hour):
timestamp_hour = timestamp_second - (timestamp_second % 3600)
This assumes you are manipulating data as numeric.
You can use DateUtils API,
import org.apache.commons.lang3.time.DateUtils;
Long epochTimestamp_hour = DateUtils.truncate(Timestamp_column, Calendar.HOUR)).getTime();
create new column of type timestamp
use that column to truncate timestamp to epochTimestamp_hour

Convert DATE to UTCTicks for sql query date input

I have created ssrs report where user can input date start and end parameter in this format yyyy-mm-dd as they are not aware of UTC Ticks. but the query that runs in the back to pull data requires UTC ticks to be inputted . Here is query and how can i modify this query to input so converts user input date into UTC ticks before it runs?
With all due respect to HABO as shown in Convert DATE to UTCTicks for sql query
declare #TicksPerDay as BigInt = 24 * 60 * 60 * Cast( 10000000 as BigInt );
select DateDiff( day, '00010101', '2020-03-02' ) * #TicksPerDay as TodayInUTCTicks;

Extract Only Date from timestamp in postgres

I have a 'timestamp without time zone' value in a column example:
PunchIn = "2019-06-10 09:10:00"
How can I extract "2019-06-10 00:00:00" from this column?
demo:db<>fiddle
SELECT my_date::date::timestamp
This converts the date column my_date to date (cuts the time part) and if you cast it back into timestamp it gets the 0 time.
Alternatively you can use the date_trunc function:
SELECT date_trunc('day', my_date)
SELECT SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21);
should return the string you're looking for (if I count the index number right:)
You can use the DATE function to extract date:
SELECT DATE(SUBSTRING("PunchIn = '2019-06-10 09:10:00'" FROM 11 FOR 21));

Date filteration in Postgre SQL

I am looking to get the row which has the date is equal to 2017-01-12. The date has field type timestamp without time zone. I tried
SELECT * FROM tabl WHERE date = 2017-01-12;
What is the correct syntax?
try:
SELECT * FROM tabl WHERE date = '2017-01-12';
If that doesn't work, try an explicit cast:
SELECT *
FROM tabl
WHERE date = DATE '2017-01-12';

sqlite date comparison

I have column of type date time and values are getting stored in format 10-29-2011 08:25.
I would like to find out the rows only which are less then current date-time. What will be the condition for date comparison for current date and this date-time column field?
Thanks.
you could use the datetime function
SELECT * FROM mytable
WHERE mydate > datetime('now')
you can even make date operations
SELECT * FROM mytable
WHERE mydate > datetime('now','-15 days')