postgresql兼容MySQL 时间函数date_format

MySQL DATE_FORMAT() 函数

定义和用法

DATE_FORMAT() 函数用于以不同的格式显示日期/时间数据。
语法

DATE_FORMAT(date,format)

date 参数是合法的日期。format 规定日期/时间的输出格式。

在postgresql创建同名函数实现相同的功能

CREATE OR REPLACE FUNCTION date_format(indate anyelement, intext text)
 RETURNS text
 LANGUAGE plpgsql
AS $function$
BEGIN
IF upper(inText) = upper('%Y%m%d_%H%i') THEN
return to_char(inDate,'YYYYMMDD_HH24MI');
END IF;
IF upper(inText) = upper('%Y%m%d%H%i%s') THEN
return to_char(inDate,'YYYYMMDDHH24MISS');
END IF;
IF upper(inText) = upper('%Y-%m-%d %H') THEN
return to_char(inDate,'YYYY-MM-DD HH24');
END IF;
IF upper(inText) = upper('%Y-%m-%d') THEN
return to_char(inDate,'YYYY-MM-DD');
END IF;
IF upper(inText) = upper('%Y-%m') THEN
return to_char(inDate,'YYYY-MM');
end if;
IF upper(inText) = upper('%m%d') THEN
return to_char(inDate,'MMDD');
END IF;
return '';
END;
$function$

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PHP 手册 Mehdi Achour Friedhelm Betz Antony Dovgal Nuno Lopes Hannes Magnusson Georg Richter Damien Seguy Jakub Vrana 其他贡献者 2007-12-10 Philip Olson 中文翻译人员: 王远之 肖理达 肖盛文 黄啸宇 宋琪 陈伯乐 刘铭 © 1997-2007 PHP 文档组 版权信息 前言 入门指引 简介 简明教程 安装与配置 安装前需要考虑的事项 Unix 系统下的安装 Mac OS X 系统下的安装 Windows 系统下的安装 PECL 扩展库安装 还有问题? 运行时配置 语言参考 基本语法 类型 变量 常量 表达式 运算符 控制结构 函数 类与对象(PHP 4) 类与对象(PHP 5) Namespaces 异常处理 引用的解释 安全 简介 总则 以 CGI 模式安装时 以 Apache 模块安装时 文件系统安全 数据库安全 错误报告 使用 Register Globals 用户提交的数据 魔术引号 隐藏 PHP 保持更新 特点 用 PHP 进行 HTTP 认证 Cookie 会话 处理 XForms 文件上传处理 使用远程文件 连接处理 数据库永久连接 安全模式 PHP 的命令行模式 函数参考 .NET — .NET 函数 Apache — Apache 特有函数 APC — APC 可选 PHP 缓存 APD — APD 高级 PHP 调试器 Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — Calendar 日历函数 CCVS — CCVS API 函数(已废弃) Classes/Objects — Classes/Objects 类/对象函数 Classkit — Classkit 函数 ClibPDF — ClibPDF Functions [deprecated] COM — COM 和 .Net(Windows)函数 Crack — Crack Functions ctype — Character Type Functions CURL — CURL, Client URL Library Functions Cybercash — Cybercash Payment Functions CyberMUT — Credit Mutuel CyberMUT functions Cyrus IMAP — Cyrus IMAP administration Functions Date/Time — Date/Time 日期/时间函数 DB++ — DB++ Functions dba — Database (dbm-style) Abstraction Layer Functions dBase — dBase Functions DBM — DBM Functions [deprecated] dbx — dbx Functions Direct IO — Direct IO Functions Directories — Directory 目录函数 DOM — DOM Functions DOM XML — DOM XML Functions enchant — enchant Functions Errors and Logging — Error Handling and Logging Functions Exif — Exif Functions Expect — Expect Functions fam — File Alteration Monitor Functions FDF — Forms Data Format Functions Fileinfo — Fileinfo Functions filePro — filePro Functions Filesystem — Filesystem 文件系统函数 Filter — Filter Functions Firebird/InterBase — Firebird/InterBase Functions Firebird/Interbase (PDO) — Firebird/Interbase Functions (PDO_FIREBIRD) FriBiDi — FriBiDi Functions FrontBase — FrontBase Functions FTP — FTP 函数 Function handling — Function Handling Functions GeoIP — GeoIP Functions gettext — Gettext Functions GMP — GMP Functions gnupg — gnupg 函数 gopher — Net_Gopher haru — Haru PDF Functions hash — hash Functions HTTP — HTTP 函数 Hyperwave — Hyperwave Functions Hyperwave API — Hyperwave API Functions i18n — i18n 函数 IBM (PDO) — IBM Functions (PDO_IBM) ibm_db2 — IBM DB2, Cloudscape and Apache Derby Functions iconv — iconv Functions id3 — ID3 Functions IIS Functions — IIS Administration Functions Image — Image 图像函数 Imagick Image Library IMAP — IMAP, POP3 and NNTP Functions Informix — Informix Functions Informix (PDO) — Informix Functions (PDO_INFORMIX) Ingres II — Ingres II Functions IRC Gateway — IRC Gateway Functions Java — PHP / Java Integration JSON — JSON 函数 kadm5 — KADM5 LDAP — LDAP Functions libxml — libxml Functions Lotus Notes — Lotus Notes Functions LZF — LZF Functions Mail — Mail 邮件函数 Mailparse — Mailparse Functions Math — Math 数学函数 MaxDB — MaxDB PHP Extension MCAL — MCAL Functions mcrypt — Mcrypt Encryption Functions MCVE — MCVE (Monetra) Payment Functions Memcache — Memcache Functions mhash — Mhash Functions Mimetype — Mimetype Functions Ming (flash) — Ming functions for Flash Misc. — Miscellaneous Functions mnoGoSearch — mnoGoSearch Functions MS SQL Server — Microsoft SQL Server Functions MS SQL Server (PDO) — Microsoft SQL Server and Sybase Functions (PDO_DBLIB) Msession — Mohawk Software Session Handler Functions mSQL — mSQL Functions Multibyte String — Multibyte String Functions muscat — muscat Functions MySQLMySQL 函数 MySQL (PDO) — MySQL Functions (PDO_MYSQL) mysqli — MySQLi 扩展库 Ncurses — Ncurses Terminal Screen Control Functions Network — Network Functions Newt — Newt Functions NSAPI — NSAPI-specific Functions Object Aggregation — Object Aggregation/Composition Functions Object overloading — Object property and method call overloading OCI8 — Oracle 函数 ODBC — ODBC Functions (Unified) ODBC and DB2 (PDO) — ODBC and DB2 Functions (PDO_ODBC) OGG/Vorbis — oggvorbis openal — OpenAL Audio Bindings OpenSSL — OpenSSL Functions Oracle — Oracle 函数(已废弃) Oracle (PDO) — Oracle Functions (PDO_OCI) Output Control — Output Control 输出控制函数 OvrimosSQL — Ovrimos SQL Functions Paradox — Paradox File Access Parsekit — Parsekit Functions PCNTL — Process Control Functions PCRE — Perl 兼容正则表达式函数 PDF — PDF 函数 PDO — PDO Functions phar — Phar archive stream and classes PHP Options/Info — PHP Options&Information POSIX — POSIX Functions POSIX Regex — POSIX 扩展正则表达式函数 PostgreSQLPostgreSQL 数据库函数 PostgreSQL (PDO) — PostgreSQL Functions (PDO_PGSQL) Printer — Printer Functions Program Execution — Program Execution Functions PS — PostScript document creation Pspell — Pspell Functions qtdom — qtdom Functions radius — Radius Rar — Rar Functions Readline — GNU Readline Recode — GNU Recode Functions RPMReader — RPM Header Reading 函数 runkit — runkit Functions SAM — SAM - Simple Asynchronous Messaging Satellite — Satellite CORBA client extension [deprecated] SCA — SCA Functions SDO — SDO Functions SDO DAS XML — SDO XML Data Access Service Functions SDO-DAS-Relational — SDO Relational Data Access Service Functions Semaphore — Semaphore, Shared Memory and IPC Functions SESAM — SESAM Database Functions Session PgSQL — PostgreSQL Session Save Handler Sessions — Session 会话处理函数 shmop — Shared Memory Functions SimpleXML — SimpleXML functions SNMP — SNMP 函数 SOAP — SOAP Functions Sockets — Socket Functions SPL — Standard PHP Library (SPL) Functions SQLite — SQLite Functions SQLite (PDO) — SQLite Functions (PDO_SQLITE) ssh2 — Secure Shell2 Functions Statistics — Statistics Functions Streams — Stream Functions Strings — String 字符串处理函数 SVN — Subversion 函数 SWF — Shockwave Flash Functions swish — Swish Functions Sybase — Sybase Functions TCP Wrappers — TCP Wrappers Functions tidy — Tidy Functions Tokenizer — Tokenizer Functions Unicode — Unicode Functions URLs — URL 函数 Variables — Variable 变量函数 Verisign Payflow Pro — Verisign Payflow Pro Functions vpopmail — vpopmail Functions W32api — W32api 函数 WDDX — WDDX Functions win32ps — win32ps Functions win32service — win32service Functions xattr — xattr Functions xdiff — xdiff Functions XML — XML 语法解析函数 XML-RPC — XML-RPC 函数 XMLReader — XMLReader functions XMLWriter — XMLWriter Functions XSL — XSL functions XSLT — XSLT Functions YAZ — YAZ Functions YP/NIS — YP/NIS Functions Zip — Zip File Functions Zlib — Zlib Compression Functions PHP at the Core: A Hacker's Guide to the Zend Engine The PHP 5 build system Extension structure Memory management Working with variables Writing functions Working with classes and objects Working with resources Working with INI settings Working with streams PDO Driver How-To Extension FAQs Zend Engine 2 API reference Zend Engine 1 The future: PHP 6 and Zend Engine 3 FAQ — FAQ:常见问题 一般信息 邮件列表 获取 PHP 数据库问题 安装 — 安装常见问题 编译问题 使用 PHP PHP 和 HTML PHP 和 COM PHP 和其它语言 从 PHP/FI 2 移植到 PHP 3 从 PHP 3 移植到 PHP 4 从 PHP 4 移植到 PHP 5 杂类问题 附录 PHP 及其相关工程的历史 Migrating from PHP 5.1.x to PHP 5.2.x Migrating from PHP 5.0.x to PHP 5.1.x 从 PHP 4 移植到 PHP 5 从 PHP 3 移植到 PHP 4 从 PHP/FI 2 移植到 PHP 3 PHP 的调试 配置选项 php.ini 配置选项 所支持时区列表 扩展库分类 函数别名列表 保留字列表 资源类型列表 支持的协议/封装协议列表 可用过滤器列表 所支持的套接字传输器(Socket Transports)列表 PHP 类型比较表 解析器代号列表 Userland Naming Guide 关于本手册 开放出版许可协议 函数索引 补充说明
MySQL中,有一个函数叫做DATE_FORMAT(),可以用来显示不同的日期/时间格式。例如,使用DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')可以显示当前日期和时间的格式为"Oct 20 2021 12:40 PM"。同样地,使用不同的格式字符串可以得到不同的输出结果。\[1\] 在PostgreSQL中,没有内置的DATE_FORMAT()函数,但我们可以使用自定义函数来实现相同的功能。通过编写自定义函数,我们可以兼容MySQL的日期格式,并简化迁移适配的应用SQL改造工作。在编写自定义函数之前,我们需要了解PostgreSQL时间/日期表现形式和处理逻辑基础。\[3\] 举例来说,如果我们想要在PostgreSQL中以与MySQL相同的格式显示当前日期和时间,我们可以编写一个自定义函数来实现。这个函数可以接受一个日期/时间值和一个格式字符串作为参数,并返回按照指定格式格式化后的结果。通过调用这个自定义函数,我们可以得到与MySQL中相同的输出结果。\[3\] 总之,虽然在PostgreSQL中没有内置的DATE_FORMAT()函数,但我们可以通过编写自定义函数来实现相同的功能,并兼容MySQL的日期格式。这样可以简化迁移适配的应用SQL改造工作。\[3\] #### 引用[.reference_title] - *1* *2* *3* [PostgreSQL兼容mysqldate_formate函数](https://blog.csdn.net/qq_36501591/article/details/127183395)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值