Selecting only time from date - sql

If I do:
SELECT PRESERV_STARTED
FROM HARVESTED_L;
I will get values like:
23-12-1999 00:00:00
21-03-2000 22:01:37
...
And so on. (PRESERV_STARTED has type DATE)
What I want is only to select the date with time part, where the time is not 00:00:00, so that I can omit those.
There is a lot of info about a solution to this, saying I can do something like:
select cast(AttDate as time) [time]
from yourtable
And for older versions of sql server:
select convert(char(5), AttDate, 108) [time]
from yourtable
And yet other proposals are:
SELECT CONVERT(VARCHAR(8),GETDATE(),108)
I tried all of these, among a few others, but no luck.
So my question is, having a date like: 23-12-1999 00:00:00, how do I select the time part?
What comes most intuitive to me (mixing with the proposals I found) is something like:
SELECT CONVERT(VARCHAR(8), GETDATE(PRESERV_STARTED), 108) AS timePortion
FROM HARVESTED_L;
I get an error from this code, saying "Missing expression". In fact, this is the error I get from most of the proposals I tried.
I am using Oracle SQL Developer version 4.1.1.19

In Oracle you can just format the date(time) however you like:
SELECT TO_CHAR(preserv_started, 'HH24:MI:SS')
FROM harvested_l

If I understand correctly you want to select only the rows for which the time part of the date column is not 00:00:00. You don't have to get the time part in order to do this. You can use TRUNC function which (by default) returns date with the time part truncated. Here's an example:
SQL> select * from t;
ID D
---------- -------------------
1 2016-01-01 00:00:00
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
3 rows selected.
SQL> select * from t where d <> trunc(d);
ID D
---------- -------------------
2 2016-01-01 00:01:00
3 2016-01-01 00:01:23
2 rows selected.

Related

Number of records in a period of time PostgreSQL

I have a PostgreSql query, which should make a count of the results that exist between that time frame (between the field "date" and the current time "now"), however the query does nothing but count all the records without applying the filter, does anyone know what I am missing in the query?
This is the query:
SELECT count(*) from table where date between
TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') and now();
Result: 15,480 (all results, does not apply filter "between")
Greetings and thanks
select TO_TIMESTAMP('2022-8-1 12:00:00','YYYY-M-D HH:MI:SS') ;
to_timestamp
------------------------
2022-01-01 00:00:00-08
Per template patterns here Format functionsTable 9.26. Template Patterns for Date/Time Formatting it needs to be:
select TO_TIMESTAMP('2022-08-01 12:00:00','YYYY-MM-DD HH24:MI:SS') ;
to_timestamp
------------------------
2022-08-01 12:00:00-07
Though it would be easier to do something like:
select '2022-8-1 12:00:00'::timestamptz;
timestamptz
------------------------
2022-08-01 12:00:00-07
Ending up with:
SELECT count(*) from table where date between
'2022-8-1 12:00:00'::timestamptz and now();

ORA-00936: missing expression issue

I'm new to oracle sql and I'm trying to run the following query but getting above mentioned error.
SELECT CONVERT(DATETIME,CASE WHEN DATEPART (Hour, OrderTime) < 5
THEN DATEADD(Day, -1, CONVERT(date, ordertime))
ELSE CONVERT(date, ordertime)
END) AS ItemOrderTradingDate FROM TBL_ITEM
Please help.
It seems that you want to extract "hour" from a date column. If that's so, see these two options:
SQL> select ordertime,
2 to_char(ordertime, 'hh24') hour_1,
3 extract(hour from cast(ordertime as timestamp)) hour_2
4 from tbl_item;
ORDERTIME HOUR_1 HOUR_2
------------------- -------- ----------
06.04.2020 08:01:05 08 8
SQL>
Error you got when running Ankint's query was because extract - for certain values (hours, minutes, seconds) works on timestamps, not dates.
I'm not familiar with functions you used (convert, datepart, dateadd) so I can't tell what you really want to do once you find which hour it is. dateadd looks like you'd want to add (or subtract) 1 day from some data value. If that's so, then just do it: date arithmetic is (by default) done by days anyway:
SQL> select sysdate right_now,
2 sysdate + 1 tomorrow,
3 sysdate - 1 yesterday
4 from dual;
RIGHT_NOW TOMORROW YESTERDAY
------------------- ------------------- -------------------
06.04.2020 08:05:34 07.04.2020 08:05:34 05.04.2020 08:05:34
SQL>
You may try below query -
SELECT CASE WHEN EXTRACT(HOUR FROM OrderTime) < 5
THEN TRUNC(ordertime) - 1
ELSE TRUNC(ordertime)
END AS ItemOrderTradingDate
FROM TBL_ITEM

adding a VARCHAR to DATE as MINUTES

I'm pretty sure this is an easy one for you guys but it's driving me crazy.
I have a column with dates in a "YYYY-MM-DD" format and a column with small intergers values between 0 and 29. So I want to add the 2 columns together and get something like this:
Date | INT | NEW timestamp
2016-01-01 | 2 | 2016-01-01 00:02:00
2016-10-15 | 21 | 2015-10-15 00:21:00
so I tried the obvious like:
"Date" + "INT" as "NEW timestamp"
and stuff like
VARCHAR_FORMAT("INT",'MI')
or even
VARCHAR_FORMAT("Date",'YYYY-MM-DD HH24:MI:SS') + VARCHAR_FORMAT("INT",'MI')
but keep getting errors. I am doing this in dashDB
One option is to use:
select add_minutes(cast("date" as timestamp),"int") from yourTable
Another simple version is:
select cast("date" as timestamp) + "int" minutes from yourTable
On db2 iseries
select TIMESTAMP_FORMAT(Date , 'YYYY-MM-DD') + INT minute as Newtimestamp from yourtable
or
select cast(cast(Date as date) as timestamp) + int minute from yourtable
you can try this
Date_add(cast(`Date` as date),interval Int minute)

Time Difference in Redshift

how to get exact time Difference between two column
eg:
col1 date is 2014-09-21 02:00:00
col2 date is 2014-09-22 01:00:00
output like
result: 23:00:00
I am getting result like
Hours Minutes Seconds
--------------------
3 3 20
1 2 30
using the following query
SELECT start_time,
end_time,
DATE_PART(H,end_time) - DATE_PART(H,start_time) AS Hours,
DATE_PART(M,end_time) - DATE_PART(M,start_time) AS Minutes,
DATE_PART(S,end_time) - DATE_PART(S,start_time) AS Seconds
FROM user_session
but i need like
Difference
-----------
03:03:20
01:02:30
Use DATEDIFF to get the seconds between the two datetimes:
DATEDIFF(second,'2014-09-23 00:00:00.000','2014-09-23 01:23:45.000')
Then use DATEADD to add the seconds to '1900-01-01 00:00:00':
DATEADD(seconds,5025,'1900-01-01 00:00:00')
Then CAST the result to a TIME data type (note that this limits you to 24 hours max):
CAST('1900-01-01 01:23:45' as TIME)
Then LTRIM the date part of the value off the TIME data (as discovered by Benny). Redshift does not allow use of TIME on actual stored data:
LTRIM('1900-01-01 01:23:45','1900-01-01')
Now, do it in a single step:
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');
:)
SELECT LTRIM(DATEADD(seconds,DATEDIFF(second,'2014-09-23 00:00:00','2014-09-23 01:23:45.000'),'1900-01-01 00:00:00'),'1900-01-01');

PLSQL: Change first two digits of year

Some of the dates in a date column have an incorrect year. How do I change the first two digits of the year?
For example:
select
ID,
to_char(END_DT,'yyyy-mm-dd hh:mi:ss') as dt
From DB
Would result:
ID dt
1 0207-08-10 12:00:00
2 0208-03-31 12:00:00
3 0200-11-10 12:00:00
I want the results to look like this:
ID dt
1 2007-08-10 12:00:00
2 2008-03-31 12:00:00
3 2000-11-10 12:00:00
If you just want to "get" the correct date, you can use something like
select
substr(to_char(END_DT,'yyyy'),2,1) ||
substr(to_char(END_DT,'yyyy'),1,1) ||
substr(to_char(END_DT,'yyyy'),3,2) ||'-'||
to_char(END_DT,'mm-dd hh:mi:ss')
as dt
From DB;
The following logic should undo the problem, if this is a consistent formatting issue:
select (case when left(to_char(END_DT, 'yyyy'), 2) = '02'
then '20'||substr(to_char(END_DT,'yyyy-mm-dd hh:mi:ss'), 3, 100)
else to_char(END_DT,'yyyy-mm-dd hh:mi:ss')
end) as BetterFormat
This assumes that the problem is that the first two digits are reversed, and that this did not happen previous to 2000.
However, I must emphasize, the data is incorrect in the database. The right thing to do is to fix the data in the database. Covering up the problem might be making things worse in the medium term.