Extract a full timestamp (date included) from a Select query; Oracle

别等时光非礼了梦想. 提交于 2019-12-04 02:29:47

问题


So I am trying to insert multiples rows of data from one table to another. I have done this; however, I am having an issue with some of my columns, specifically my date columns. When the query returns data it is missing the time component of the date which is normally present. If this doesn't make sense hopefully the follow helps things to make sense.

My original query

SELECT 'insert into dante2 (subcar, batch_id, silicon, temperature, sulphur, manganese, phosphorus, start_pour, end_pour, sched_cast_date) values(',    SUBCAR,',',BBP.BATCH_ID   ,',',   SILICON                 ,',',TEMPERATURE                   ,',',   SULPHUR                 ,',',   MANGANESE                  ,',',   pHOSPHORUS                  ,',''',START_POUR      ,''',''',    END_POUR,''',''',SCHED_CAST_DATE              ,''');'
FROM  bof_chem_sample bcs, bof_batch_pour bbp, bof_celox_sample bofcs 
WHERE bcs.SAMPLE_CODE= to_char('D1')
and bofcs.sample_code=bcs.sample_code
and bofcs.batch_id=bcs.batch_id
and bcs.batch_id = bbp.batch_id
and bofcs.temperature>0
AND bbp.START_POUR>=to_date('01-JAN-11')
order by bbp.start_pour

Results of the query:

insert into dante2 (subcar, batch_id, silicon, temperature, sulphur, manganese, phosphorus, start_pour, end_pour, sched_cast_date) 
  values( 101,65277 ,0.6631,2525 ,0.0551,0.3366,0.043,'01-JAN-11','01-JAN-11','31-DEC-10'); 
insert into dante2 (subcar, batch_id, silicon, temperature, sulphur, manganese, phosphorus, start_pour, end_pour, sched_cast_date) 
  values( 123,65277 ,0.6631,2525 ,0.0551,0.3366,0.043,'01-JAN-11','01-JAN-11','31-DEC-10'); 
insert into dante2 (subcar, batch_id, silicon, temperature, sulphur, manganese, phosphorus, start_pour, end_pour, sched_cast_date) 
  values( 123,65278 ,0.7116,2470 ,0.0598,0.333,0.0423,'01-JAN-11','01-JAN-11','31-DEC-10'); 
insert into dante2 (subcar, batch_id, silicon, temperature, sulphur, manganese, phosphorus, start_pour, end_pour, sched_cast_date) 
  values( 116,65278 ,0.7116,2470 ,0.0598,0.333,0.0423,'01-JAN-11','01-JAN-11','31-DEC-10'); 

However, I want the dates to look like dd-mon-yy hh24:mi. Does anyone know how to fix this?


回答1:


Two options:

1) If you have alter session privilege, change the NLS_DATE_FORMAT before running the SELECT statement as given below:

ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-RR HH24:MI';

2) If you don't have ALTER session privilege then apply the transformation for each date field as given below (the stmnt below shows it for START_POUR)

'TO_DATE(''' || TO_CHAR(START_POUR, 'DD-MON-RR HH24:MI') || ''', ''DD-MON-RR HH24:MI'')'



回答2:


I wonder if you are making this problem more complicated than it needs to be.

There is the simple form of INSERT where you need need to provide a VALUES clause, but you can also insert the results of a SELECT statement directly.

So you could just write something like:

insert into dante2 (
  subcar, batch_id, silicon, temperature, sulphur, manganese, 
  phosphorus, start_pour, end_pour, sched_cast_date
)
SELECT 
  SUBCAR, BBP.BATCH_ID, SILICON, TEMPERATURE, SULPHUR, MANGANESE,
  pHOSPHORUS, START_POUR, END_POUR, SCHED_CAST_DATE
FROM 
  bof_chem_sample bcs, 
  bof_batch_pour bbp, 
  bof_celox_sample bofcs 
WHERE 
  bcs.SAMPLE_CODE= to_char('D1') and 
  bofcs.sample_code=bcs.sample_code and 
  bofcs.batch_id=bcs.batch_id and 
  bcs.batch_id = bbp.batch_id and 
  bofcs.temperature>0 AND 
  bbp.START_POUR>=to_date('01-JAN-2011', 'dd-mon-yyyy');

This will have the advantage of being much faster than your existing solution since each of the INSERT statements that you were running would take time to parse and execute but this has only single statement to run. It also runs in one step since you don't need to create the INSERT statements and all the types (like dates) are handled correctly.

However if you still need to create insert statements then you need to handle the data types. Strings and numbers are fine because Oracle knows what to do with them. dates are more of an issue because unless you use the to_char and to_date functions then you are relying on implicit conversion which will cause you mysterious problems like the ones you are experiencing. It's probably worth reading the Oracle documentation on implicit conversion to find out why it is bad.

As already mentioned by Cybernate, you would need to explicitly convert the date/time columns firstly to a character string (using to_char) that can then be converted back to a date by using to_date.




回答3:


First, do you have some sample data of the date fields?

If the information about the timestamp isn't stored it will do little good to call it.

The main issue I would look into it the format of the source you're pulling from.

From there you can set the format of the SELECT statement's output.



来源:https://stackoverflow.com/questions/6334789/extract-a-full-timestamp-date-included-from-a-select-query-oracle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!