SQL Oracle 日期 TO_CHAR('Month DD, YYYY') 中有多余的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7166446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:49:43  来源:igfitidea点击:

Oracle Date TO_CHAR('Month DD, YYYY') has extra spaces in it

sqloracleoracle10g

提问by contactmatt

When I do...

当我做...

Select TO_CHAR (date_field, 'Month DD, YYYY')
from...

I get the following:

我得到以下信息:

July      01, 2011
April     01, 2011
January   01, 2011

Why are there extra spaces between my month and day? Why doesn't it just put them next to each other?

为什么我的月和日之间有多余的空格?为什么不把它们放在一起?

采纳答案by Ben

if you use 'Month' in to_char it right pads to 9 characters; you have to use the abbreviated 'MON', or to_char then trim and concatenate it to avoid this. See, http://www.techonthenet.com/oracle/functions/to_char.php

如果你在 to_char 中使用 'Month',它会填充到 9 个字符;您必须使用缩写的“MON”或 to_char 然后修剪并连接它以避免这种情况。见,http://www.techonthenet.com/oracle/functions/to_char.php

select trim(to_char(date_field, 'month')) || ' ' || to_char(date_field,'dd, yyyy')
  from ...

or

或者

select to_char(date_field,'mon dd, yyyy')
  from ...  

回答by NullUserException

Why are there extra spaces between my month and day? Why does't it just put them next to each other?

为什么我的月和日之间有多余的空格?为什么不把它们放在一起?

So your output will be aligned.

所以你的输出将是对齐的。

If you don't want padding use the format modifier FM:

如果您不想填充,请使用格式修饰符FM

SELECT TO_CHAR (date_field, 'fmMonth DD, YYYY') 
  FROM ...;

Reference: Format Model Modifiers

参考:格式模型修饰符

回答by Rattlesnake

You should use fm element to delete blank spaces.

您应该使用 fm 元素来删除空格。

SELECT TO_CHAR(sysdate, 'fmDAY DD "de" MONTH "de" YYYY') CURRENT_DATE
FROM   dual;

回答by Ashish Brajesh

try this:-

尝试这个:-

select to_char(to_date('01/10/2017','dd/mm/yyyy'),'fmMonth fmDD,YYYY') from dual;

select to_char(sysdate,'fmMonth fmDD,YYYY') from dual;

回答by Lijo Mathews

select to_char(sysdate, 'DD-fmMONTH-YYYY') "Date" from Dual;

select to_char(sysdate, 'DD-fmMONTH-YYYY') "Date" from Dual;

The above query result will be as given below.

上述查询结果如下。

Date

日期

01-APRIL-2019

2019 年 4 月 1 日

回答by Brian Fitzgerald

SQL> -- original . . .
SQL> select
  2  to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ) dt
  3  from dual;

DT
----------------------------------------
Friday    the 13th of May      , 2016

SQL>
SQL> -- collapse repeated spaces . . .
SQL> select
  2  regexp_replace(
  3      to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
  4      '  * *', ' ') datesp
  5  from dual;

DATESP
----------------------------------------
Friday the 13th of May , 2016

SQL>
SQL> -- and space before commma . . .
SQL> select
  2  regexp_replace(
  3      to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
  4      '  *(,*) *', ' ') datesp
  5  from dual;

DATESP
----------------------------------------
Friday the 13th of May, 2016

SQL>
SQL> -- space before punctuation . . .
SQL> select
  2  regexp_replace(
  3      to_char( sysdate, 'Day "the" Ddth "of" Month, yyyy' ),
  4      '  *([.,/:;]*) *', ' ') datesp
  5  from dual;

DATESP
----------------------------------------
Friday the 13th of May, 2016