[英]Get month and Date from Year in Oracle Database
I am trying to get the date and month from Year entered by user.我试图从用户输入的年份中获取日期和月份。
Query I have used is:我使用的查询是:
SELECT trans_date FROM transactions WHERE (SUBSTR(trans_date,4,4) = 'June' AND TO_NUMBER(SUBSTR(trans_date,9,4)) = 2010);
This is not working but if I do the same with DUAL table it works.这不起作用,但如果我对 DUAL 表做同样的事情,它就可以工作。
SELECT SUBSTR('22-June-2018', 4, 4) FROM DUAL;
What is the problem with it ?它有什么问题?
Have you considered something like to_char()
?你有没有考虑过像
to_char()
这样的东西?
SELECT trans_date
FROM transactions
WHERE TO_CHAR(trans_date, 'YYYY-MM') = '2010-06';
Or better yet, proper date comparisons:或者更好的是,适当的日期比较:
WHERE trans_date >= DATE '2010-06-01' AND
trans_date < DATE '2010-07-01'
This has the advantage that the query can use an index on trans_date
.这样做的好处是查询可以使用
trans_date
上的索引。
SELECT to_char(to_date(trans_date,'DD-Month-YYYY'),'DD') day,
to_char(to_date(trans_date,'DD-Month-YYYY'),'Month') month
FROM transactions
WHERE to_char(to_date(trans_date,'DD-Month-YYYY'),'Month') = 'June'
AND to_char(to_date(trans_date,'DD-Month-YYYY'),'YYYY') = '2010');
What is the problem with it ?
它有什么问题?
SUBSTR( string_value, start, length )
takes a string data type as the 1st argument. SUBSTR( string_value, start, length )
将字符串数据类型作为第一个参数。 '22-June-2018'
is not a DATE
data type - it is a string data type (that just happens to contain characters that we recognise as a date); '22-June-2018'
不是DATE
数据类型 - 它是字符串数据类型(恰好包含我们识别为日期的字符); so using SUBSTR( '22-June-2018', 4, 4 )
works as expected.所以使用
SUBSTR( '22-June-2018', 4, 4 )
按预期工作。
trans_date
is a DATE
data type and not a string data type. trans_date
是DATE
数据类型,而不是字符串数据类型。
SUBSTR( trans_date, 4, 4 )
Involves an implicit conversion from a date to a string;涉及从日期到字符串的隐式转换; so is effectively doing:
所以有效地做:
SUBSTR( TO_CHAR( trans_date ), 4, 4 )
Since, TO_CHAR( date_value, format_model )
needs to know how to format the DATE
to a string, if you do not provide a second argument then it will use the NLS_DATE_FORMAT
session parameter and the expression is effectively:由于
TO_CHAR( date_value, format_model )
需要知道如何将DATE
格式化为字符串,如果您不提供第二个参数,那么它将使用NLS_DATE_FORMAT
会话参数并且表达式有效:
SUBSTR(
TO_CHAR(
trans_date
( SELECT value FROM nls_session_parameters WHERE parameter = 'NLS_DATE_FORMAT' )
),
4,
4
)
If the NLS_DATE_FORMAT
session parameter does not match DD-Month-YYYY
(or something equivalent where the 4-7th characters are month (which it would only be for 4-character month names) and 9th-12th characters are year (again, this would only be true for months with 4-character names) then your query will not work.如果
NLS_DATE_FORMAT
会话参数不匹配DD-Month-YYYY
(或其中第 4-7 个字符是月份(这仅适用于 4 个字符的月份名称)和第 9-12 个字符是年份(同样,这将仅适用于具有 4 个字符的名称的月份),那么您的查询将不起作用。
I am trying to get the date and month from Year entered by user.
我试图从用户输入的年份中获取日期和月份。
Use EXTRACT
:使用
EXTRACT
:
SELECT trans_date
FROM transactions
WHERE EXTRACT( MONTH FROM trans_date ) = 6
AND EXTRACT( YEAR FROM trans_date ) = 2010
Use TO_CHAR( date_value, format_model )
:使用
TO_CHAR( date_value, format_model )
:
SELECT trans_date
FROM transactions
WHERE TO_CHAR( trans_date, 'Month' ) = 'June'
AND TO_CHAR( trans_date, 'year' ) = '2010'
Use date literals:使用日期文字:
SELECT trans_date
FROM transactions
WHERE trans_date >= DATE '2010-06-01'
AND trans_date < ADD_MONTHS( DATE '2010-06-01', 1 );
Use TRUNC( date_value, 'MM' )
:使用
TRUNC( date_value, 'MM' )
:
SELECT trans_date
FROM transactions
WHERE TRUNC( trans_date, 'MM' ) = DATE '2010-06-01';
你可以使用像
WHERE TO_CHAR(date_column_name,'MM-YYYY')='06-2010'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.