Below is a query I ran just to try and investigate why one of my more complex queries isnt returning dates that I know are in tables. The query is in fact returning nothing.
Any idea why?
SELECT [Table].[Date Completed]
FROM [Table] WHERE [Table].[Date Completed]=6/28/2016;
There's a number of ways you can do this - it all boils down to getting SQL to recognise the date in the correct format.
So, as you've found yourself you can use:
SELECT [Date Completed] FROM Table WHERE [Date Completed] = #6/28/2016#
you could also use:
SELECT [Date Completed] FROM Table1 WHERE [Date Completed] = CDATE("28-Jun-2016")
This would also work:
SELECT [Date Completed] FROM Table1 WHERE [Date Completed] = #2016/06/28#
and this:
SELECT [Date Completed] FROM Table1 WHERE [Date Completed] = 42549
It's just a case of getting SQL to recognise the date.
Exact dates should between #
SELECT [Table].[Date Completed]
FROM [Table]
WHERE [Table].[Date Completed]=#6/28/2016#;
Related
I have tried below sql but somehow i am not able to delete or select data. The CRDT is timestamp datatype and i want to delete data from table where CRDT = '2021-03-13 14:00:01'. Below is my sql which is not working:
DELETE from TST_EP
where CRDT = TO_TIMESTAMP('2021-03-13 14:00:01');
You do not explain what "not working" means but, if you are experiencing issues with matching fractional seconds then you can match the down to the nearest second using:
DELETE FROM TST_EP
WHERE CRDT >= TIMESTAMP '2021-03-13 14:00:01'
AND CRDT < TIMESTAMP '2021-03-13 14:00:02';
Please define "is not working".
I'm guessing that you are getting an error message, first because you did not close the single quote around your string representation of a timestamp.
And after you fix that, you need to provide a format mask so that oracle will know how to interpret that string representation of a timestamp'
DELETE from TST_EP
where CRDT = TO_TIMESTAMP('2021-03-13 14:00:01','yyyy-mm-dd hh24:mi:ss');
If that doesn't fix your problem, then you need to be more explicit about "does not work".
And kudos on using 4-digit year. I am continually appalled at the number of duh-velopers who continue to recreate the Y2k bug.
The most probably explanation is that your TIMESTAMPhas defined with fractional seconds.
Here a small example
select crdt from tab;
CRDT
-------------------
25.03.2021 15:58:48
So you have one row in the table with this TIMESTAMP, let's try to select it
select count(*) from tab
where crdt = TO_TIMESTAMP('25.03.2021 15:58:48','dd.mm.yyyy hh24:mi:ss');
COUNT(*)
----------
0
Solution is to show the full TIMESTAMP with the fractional seconds:
select to_char(crdt,'DD.MM.YYYY HH24:MI:SSXFF') crdt from tab;
CRDT
-----------------------------
25.03.2021 15:58:48,116000
Now when use the complete TIMESTAMP you get the match.
select count(*) from tab
where crdt = TO_TIMESTAMP('25.03.2021 15:58:48,116000','DD.MM.YYYY HH24:MI:SSXFF');
COUNT(*)
----------
1
An other problem could be with the * time zone*, but this should not be your case, as you use the data type TIMESTAMP, i.e. without time zone.
Hi i'm new to Hive and I want to insert the current timestamp into my table along with a row of data.
Here is an example of my team table :
team_id int
fname string
lname string
time timestamp
I have looked at some other examples, How to insert timestamp into a Hive table?, How can I add a timestamp column in hive and can't seem to get it to work.
This is what I am trying:
insert into team values('101','jim','joe',from_unixtime(unix_timestamp()));
The error I get is:
FAILED: SemanticException [Error 10293]: Unable to create temp file for insert values Expression of type TOK_FUNCTION not supported in insert/values
If anyone could help, that would be great, many thanks frostie
Can be achieved through current_timestamp() , but only via select clause. don't even require from clause in select statment.
insert into team select '101','jim','joe',current_timestamp();
or if your hive version doesn't support leaving from in select statment
insert into team select '101','jim','joe',current_timestamp() from team limit 1;
If you don't already have a table with at least one row, you can accomplish the desired result as such.
insert into team select '101','jim','joe',current_timestamp() from (select '123') x;
I am proficient in SQL-Server and other forms of SQL, but am trying to learn Oracle SQL. For some reason I cannot get even the simplest form of INSERT INTO .. SELECT .. to work, it always fails with "SQL command not properly ended."
Here is my current example:
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99));
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013;
Here's a SqlFiddle of it: http://sqlfiddle.com/#!4/c4d34/1
I cannot seem to figure out what's wrong here. I have checked about a dozen other related question here at SO and more than another dozen on Google but all of the answers either don't apply, or don't work. I have also tried about a million variations of the commands above, nothing seems to work.
Any help greatly appreciated.
FWIW, I now think that this is just a SQLFiddle problem, as many had contended.
The Oracle User who reported the problem to me with my code, was of course using the full SQL statement, before I had stripped it down to try to isolate the problem. That query had a completely different problem that just happened to report the same error in SQLFiddle. Specifically, its problem was that I was using As for table aliases, which apparently are invalid in Oracle (or perhaps, just in the query I had written).
In any event, sincere thanks to all who tried to help me.
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
/
INSERT INTO table1
(year, id, dat, categ)
select year, id, dat, categ from table1 where id=5000 and year=2013
This works, that is, if you paste both statements in the left (schema) window in SQL fiddle. I dont' think SQL Fiddle allows insert..select in the SQL window at all.
SQL Fiddle
CREATE TABLE table1 (year INT, id INT, dat DATE, categ VARCHAR(99))
//
INSERT INTO table1 (year, id, dat, categ)
SELECT year, id, dat, categ
FROM table1
WHERE id = 5000 AND year=2013
//
I don't know why you are facing this problem but there is no issue with syntax
I think it is just how you are executing the query on fiddle i just changed the execution flow and moved Insert statement in schema build section then the whole thing worked fine without changing a word (but i have inserted some sample data to show the exact working)
see this http://sqlfiddle.com/#!4/38e62/1
I have accdently deleted some rows in a table and did the commit too. Now
I want to recover them.
The DB I'm using is Oracle 11g R2.
I used the following query to get deleted records:
SELECT * FROM MY_TABLE AS OF TIMESTAMP ('13-MAR-11 8:50:58','DD-MON-YY HH24: MI: SS')
But while executing it gives an error saying:
Error at Command Line:3 Column:75
Error report:
SQL Error: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
But I couldn't figure the problem in this queury.
Can anyone pls help?
That requires an actual timestamp (or date), you're passing a pair of values.
Try:
SELECT * FROM MY_TABLE
AS OF TIMESTAMP TO_DATE('13-MAR-11 08:50:58','DD-MON-YY HH24:MI:SS')
(Your time format specifier isn't correct either and doesn't match your date string.)
for example :
SELECT * FROM EMP AS OF TIMESTAMP
TO_TIMESTAMP('2005-04-04 09:30:00', 'YYYY-MM-DD HH:MI:SS')
WHERE name = 'JOHN';
But flashback query may fail with ORA-1555 , other option :
Logminer
if Oracle supplement log is enabled , you can get undo sql for your delete statement
-- switch again logfile to get a minimal redo activity alter system switch logfile;
-- mine the last written archived log
exec dbms_logmnr.add_logfile('archivelog/redologfile', options =>dbms_logmnr.new);
exec dbms_logmnr.start_logmnr(options => dbms_logmnr.dict_from_online_catalog);
select operation, sql_redo from v$logmnr_contents where seg_name = 'EMP';
Oracle PRM-DUL
PRM-DUL will be last option. Even deleted row piece in Oracle block is always just marked row flag with deleted mask, the row piece still can be read via scan Oracle data block . PRM-DUL can scan the whole table , find out every record/row piece marked deleted and write out to flat file.
I'm reading a pipe delimited file with SQL Loader and want to populate a LAST_UPDATED field in the table I am populating. My Control File looks like this:
LOAD DATA
INFILE SampleFile.dat
REPLACE
INTO TABLE contact
FIELDS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
(
ID,
FIRST_NAME,
LAST_NAME,
EMAIL,
DEPARTMENT_ID,
LAST_UPDATED SYSTIMESTAMP
)
For the LAST_UPDATED field I've tried SYSTIMESTAMP and CURRENT_TIMESTAMP and neither work. SYSDATE however works fine but doesn't give me the time of day.
I am brand new to SQL Loader so I really know very little about what it is or isn't capable of. Thanks.
Have you tried the following:
CURRENT_TIMESTAMP [ (precision) ]
select current_timestamp(3) from dual;
CURRENT_TIMESTAMP(3)
-----------------------------
10-JUL-04 19.11.12.686 +01:00
To do this in SQLLDR, you will need to use EXPRESSION in the CTL file so that SQLLDR knows to treat the call as SQL.
Replace:
LAST_UPDATED SYSTIMESTAMP
with:
LAST_UPDATED EXPRESSION "current_timestamp(3)"
I accepted RC's answer because ultimately he answered what I was asking but my unfamiliarity with some of Oracle's tools led me to make this more difficult than it needed to be.
I was trying to get SQL*Loader to record a timestamp instead of just a date. When I used SYSDATE, and then did a select on the table it was only listing the the date (05-AUG-09).
Then, I tried RC's method (in the comments) and it worked. However, still, when I did a select on the table I got the same date format. Then it occurred to me it could just be truncating the remainder for display purposes. So then I did a:
select TO_CHAR(LAST_UPDATED,'MMDDYYYY:HH24:MI:SS') from contact;
And it then displayed everything. Then I went back to the control file and changed it back to SYSDATE and ran the same query and sure enough, the HH:MI:SS was there and accurate.
This is all being done in SqlDeveloper. I don't know why it defaults to this behavior. Also what threw me off are the following two statements in sqldeveloper.
SELECT CURRENT_TIMESTAMP FROM DUAL; //returns a full date and time
SELECT SYSDATE FROM DUAL; // returns only a date
If you want to use the table defined default you can use:
ROWDATE EXPRESSION "DEFAULT"
In Sql Developer run:
ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
and then check it with
SELECT SYSDATE FROM DUAL