invalid number oracle sql on datetime add - sql

I believe there is some bad data in some of the rows in this table. I'm getting invalid number when I try something like this... other tables don't cause an issue.
Is there someway I can prevent the query from failing?
where to_char(t1.create_date)
between to_char(t2.create_date - INTERVAL '10' MINUTE) and
to_char(t2.create_date + INTERVAL '10' MINUTE)

You could do something like this:
where t1.create_date
between (t2.create_date - 10/1440) and (t2.create_date + 10/1440)

Related

DateDiff in SSIS

I want to include a datediff into my SQL Command in SSIS but I am receiving the error below:
I have done quite a bit of research but cannot seem to get the command to work, tried changing the second to ss, tried include (DT_Date), tried quotations around second and other things but not sure why it#s not working.
Does anyone know what is the correct syntax to pass the query?
SELECT
http.xxx.xxx_at AS xxx_at,
http_xxxxx.xxxxx_at AS xxxxx_at,
DATEDIFF(second, xxx.xxx_at,http_xxxxx.xxxxx_at) As duration
FROM public.xxxxx
FETCH FIRST 100 ROWS ONLY
As suggested by Martin, there is no DATEDIFF command for postgres. The workaround is using DATE_PART:
((DATE_PART('day', created_at::timestamp - completed_at::timestamp) * 24 +
DATE_PART('hour', created_at::timestamp -completed_at::timestamp)) * 60 +
DATE_PART('minute', created_at::timestamp - completed_at::timestamp)) * 60 +
DATE_PART('second', created_at::timestamp -completed_at::timestamp) As Duration
Subtracting one timestamp from another will yield and interval from which you can then extract the seconds:
extract(epoch from created_at::timestamp - completed_at::timestamp) as duration

Nested query in INTERVAL

So I am trying to have a select statement within an INTERVAL in postgres
UPDATE v_obligation
SET current_alert_level=0,
last_update_date=now()AT TIME ZONE 'PST',
next_due_date = (now()+INTERVAL ''(
SELECT recurrence FROM v_obligation
WHERE obligation_id=4) Day'')
WHERE obligation_id = 4
But I am getting a syntax error in the nested query. I can't recall doing anything like this before. As you can see I tried escaping the quotes, but to no avail.
Sincere thanks for any help... It is greatly appreciated!
There is a simple way to convert integer to interval, eg.
select now()+ (select 5) * '1 day'::interval
So try this:
UPDATE v_obligation
SET current_alert_level=0,
last_update_date=now()AT TIME ZONE 'PST',
next_due_date = (now()+
(SELECT recurrence FROM v_obligation
WHERE obligation_id=4)* '1 day'::interval
WHERE obligation_id = 4

ORA-00936: Missing Expression Error Code

New here and was wondering if anyone can shed some light into this ORA-00936 error with my SQL Query.
select Count(Auftragsnr) as Open_Orders
from wa_kopf
where convert(varchar(4), Aender_Datum, 104) = convert(varchar(4), getdate(), 104)
and zustand < (60);
Get the following error:
14:47:37 [SELECT - 0 row(s), 0.000 secs] [Error Code: 936, SQL
State: 42000] ORA-00936: Missing Expression
Any help would be greatly appreciated.
Regards
JRidge
You're using SQL Server syntax, which won't work with an Oracle database. You seem to be trying to format a date as dd.mm.yyyy (but in a 4-character string, somehow?). There is no getdate function in Oracle either, but it does have sysdate instead.
So the Oracle equivalent for what I think you're trying to do would be:
where to_char(Aender_Datum, 'dd.mm.yyyy') = to_char(sysdate, 'dd.mm.yyyy')
But that would just mean you're comparing dates as string, which isn't generally a good idea. If you're looking for any value from today you could use the trunc() function, which strips the time portion from a date by default; if your column is only representing a date and the time is always midnight then you can do:
where Aender_Datum = trunc(sysdate)
If your column has varying times then you could truncate that as well:
where trunc(Aender_Datum) = trunc(sysdate)
... but that can have a performance impact if the column is indexed, and an alternative is to use a range comparison:
where Aender_Datum >= trunc(sysdate)
and Aender_Datum < trunc(sysdate) + 1

Type TIME is not a defined system type

We are using Microsoft Server 2012. I need to sum some variables from each day between given time gap.
Here is simplified query:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE CAST([wrkActionDate] as TIME) BETWEEN '19:00:00' AND '07:00:00'
And i get error:
Type TIME is not a defined system type.
I guess data type doesn't exist in "Microsoft Server 2012" if so how to write query which would do what i want to do?
P.S: Sorry if I made some writing errors. I don't compute English :)
FROM and WHERE should be swapped.
The cast to time works - just tried it with SQL Server 2012 - but the range is invalid (starting value greater than the end one) - you should do something like this:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE
CAST([wkActionDate] AS TIME) BETWEEN '19:00:00' AND '23:59:59' OR
CAST([wkActionDate] AS TIME) BETWEEN '00:00:00' AND '07:00:00'
You can also use DATEPART:
SELECT SUM(SomeVariable)
FROM MyTable
WHERE
DATEPART(hour,[wrkActionDate]) BETWEEN 19 AND 24 OR
DATEPART(hour,[wrkActionDate]) BETWEEN 0 AND 7

How to get records after a certain time using SQL datetime field

If I have a datetime field, how do I get just records created later than a certain time, ignoring the date altogether?
It's a logging table, it tells when people are connecting and doing something in our application. I want to find out how often people are on later than 5pm.
(Sorry - it is SQL Server. But this could be useful for other people for other databases)
For SQL Server:
select * from myTable where datepart(hh, myDateField) > 17
See http://msdn.microsoft.com/en-us/library/aa258265(SQL.80).aspx.
What database system are you using? Date/time functions vary widely.
For Oracle, you could say
SELECT * FROM TABLE
WHERE TO_CHAR(THE_DATE, 'HH24:MI:SS') BETWEEN '17:00:00' AND '23:59:59';
Also, you probably need to roll-over into the next day and also select times between midnight and, say, 6am.
In MySQL, this would be
where time(datetimefield) > '17:00:00'
The best thing I can think would be: don't use a DateTime field; well, you could use a lot of DATEADD/DATEPART etc, but it will be slow if you have a lot of data, as it can't really use an index here. Your DB may offer a suitable type natively - such as the TIME type in SQL Server 2008 - but you could just as easily store the time offset in minutes (for example).
For MSSQL use the CONVERT method:
DECLARE #TempDate datetime = '1/2/2016 6:28:03 AM'
SELECT
#TempDate as PassedInDate,
CASE
WHEN CONVERT(nvarchar(30), #TempDate, 108) < '06:30:00' then 'Before 6:30am'
ELSE 'On or after 6:30am'
END,
CASE
WHEN CONVERT(nvarchar(30), #TempDate, 108) >= '10:30:00' then 'On or after 10:30am'
ELSE 'Before 10:30am'
END
Another Oracle method for simple situations:
select ...
from ...
where EXTRACT(HOUR FROM my_date) >= 17
/
http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/functions050.htm#SQLRF00639
Tricky for some questions though, like all records with the time between 15:03:21 and 15:25:45. I'd also use the TO_CHAR method there.
In Informix, assuming that you use a DATETIME YEAR TO SECOND field to hold the full date, you'd write:
WHERE EXTEND(dt_column, HOUR TO SECOND) > DATETIME(17:00:00) HOUR TO SECOND
'EXTEND' can indeed contract the set of fields (as well as extend it, as the name suggests).
As Thilo noted, this is an area of extreme variability between DBMS (and Informix is certainly one of the variant ones).
Ok, I've got it.
select myfield1,
myfield2,
mydatefield
from mytable
where datename(hour, mydatefield) > 17
This will get me records with a mydatefield with a time later than 5pm.