从 Select 查询中提取完整时间戳(包括日期);甲骨文
所以我试图将多行数据从一个表插入到另一个表中。我已经做到了;但是,我的一些列有问题,特别是日期列。当查询返回数据时,它缺少通常存在的日期的时间部分。如果这没有意义,希望以下内容能帮助事情变得有意义。
我的原始查询
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
查询结果:
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');
但是,我希望日期看起来像 dd-mon-yy hh24:mi。 有谁知道如何解决这个问题?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有两个选项:
1) 如果您具有更改会话权限,请在运行
SELECT
语句之前更改 NLS_DATE_FORMAT,如下所示:2) 如果您没有 ALTER 会话权限,则对每个日期字段应用转换如下所示(下面的 stmnt 显示了 START_POUR)
Two options:
1) If you have alter session privilege, change the NLS_DATE_FORMAT before running the
SELECT
statement as given below: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)
我想知道您是否使这个问题变得比实际需要的更复杂。
有一种简单的
INSERT
形式,您需要提供VALUES
子句,但您也可以直接插入SELECT
语句的结果。因此,您可以编写如下内容:
这将具有比现有解决方案快得多的优点,因为您运行的每个 INSERT 语句都需要时间来解析和执行,但只有单个语句要运行。它还一步运行,因为您不需要创建 INSERT 语句,并且所有类型(如日期)都得到正确处理。
但是,如果您仍然需要创建插入语句,那么您需要处理数据类型。字符串和数字很好,因为 Oracle 知道如何处理它们。日期更是一个问题,因为除非您使用
to_char
和to_date
函数,否则您将依赖隐式转换,这将导致您遇到的神秘问题。可能值得阅读有关隐式转换的 Oracle 文档< /a> 找出为什么不好。正如 Cybernate 已经提到的,您需要首先将日期/时间列显式转换为字符串(使用 to_char),然后可以使用 to_date 将其转换回日期。
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 aVALUES
clause, but you can also insert the results of aSELECT
statement directly.So you could just write something like:
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
andto_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.
首先,您有一些日期字段的示例数据吗?
如果没有存储有关时间戳的信息,那么调用它就没有什么用处。
我要研究的主要问题是您从中获取的源的格式。
从那里您可以设置 SELECT 语句的输出格式。
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.