在PostgreSQL中,to_char()函数用于将数值、日期和时间类型的值转换为字符串。以下是to_char()常用操作方法的一些示例:
- 将数值转换为指定格式的字符串:
-
to_char(123.45, ‘999,999.99’) 返回 ‘123.45’
-
to_char(123.45, ‘999.99’) 返回 ‘123.45’
-
to_char(123.45, ‘$999.99’) 返回 ‘$123.45’
- 将日期或时间类型转换为指定格式的字符串:
-
to_char(current_date, ‘YYYY-MM-DD’) 返回当前日期的字符串表示,例如 ‘2021-01-01’
-
to_char(current_timestamp, ‘YYYY-MM-DD HH24:MI:SS’) 返回当前日期和时间的字符串表示,例如 ‘2021-01-01 12:34:56’
-
to_char(current_timestamp, ‘FMMonth DD, YYYY’) 返回当前日期的字符串表示,例如 ‘January 01, 2021’
- 将日期或时间类型的值按照指定语言环境转换为字符串:
-
to_char(current_date, ‘FMMonth’, ‘French’) 返回当前月份的法语表示,例如 ‘janvier’
-
to_char(current_date, ‘FMMonth’, ‘German’) 返回当前月份的德语表示,例如 ‘Januar’
- 将数值或日期类型转换为带有千位分隔符的字符串:
-
to_char(1234567.89, ‘999G999G999D99’) 返回 ‘1,234,567.89’
-
to_char(current_date, ‘FMMonth DD, YYYY’, ‘999G999G999’) 返回 ‘Januar 01, 2021’
这只是to_char()函数的一些常见操作方法示例,实际使用中可以根据需要进行更多的格式化操作。