简体   繁体   English

在一个查询中将SQL中的日期从一种格式转换为多种格式

[英]Convert a date in SQL from one format to multiple formats in one query

I'm taking SQL in school and am really new to it. 我在学校学习SQL,对它真的很陌生。 We are using Oracle APEX for school. 我们正在学校使用Oracle APEX。 I need to write ONE query to convert a date into multiple formats. 我需要编写一个查询才能将日期转换为多种格式。 I thought I could do this by writing the statement with a substitution variable so I could input what the new format would be, but it isn't working exactly right. 我以为我可以通过用替换变量编写语句来做到这一点,这样我就可以输入新格式,但是这种格式工作得并不正确。 Below is my attempt at the one statement: 以下是我尝试的一种说法:

 SELECT TO_CHAR(TO_DATE('04-Jan-2008', 'DD-Mon-YYYY'), :xx) AS "Date"
 FROM dual; 

When I click "Run", the variables box pops up,and I type in the below so that it shows as "January 4th, 2008". 当我单击“运行”时,将弹出变量框,并在下面键入内容,使其显示为“ 2008年1月4日”。 I've tried it with and without quote marks. 我已经试过了,没有引号。 "January 4th" shows up, but not the comma and the year. 显示“ 1月4日”,但不显示逗号和年份。

 fmMonth ddth, YYYY   

Any thoughts? 有什么想法吗? Thanks for your input! 感谢您的输入!

Assuming the item where you enter the date mask is named P1_MASK, you'd have to refer it in your query with notation "& + item_name + .": 假设您在其中输入日期掩码的项目名为P1_MASK,则必须在查询中使用符号“&+ item_name +”来引用它:

SELECT to_char(TO_DATE('04-Ene-2008', 'DD-Mon-YYYY'), '&P1_MASK.') AS "Date"
  FROM dual; 

I figured it out. 我想到了。 I added a REPLACE function to it. 我添加了一个REPLACE函数。

SELECT REPLACE(TO_CHAR(TO_DATE('04-Jan-2008', 'DD-Mon-YYYY'), :xx), '-', ',') AS "Date"
FROM dual;

When the variables box pops up, I key in: 当变量框弹出时,我键入:

fmMonth ddth- YYYY 

It works perfectly! 它完美地工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM