在开发Oracle SQL或PL/SQL程序过程中,我们常常遇到将字符串类型转换成DATE类型的需求。我们会使用to_date来实现这个转换需求。但
在开发Oracle SQL或PL/SQL程序过程中,我们常常遇到将字符串类型转换成DATE类型的需求。我们会使用to_date来实现这个转换需求。但有时候这种转换会出错,报ORA-01843:not a valid month。有时候这种转换在开发系统上测试时是正常的,部署到生产环境时却出这种错误。
例如,我们需要将字符串类型的字符串'19-JUN-01'转换为时间类型的对象。
这个字符串是常见的英文环境中的时间表达方式"天-月-年",其中"月"使用英文简写。所以使用to_date(xx,'dd-mon-yy')转换它,转换格式是'dd-mon-yy'。
但是,在实际转换操作时,转换失败,如下所示:
SQL> select To_Date('19-JUN-01','dd-mon-yy') from dual;
select To_Date('19-JUN-01','dd-mon-yy') from dual
ORA-01843: not a valid month
出错信息显示"月"所对应的数据无效。
就是说,to_date函数在这个系统中不能识别"JUN".这是英文的月简写。
如果换成中文的表达方式,如'19-10月-01'。则系统就能正确转换。如下所示:
SQL> select To_Date('19-10月-01','dd-mon-yy') from dual;
TO_DATE('19-10月-01','DD-MON-Y
------------------------------
2001-10-19
这个问题的本质是系统不能识别英文的月简写,而能识别中文。
Oracle系统的语言配置主要保存在V$NLS_PARAMETERS数据字典视图中。查询该视图关于语言的设置值。如下:
SQL> select * from v$nls_parameters where parameter like '%DATE%';
PARAMETER VALUE
---------------------------------------------------------------- ----------------------------------------------------------------
NLS_LANGUAGE SIMPLIFIED CHINESE
NLS_DATE_LANGUAGE SIMPLIFIED CHINESE
证实数据库系统是语言环境是简体中文,这也是数据库实例级别的配置。
如果需要使To_Date('19-JUN-01','dd-mon-yy') 正常操作,只需要修改一下NLS_DATE_LANGUAGE的值,,修改为AMERICAN,就能识别。
对于开发人员而言,实例级的语言设置参数NLS_DATE_LANGUAGE是不能修改的,但你可以修改会话级的。只要将你连接上数据库实例的会话中该参数值修改为AMERICAN,也是能转换的。
SQL> alter session set nls_date_language='american';
Session altered
SQL> select To_Date('19-JUN-01','dd-mon-yy') from dual;
TO_DATE('19-JUN-01','DD-MON-YY
------------------------------
2001-6-19
SQL>
这样就可以使用了。

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

MySQLoffersvariouscharactersetsforstringdatatypes:1)latin1forWesternEuropeanlanguages,2)utf8formultilingualsupport,3)utf8mb4forextendedUnicodeincludingemojis,4)ucs2forfixed-widthencoding,and5)asciiforbasicLatin.Choosingtherightsetensuresdataintegrity

Streaming BLOBs is indeed better than direct storage because it reduces memory usage and improves performance. 1) By gradually reading and processing files, database bloat and performance degradation are avoided. 2) Streaming requires more complex code logic and may increase the number of I/O operations.

MySQLstringtypesimpactstorageandperformanceasfollows:1)CHARisfixed-length,alwaysusingthesamestoragespace,whichcanbefasterbutlessspace-efficient.2)VARCHARisvariable-length,morespace-efficientbutpotentiallyslower.3)TEXTisforlargetext,storedoutsiderows,


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
