简体   繁体   English

尝试在 oracle 中转换日期时间

[英]Trying to convert datetime in date in oracle

Hi i'm trying to convert date 01-03-2020 10:48:27 which obtained from query您好我正在尝试转换从查询中获得的日期 01-03-2020 10:48:27

 SELECT
  LAST_DAY( ADD_MONTHS(SYSDATE,-3 ) )+1
FROM
  dual;

into '01-Mar-2020' but not able to do trying many concept eg.进入“2020 年 3 月 1 日”,但无法尝试许多概念,例如。

trunc(SELECT  LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM  dual),'YEAR')

and

SELECT TRUNC(TO_DATE('SELECT  LAST_DAY( ADD_MONTHS(SYSDATE , - 3 ) )+1 FROM  dual','DD-MON-YY'), 'YEAR')  "New Year" FROM DUAL;

but getting error Any idea would be appreciated但得到错误任何想法将不胜感激

You're making things way too complicated.你把事情弄得太复杂了。 Oracle TRUNC takes an additional parameter to specify whatever time interval to truncate to: Oracle TRUNC 需要一个附加参数来指定要截断的任何时间间隔:

SELECT TRUNC(some_date_here, 'MON') FROM dual

If you put some_date_here as sysdate, then currently it will return 01-May-2020 until next month when it starts returning 01-Jun-2020如果您将 some_date_here 设置为 sysdate,则当前它将返回 01-May-2020,直到下个月开始返回 01-Jun-2020

You can truncate to any interval;您可以截断到任何间隔; TRUNC 01/01/2000 12:34:56 with 'MI' will return 01/01/2000 12:34:00. TRUNC 01/01/2000 12:34:56 与“MI”将返回 01/01/2000 12:34:00。 Truncating to DD is the default (cut the time off).截断到 DD 是默认设置(截断时间)。 Truncating to DAY sets the date back to the day that started the week in the country oracle thinks it is in (probably a Sunday or Monday)截断为 DAY 将日期设置回该国家/地区开始一周的日期 oracle 认为它在(可能是星期日或星期一)

More info: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084更多信息: https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions230.htm#i1002084

As I understood your problem you want to go from the current date, to the first of the month that was between 2 and < 3 months ago (so if it's May now, you want to go back to first of March until it's June, when you want to go back to first of April)据我了解您的问题,您希望 go 从当前日期到 2 到 3 个月前的第一个月的第一天(因此,如果现在是 5 月,您希望 go 回到 3 月 1 日,直到 6 月,你想 go 回到四月一日)

If you hence, in the current date of 5th May, want to go back to a date of 1 March, take 2 months off the current date and then TRUNC to the start of the month:因此,如果您想在当前日期 5 月 5 日将 go 退回到 3 月 1 日,请从当前日期推迟 2 个月,然后 TRUNC 到月初:

SELECT TRUNC(ADD_MONTHS(sysdate, -2), 'MON') FROM dual

Don't forget you can TRUNC to the nearest quarter of a year, so if you're doing a report that is "the current quarter", then looking at a variation of TRUNC(sysdate, 'Q') would be the way to go不要忘记你可以 TRUNC 到一年中最近的一个季度,所以如果你正在做一个“当前季度”的报告,那么查看 TRUNC(sysdate, 'Q') 的变体将是go

Lastly, I'd urge you NOT to use oracle to convert your dates to strings (in most cases) - if you keep it as a date all the way 'tIl it hits the user's computer it can be formatted for their regional preferences.最后,我敦促您不要使用 oracle 将您的日期转换为字符串(在大多数情况下) - 如果您一直将其保留为日期,直到它到达用户的计算机,它可以根据他们的区域偏好进行格式化。 If you make a decision as to the format as its coming out the dB it makes it much harder to deliver a good international experience for your app如果您决定以 dB 为单位的格式,则为您的应用程序提供良好的国际体验会变得更加困难

"Convert" in your case means TO_CHAR ;在您的情况下,“转换”是指TO_CHAR alter session is here to set default format for this session. alter session在这里设置此 session 的默认格式。

SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi:ss';

Session altered.

SQL> select
  2            last_Day(add_months(sysdate, -3)) + 1 orig,
  3    to_char(last_day(add_months(sysdate, -3)) + 1, 'dd-Mon-yyyy', 'nls_Date_language = english') result
  4  from dual;

ORIG                RESULT
------------------- --------------------
01.03.2020 07:25:44 01-Mar-2020

SQL>

Or, if you altered the session, you'd get it as或者,如果你改变了 session,你会得到它

SQL> alter session set nls_date_language = 'english';

Session altered.

SQL> alter session set nls_date_format = 'dd-Mon-yyyy';

Session altered.

SQL> select
  2            last_Day(add_months(sysdate, -3)) + 1 orig
  3  from dual;

ORIG
-----------
01-Mar-2020

SQL>

But, yes - usually we TO_CHAR it.但是,是的——通常我们TO_CHAR它。

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

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