[英]Oracle date extract and concatenate
I have to take a date ( dd-mon-yyyy
) and change the year based on a user input. 我必须输入日期( dd-mon-yyyy
)并根据用户输入更改年份。 How do I extract the day and month and add the year while keeping the format? 如何在保留格式的同时提取日期和月份并添加年份?
For example, it is currently 01-JAN-2015
, and the user wants it to be 01-JAN-2016
. 例如,当前为01-JAN-2015
,用户希望它为01-JAN-2016
。 I currently am doing: 我目前正在做:
SELECT EXTRACT(DAY FROM contract_year) || EXTRACT(MONTH FROM contract_year) || '2016'
FROM table
WHERE program = programid
And my output is 112016
, but I want 01-JAN-2016
. 我的输出是112016
,但我想01-JAN-2016
。 Any suggestions? 有什么建议么?
Try this 尝试这个
SELECT to_char(add_months('01-JAN-2015', 12*1),'dd-MON-yyyy') -- replace 1 with n where n = years
from dual
If you want to add use given year to current date/month, use this 如果要将给定年份的使用添加到当前日期/月份,请使用此
with your_table as(
select '01-JAN-2015' as contract_year from dual
)
select to_date(substr(to_char(contract_year),1,7)||'2017','DD-MON-YYYY') from your_table
If your are not seeing output as required, it means that you have to change your client setting. 如果您没有看到所需的输出,则意味着您必须更改客户端设置。 Try running this in SQL Plus window. 尝试在SQL Plus窗口中运行它。
Combine to_date and to_char: 结合to_date和to_char:
SELECT to_date(to_char(contract_year,'dd-mm-') || '2016','dd-mm-yyyy')
FROM table
WHERE program = programid
It's not entirely clear to me whether contract_year
is a date or a string. 对我来说,不是很清楚contract_year
是日期还是字符串。
If it's a date: 如果是日期:
select to_char(contract_year, 'DD-MON-') || '2016'
from table
If it's a string in the format DD-MON-YYYY
then: 如果它是格式为DD-MON-YYYY
的字符串,则:
select to_char(to_date(contract_year, 'DD-MON-YYYY'), 'DD-MON-') || '2016'
from table
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.