I want to perform addition on date field and int field.
I am executing the below query. It's throwing the error. I am not able to add. In this create_date is DATE data type and duration is INT data type.
SELECT (create_date+duration)
FROM course;
Please help me.
Thanks
Assuming that duration is in days, you can try date_add function.
Function signature
date_add(string create_date, int duration)
Adds a number of days to create_date: date_add('2008-12-31', 1) = '2009-01-01'.
Usage
select
date_add(string create_date, int duration)
from
course;
Reference:
Date functions in Hive
Related
I have a column named 'CreatedAt' in postgres (DBeaver client) that is an int8 datatype and holds a unix timestamp value. Example: 1659347651689
I am writing a query that I'd like to input an ISO datet ime in the where clause and have it automatically convert to find the applicable records.
For example:
Normally, I'd write:
select * from table1 where CreatedAt = '2022-08-01 09:54:11.000'
I can't do that because the CreatedAt column value is 1659347651689. Is there a way to have it automatically convert and locate the record with that datetime?
I tried this:
`select * from table1 where CreatedAt = date("CreatedAt",strtotime('2022-08-01 09:53:27.000'))`
but strtotime doesn't exist (guessing because it's a Python command. I tried date, dateadd, but no luck)
Your data appears to be in milliseconds, so:
select to_timestamp(1659347651689/1000);
Yes thank you Jeroen Mostert and a_horse_with_no_name (great userid). After reading the links here, I got it.
If anyone else is looking, the answer is:
select * from table1 pfs
where timestamp 'epoch' + pfs."CreatedAt" /1000 * interval '1 second' = '2022-08-01 09:53:13.000'
I'm trying to write a query to return records from a column called "businessdate" (which is in the YYYY-MM-DD format, and it is a string data type), using the previous days "businessdate" records and am stuck. I've tried different things, and I get error messages ranging from argument or matching method, etc. It's probably simple, and I feel dumb. Please help!
Query;
Select businessdate from dbname
Where businessdate = current_date - 1
Use date_sub function to subtract one day from current_date:
select businessdate
from dbname.tablename
where businessdate = date_sub(current_date,1)
I want to get sales with date, which started with'2007-02':
SELECT *
FROM payment
WHERE payment_date LIKE '2007%';
But there is the error:
ERROR: ERROR: operator does not exist: timestamp without time zone ~~ unknown
LINE 9: WHERE payment_date LIKE '2007%'
^
HINT: Operator with given name and argument types could not be found. Perhaps you should add explicit casts.
But another query as
SELECT email
FROM customer
WHERE first_name = 'Kelly'
AND last_name LIKE 'K%';
work proper. How can i force this query to work?
You can get my learning database via link: https://dropmefiles.com/LX8cu
I want to get sales with date, which started with '2007-02':
LIKE is a string function. As the error message indicates, the payment_date column is of timestamp datatype, so use date filtering:
where payment_date >= date '2007-02-01' and payment_date < date '2017-03-01'
Or, if you want the whole year:
where payment_date >= date '2007-01-01' and payment_date < date '2018-01-01'
You might be tempted to use a date function to extract the year part of the date, and use it for comparison:
where extract(year from payment_date) = 2007
I would not recommend that; this is far less efficient, because the function needs to be applied to the entire column before the filtering can happen, as opposed to the direct filtering against literals. One says that the expression is not SARGable.
The error message really says it all - the like operator takes strings on both its sides, while you have a timestamp.
An alternative approach could be to extract the year:
SELECT *
FROM payment
WHERE EXTRACT(YEAR FROM payment_date) = 2007;
I assume that your fiel payment_date is of type date, time or timestamp.
Try to convert the date/time into string before using the like operator.
https://www.postgresql.org/docs/13/functions-formatting.html
I have SQL query like this.
select *
from TABLE_A
where LogDateTime >= startdatetime and LogDateTime <= enddatetime;
But some reason enddatetime equal is not working. I have the record with the date 11/23/09 8:50:09. When I select enddatetime as 11/23/09 8:50:09 it's not returning this record. It's returning till 8.49:59. What could be the problem? Why the timestamp is not working? Please let me know.
Thank you..
Oracle might store the datetime in higher precision, like 8:49:59.200. That's bigger than 8:49:59, but it will display the same.
Try this WHERE clause:
LogDateTime < (enddatetime + INTERVAL '1' SECOND)
This will still include anything which has the same starting second as the enddatetime.
What datatype is enddatetime? If it's a timestamp then there might be a mismatch between the type of the variable you are passing in (DateTime) and the type of the data in the table (Timestamp) this could cause this as there might not be a timestamp valeue that exactly matches the datetime value ... and the closest available value might be "off" in the direction that causes the record to be filtered out.
Is LogDateTime of TIMESTAMP datatype? It stores fractional part of seconds. Possibly your date is not exactly 11/23/09 8:50:09.
Try to output your date using TO_CHAR(LogDateTime,'MM/DD/YYYY HH24:MI:SS.FF') to see if that's the case.
How to write a query to find the time difference ?
time format is like this
2009-08-12 02:59:59
i want to compare
this time with
2009-08-12 02:59:10
how to check these two
i want to return some row having the time difference is 30sec
how to write a SQL statement ??
select date_part('second',date1) - date_part('second',date2)
In SQL you can do like this which give you output in seconds
SELECT * FROM your_table WHERE time1_column - time2_column = interval '30s'
Sorry this is the best I can do, given your description of the problem...
if both the times are columns in database table, you can directly use
relational operators (>, < and =)
Ex. if you have a table like
Test
(
id bigint,
starttime timestamp,
endtime timestamp
);
then you can have queries like
select * from test where starttime > end time
etc..
If you want to compare two dates in query, you can first convert text to time and then compare them
you can use: datetime function of pgsql
see: http://www.postgresql.org/docs/6.3/static/c10.htm
for more details