- 浏览: 442142 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
鱼里的yeol:
正在学习EJB 感觉有些吃力
Ejb3学习之二----Ejb3的Ejb Bean类型介绍 -
phoenix5870:
默认就是singleton的。
Spring中的Singleton模式和Java中的Singleton模式 -
jhys7s8jd:
pdf打印机下载 无水印http://www.onlinedo ...
PDFCreate工具的使用 -
wang371134086:
:e vil:
浅谈Struts2中的拦截器实现责任链模式 -
liu765023051:
亲,local与remote有什么区别呢
EJB学习之三---Local和Remote接口
在日常的项目中,经常遇见User需要显示不同的日期格式。当然,在Oracle和Ms Sql server中,处理日期格式的函数就是两个:TO_Char(Oracle)和Convert(Sql Server).在本文中,对Oracle处理日期格式进行详细介绍,对Sql Server中的处理方式只是蜻蜓点水,做个对照。下面就是一个Sample:要求将日期转化成 "DD MMM YYYY"这种形式显示。
一. Format Description:以下来源于Oracle 官方站点。
TO_CHAR(number, format, NLS_Params)
The format mask and the NLS parameters are identical to the TO_NUMBER function.
The NLS parameters again are
NLS_NUMERIC_CHARACTERS -- Specifies characters to use for group separators and the decimal point.
NLS_CURRENCY -- Specifies the local currency.
NLS_ISO_CURRENCY -- Character(s) to represent the ISO currency symbol.
The optional format string you may pass to TO_CHAR() has a number of parameters that affect the string returned by TO_CHAR().
Some of these parameters are listed in the following table.
Parameter Format Examples Description
9 999 Returns digits in specified positions with leading negative sign if the number is negative.
0 0999 9990 0999: Returns a number with leading zeros.9990: Returns a number with trailing zeros.
. 999.99 Returns a decimal point in the specified position.
, 9,999 Returns a comma in the specified position.
$ $999 Returns a leading dollar sign.
B B9.99 If the integer part of a fixed point number is zero, returns spaces for the zeros.
C C999 Returns the ISO currency symbol in the specified position. The symbol comes from the NLS_ISO_CURRENCY parameter.
D 9D99 Returns the decimal point symbol in the specified position. The symbol comes from the NLS_NUMERIC_CHARACTER parameter (default is a period character).
EEEE 9.99EEEE Returns number using the scientific notation.
FM FM90.9 Removes leading and trailing spaces from number.
G 9G999 Returns the group separator symbol in the specified position. The symbol comes from the NLS_NUMERIC_CHARACTER parameter.
L L999 Returns the local currency symbol in the specified position. The symbol comes from the NLS_CURRENCY parameter.
MI 999MI Returns a negative number with a trailing minus sign. Returns a positive number with a trailing space.
PR 999PR Returns a negative number in angle brackets (< >). Returns a positive number with leading and trailing spaces.
RN rn RN rn Returns number as Roman numerals. RN returns uppercase numerals; rn returns lowercase numerals. Number must be an integer between 1 and 3999.
S S999 999S S999: Returns a negative number with a leading negative sign; returns a positive number with a leading positive sign.999S: Returns a negative number with a trailing negative sign; returns a positive number with a trailing positive sign.
TM TM Returns a number using the minimum number of characters. Default is TM9, which returns the number using fixed notation unless the number of characters is greater than 64. If greater than 64, the number is returned using scientific notation.
U U999 Returns the dual currency symbol (Euro, for example) in the specified position. The symbol comes from the NLS_DUAL_CURRENCY parameter.
V 99V99 Returns number multiplied by 10x where x is the number of 9 characters after the V. If necessary, the number is rounded.
X XXXX Returns the number in hexadecimal. If the number is not an integer, the number is rounded to an integer.
Quote from:
Oracle Database 10g SQL (Osborne ORACLE Press Series) (Paperback)
# Paperback: 608 pages
# Publisher: McGraw-Hill Osborne Media; 1st edition (February 20, 2004)
# Language: English
# ISBN-10: 0072229810
# ISBN-13: 978-0072229813
二. 显示各种形式的日期:
1. to_char(sysdate,'hh24:mi:ss')
2. to_char(to_date(varchar2,'dd/mm/yyyy'),'is on Day')
3. to_char(sysdate,'DAY dy Dy')
4. to_char(sysdate,'MONTH mon') as month
5. to_char(sysdate,'fmMonth ddth, yyyy')
SQL Functions and Their Valid NLS Parameters
SQL Function Valid NLS Parameters
对比Sql Server的Convert函数:
在下表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。
* 默认值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始终返回世纪数位 (yyyy)。
** 当转换为 datetime 时输入;当转换为字符数据时输出。
*** 专门用于 XML。对于从 datetime 或 smalldatetime 到 character 数据的转换,输出格式如表中所示。对于从 float、money 或 smallmoney 到 character 数据的转换,输出等同于 style 2。对于从 real 到 character 数据的转换,输出等同于 style 1。
重要 默认情况下,SQL Server 根据截止年份 2049 解释两位数字的年份。即,两位数字的年份 49 被解释为 2049,而两位数字的年份 50 被解释为 1950。许多客户端应用程序(例如那些基于 OLE 自动化对象的客户端应用程序)都使用 2030 作为截止年份。SQL Server 提供一个配置选项("两位数字的截止年份"),借以更改 SQL Server 所使用的截止年份并对日期进行一致性处理。然而最安全的办法是指定四位数字年份。
当从 smalldatetime 转换为字符数据时,包含秒或毫秒的样式将在这些位置上显示零。当从 datetime 或 smalldatetime 值进行转换时,可以通过使用适当的 char 或 varchar 数据类型长度来截断不需要的日期部分。
下表显示了从 float 或 real 转换为字符数据时的 style 值。
在下表中,左列表示从 money 或 smallmoney 转换为字符数据时的 style 值。
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
SQL> select to_char(sysdate, 'dd Mon yyyy', 'NLS_DATE_LANGUAGE = AMERICAN')Eng_Show,to_char(sysdate, 'dd Mon yyyy') default_Show from dual; ENG_SHOW DEFAULT_SHOW -------------------- ---------------- 09 Mar 2010 09 3月 2010
一. Format Description:以下来源于Oracle 官方站点。
TO_CHAR(number, format, NLS_Params)
The format mask and the NLS parameters are identical to the TO_NUMBER function.
The NLS parameters again are
NLS_NUMERIC_CHARACTERS -- Specifies characters to use for group separators and the decimal point.
NLS_CURRENCY -- Specifies the local currency.
NLS_ISO_CURRENCY -- Character(s) to represent the ISO currency symbol.
The optional format string you may pass to TO_CHAR() has a number of parameters that affect the string returned by TO_CHAR().
Some of these parameters are listed in the following table.
Parameter Format Examples Description
9 999 Returns digits in specified positions with leading negative sign if the number is negative.
0 0999 9990 0999: Returns a number with leading zeros.9990: Returns a number with trailing zeros.
. 999.99 Returns a decimal point in the specified position.
, 9,999 Returns a comma in the specified position.
$ $999 Returns a leading dollar sign.
B B9.99 If the integer part of a fixed point number is zero, returns spaces for the zeros.
C C999 Returns the ISO currency symbol in the specified position. The symbol comes from the NLS_ISO_CURRENCY parameter.
D 9D99 Returns the decimal point symbol in the specified position. The symbol comes from the NLS_NUMERIC_CHARACTER parameter (default is a period character).
EEEE 9.99EEEE Returns number using the scientific notation.
FM FM90.9 Removes leading and trailing spaces from number.
G 9G999 Returns the group separator symbol in the specified position. The symbol comes from the NLS_NUMERIC_CHARACTER parameter.
L L999 Returns the local currency symbol in the specified position. The symbol comes from the NLS_CURRENCY parameter.
MI 999MI Returns a negative number with a trailing minus sign. Returns a positive number with a trailing space.
PR 999PR Returns a negative number in angle brackets (< >). Returns a positive number with leading and trailing spaces.
RN rn RN rn Returns number as Roman numerals. RN returns uppercase numerals; rn returns lowercase numerals. Number must be an integer between 1 and 3999.
S S999 999S S999: Returns a negative number with a leading negative sign; returns a positive number with a leading positive sign.999S: Returns a negative number with a trailing negative sign; returns a positive number with a trailing positive sign.
TM TM Returns a number using the minimum number of characters. Default is TM9, which returns the number using fixed notation unless the number of characters is greater than 64. If greater than 64, the number is returned using scientific notation.
U U999 Returns the dual currency symbol (Euro, for example) in the specified position. The symbol comes from the NLS_DUAL_CURRENCY parameter.
V 99V99 Returns number multiplied by 10x where x is the number of 9 characters after the V. If necessary, the number is rounded.
X XXXX Returns the number in hexadecimal. If the number is not an integer, the number is rounded to an integer.
Quote from:
Oracle Database 10g SQL (Osborne ORACLE Press Series) (Paperback)
# Paperback: 608 pages
# Publisher: McGraw-Hill Osborne Media; 1st edition (February 20, 2004)
# Language: English
# ISBN-10: 0072229810
# ISBN-13: 978-0072229813
二. 显示各种形式的日期:
1. to_char(sysdate,'hh24:mi:ss')
SQL> select sysdate as today, to_char(sysdate, 'hh24:mi:ss') convert_date from dual; TODAY CONVERT_DATE ----------- ------------ 3/9/2010 10 22:22:57
2. to_char(to_date(varchar2,'dd/mm/yyyy'),'is on Day')
SQL> select sysdate as today ,to_char(to_date('01/01/2010','dd/mm/yyyy'),'"is on "Day') as convert_date from dual; TODAY CONVERT_DATE ----------- --------------- 3/9/2010 10 is on 星期五
3. to_char(sysdate,'DAY dy Dy')
SQL> select to_char(sysdate,'DAY dy Dy') as day from dual; DAY -------------------------------------- 星期二 星期二 星期二
4. to_char(sysdate,'MONTH mon') as month
SQL> select to_char(sysdate,'MONTH mon') as month from dual; MONTH ----------------- 3月 3月
5. to_char(sysdate,'fmMonth ddth, yyyy')
SQL> select to_char(sysdate,'fmMonth ddth, yyyy') as convert_date from dual; CONVERT_DATE ------------------- 3月 9th, 2010
SQL Functions and Their Valid NLS Parameters
SQL Function Valid NLS Parameters
TO_DATE NLS_DATE_LANGUAGE NLS_CALENDAR TO_NUMBER NLS_NUMERIC_CHARACTERS NLS_CURRENCY NLS_DUAL_CURRENCY NLS_ISO_CURRENCY TO_CHAR NLS_DATE_LANGUAGE NLS_NUMERIC_CHARACTERS NLS_CURRENCY NLS_ISO_CURRENCY NLS_DUAL_CURRENCY NLS_CALENDAR TO_NCHAR NLS_DATE_LANGUAGE NLS_NUMERIC_CHARACTERS NLS_CURRENCY NLS_ISO_CURRENCY NLS_DUAL_CURRENCY NLS_CALENDAR NLS_UPPER NLS_SORT NLS_LOWER NLS_SORT NLS_INITCAP NLS_SORT NLSSORT NLS_SORT
对比Sql Server的Convert函数:
在下表中,左侧的两列表示将 datetime 或 smalldatetime 转换为字符数据的 style 值。给 style 值加 100,可获得包括世纪数位的四位年份 (yyyy)。
不带世纪数位 (yy) 带世纪数位 (yyyy) 标准 输入/输出** - 0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy 2 102 ANSI yy.mm.dd 3 103 英国/法国 dd/mm/yy 4 104 德国 dd.mm.yy 5 105 意大利 dd-mm-yy 6 106 - dd mon yy 7 107 - mon dd, yy 8 108 - hh:mm:ss - 9 或 109 (*) 默认值 + 毫秒 mon dd yyyy hh:mi:ss:mmmAM(或 PM) 10 110 美国 mm-dd-yy 11 111 日本 yy/mm/dd 12 112 ISO yymmdd - 13 或 113 (*) 欧洲默认值 + 毫秒 dd mon yyyy hh:mm:ss:mmm(24h) 14 114 - hh:mi:ss:mmm(24h) - 20 或 120 (*) ODBC 规范 yyyy-mm-dd hh:mm:ss[.fff] - 21 或 121 (*) ODBC 规范(带毫秒) yyyy-mm-dd hh:mm:ss[.fff] - 126(***) ISO8601 yyyy-mm-dd Thh:mm:ss:mmm(不含空格) - 130* 科威特 dd mon yyyy hh:mi:ss:mmmAM - 131* 科威特 dd/mm/yy hh:mi:ss:mmmAM
* 默认值(style 0 或 100、9 或 109、13 或 113、20 或 120、21 或 121)始终返回世纪数位 (yyyy)。
** 当转换为 datetime 时输入;当转换为字符数据时输出。
*** 专门用于 XML。对于从 datetime 或 smalldatetime 到 character 数据的转换,输出格式如表中所示。对于从 float、money 或 smallmoney 到 character 数据的转换,输出等同于 style 2。对于从 real 到 character 数据的转换,输出等同于 style 1。
重要 默认情况下,SQL Server 根据截止年份 2049 解释两位数字的年份。即,两位数字的年份 49 被解释为 2049,而两位数字的年份 50 被解释为 1950。许多客户端应用程序(例如那些基于 OLE 自动化对象的客户端应用程序)都使用 2030 作为截止年份。SQL Server 提供一个配置选项("两位数字的截止年份"),借以更改 SQL Server 所使用的截止年份并对日期进行一致性处理。然而最安全的办法是指定四位数字年份。
当从 smalldatetime 转换为字符数据时,包含秒或毫秒的样式将在这些位置上显示零。当从 datetime 或 smalldatetime 值进行转换时,可以通过使用适当的 char 或 varchar 数据类型长度来截断不需要的日期部分。
下表显示了从 float 或 real 转换为字符数据时的 style 值。
值 输出 0(默认值) 最大为 6 位数。根据需要使用科学记数法。 1 始终为 8 位值。始终使用科学记数法。 2 始终为 16 位值。始终使用科学记数法。
在下表中,左列表示从 money 或 smallmoney 转换为字符数据时的 style 值。
值 输出 0(默认值) 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取两位数,例如 4235.98。 1 小数点左侧每三位数字之间以逗号分隔,小数点右侧取两位数,例如 3,510.92。 2 小数点左侧每三位数字之间不以逗号分隔,小数点右侧取四位数,例如 4235.9819。
使用 CONVERT:
CONVERT (data_type[(length)], expression [, style])
select CONVERT(varchar, getdate(), 120 ) 2004-09-12 11:06:08 select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),\'-\',\'\'),\' \',\'\'),\':\',\'\') 20040912110608 select CONVERT(varchar(12) , getdate(), 111 ) 2004/09/12 select CONVERT(varchar(12) , getdate(), 112 ) 20040912 select CONVERT(varchar(12) , getdate(), 102 ) 2004.09.12 select CONVERT(varchar(12) , getdate(), 101 ) 09/12/2004 select CONVERT(varchar(12) , getdate(), 103 ) 12/09/2004 select CONVERT(varchar(12) , getdate(), 104 ) 12.09.2004 select CONVERT(varchar(12) , getdate(), 105 ) 12-09-2004 select CONVERT(varchar(12) , getdate(), 106 ) 12 09 2004 select CONVERT(varchar(12) , getdate(), 107 ) 09 12, 2004 select CONVERT(varchar(12) , getdate(), 108 ) 11:06:08 select CONVERT(varchar(12) , getdate(), 109 ) 09 12 2004 1 select CONVERT(varchar(12) , getdate(), 110 ) 09-12-2004 select CONVERT(varchar(12) , getdate(), 113 ) 12 09 2004 1 select CONVERT(varchar(12) , getdate(), 114 ) 11:06:08.177
评论
1 楼
25627380
2010-08-20
很强悍,我喜欢:
to_char(last_cal_date, 'DD Mon YYYY', 'NLS_DATE_LANGUAGE = AMERICAN')
这个在我的项目里有用到,谢谢!
to_char(last_cal_date, 'DD Mon YYYY', 'NLS_DATE_LANGUAGE = AMERICAN')
这个在我的项目里有用到,谢谢!
发表评论
-
Oracle的Hash Join之探究整理
2012-04-08 15:48 11312Hash join算法原理 自从or ... -
Oracle Event 10046
2012-03-29 23:17 1596下面是一个10046的例子,可以用来分析具体的一些sql执行计 ... -
使用SQL_TRACE进行数据库诊断
2012-03-29 23:08 964From:http://www.eygle.com/archi ... -
Oracle索引之B-Tree和Bitmap索引对比
2012-03-23 18:32 2326B树索引是所有大型关系 ... -
Oracle性能优化五大工具介绍
2012-03-23 15:55 1534本文介绍了Oracle性能优化工具Oracle数据库在线数据字 ... -
Oracle事物处理中回滚段容量的问题
2012-03-23 15:33 1592在执行大事务时,有时oracle会报出如下的错误: ORA- ... -
Oracle 中条件分歧总结
2011-06-01 23:17 1307Oracle 中条件分歧总结: * Decode * IF ... -
ORACLE CASE WHEN 及 SELECT CASE WHEN的用法
2011-06-01 23:07 2270转载:http://blog.csdn.net/songsen ... -
View的作用
2010-11-11 00:53 3128这篇文章重要讲述下关 ... -
Oracle For Update 行锁
2010-11-08 23:43 1922转自:http://hi.baidu.com/mcj0127/ ... -
Mysql的存储引擎:InnoDB和MyISAM区别
2010-10-05 02:25 1334InnoDB和MyISAM是许多人在 ... -
SSMAを活用してMySQL/AccessからSQL Server/Azureへマイグレーション
2010-09-10 23:03 2299作者 Abel Avram , 翻訳者 (株)ネクストスケープ ... -
Oracle 嵌套事务与自治事务思考
2010-04-22 21:35 6202关键字 嵌套事务和自治事务的概念 嵌套事务的使用 ... -
Oracle中Cursor介绍
2010-04-21 22:09 1734关键字 概念 类型 异常处理 一 概念 游标是SQL ... -
Oracle 10g 中动态性能视图
2010-04-16 19:18 2017动态性能视图用于记录当前例程的活动。启动例程时,oracle会 ... -
Oracle中RowNum的用法
2010-02-08 22:05 1771ROWNUM,是一种伪列,它根据特定记录返回一个序列化的数字。 ... -
ORACLE 分析函数解析
2010-01-17 21:29 1306分析函数是oracle 8.1.6中就引入的一个全新的概念,为 ... -
ORACLE LOB大对象处理
2010-01-16 21:42 2419ORACLE LOB大对象处理 主要是用来存储大量数据的数据库 ... -
DUMP用法
2010-01-16 09:13 1817一 DUMP():查看表中列在datafile中的存储内容,它 ... -
全角,半角互换
2010-01-12 12:08 1999对于全角和半角互换,oracle 提供了两个函数to_mult ...
相关推荐
Oracle数据库导入到SqlServer步骤(图文篇),自己写的,碰到不少问题,终于解决了。
Oracle通过透明网关连接SQL SERVER的实战记录,包括一些错误解决方法。
数据库访问类,采用工厂模式,支持Oracle,SQL Server,Access,以及一些常用的数据库相关的操作方法(如构造日期条件、构造In条件)
日期函数 1.绝对值 S:select abs(-1) value O:select abs(-1) value from dual 2.取整(大) S:select ceiling(-1.001) value O:select ceil(-1.001) value from dual 3.取整(小) S:...
SQL中的单记录函数 1.ASCII 返回与指定的字符对应的十进制数; SQL> select ascii('A') A,ascii('a') a,ascii('0') zero,ascii(' ') space from dual; A A ZERO SPACE --------- --------- --------- --------- ...
介绍就不多说了,下边是部分目录,觉得有用的话就顶一个 C:. │ sqlserver2000.txt │ ├─第01章 │ 1.9.1 设置内存选项.sql │ 1.9.2(2) 使用文件及文件组.sql │ 1.9.2(3) 调整...
。。。
。。。
亲测正确、有效的格式转换方法,试用于常见的应用场景。适合熟悉oracle,但在用SQLServer的小伙伴。
2或者sqlserver, mssqlserver数据库,不区分大小写,需java1.6及以上版本 3或者mysql, mysql数据库,不区分大小写,需java1.5及以上版本 4或者sybase, sybase数据库,不区分大小写(暂未测试) -url 数据库连接的url....
开发人员有时候使用类似下面SQL将字符串转换为日期时间类型,乍一看,这样的SQL的写法是没有什么问题的。但是这样的SQL其实有时候就是一个定时炸弹,随时可能出现问题(),下面简单对这种情况进行一个简单概括。 ...
There’s also more information about server internals–the way PL/SQL programs are run–than before, better enabling readers to optimize their code for fast and safe execution. Steven Feuerstein ...
文档描述了oracle中日期、字符串的相关转换方式,包括日期的8位、10位格式,精确到日、分、秒等
项目开发中用到的知识点 1、文件的操作,根据数据库日期创建Log文件 2、文件的读写操作,对有文件交互类的程序由较好的参考价值 3、Oracle数据库的操作(查删增改,存储过程的调用...4、SqlServer 数据库的查询更新操作
全书分为23章,涉及到SQLServer2000、SQLServer2005、Access、Oracle10g、MySql5.0和Postgresql8.2等6种数据库语言,内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、数据排序、...
delphi日期函数 Delphi里有现成的函数可以实现日期加减,是在DateUtils单元里的 mssql日期函数 日期转换参数
全书分为23章,涉及到SQLServer2000、SQLServer2005、Access、Oracle10g、MySql5.0和Postgresql8.2等6种数据库语言,内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、数据排序、...
北京 数据库端SQL语法的迁移以下为常用的SQL语法迁移,包括数据类型、ID列向SEQUENCE迁移、表(主键、...一>数据类型的迁移 、ORACLE端语法说明在ORACLE中,分析其数据类型,大致可分为数字、字符、日期时间和特殊
BDB是跨数据库平台的数据库设计和自动安装工具、 支持Oracle、MS SQLServer、Access、MySQL、SQLAnyWhere、Sybase数据库。 通过BDB可以快速建立数据库模型、并随时与实体数据库进行双向同步。 同时它还可以...
全书分为23章,涉及到SQLServer2000、SQLServer2005、Access、Oracle10g、MySql5.0和Postgresql8.2等6种数据库语言,内容包括SQL语言基础、常规数据查询、高级数据过滤、字符串查询、日期函数、日期函数、数据排序、...