9.8. �������͸�ʽ������

PostgreSQL ��ʽ�������ṩһ����Ч�Ĺ������ڰѸ����������� ������/ʱ�䣬integer��floating point��numeric�� ת���ɸ�ʽ�����ַ����Լ��������Ӹ�ʽ�����ַ���ת���� ָ�����������͡� Table 9-20 �г�����Щ������ ��Щ��������ѭһ�������ĵ���ϰ�ߣ� ��һ�������Ǵ���ʽ����ֵ�����ڶ�����һ�����������ʽ��ģ�塣

Table 9-20. ��ʽ������

��������������������
to_char(timestamp, text)text��ʱ���ת�����ִ�to_char(current_timestamp, 'HH12:MI:SS')
to_char(interval, text)text��ʱ����תΪ�ִ�to_char(interval '15h 2m 12s', 'HH24:MI:SS')
to_char(int, text)text������ת�����ִ�to_char(125, '999')
to_char(double precision, text)text��ʵ��/˫������ת�����ִ�to_char(125.8::real, '999D9')
to_char(numeric, text)text�� numeric ת�����ִ�to_char(-125.8, '999D99S')
to_date(text, text)date���ִ�ת��������to_date('05 Dec 2000', 'DD Mon YYYY')
to_timestamp(text, text)timestamp with time zone���ִ�ת����ʱ���to_timestamp('05 Dec 2000', 'DD Mon YYYY')
to_number(text, text)numeric���ִ�ת���� numericto_number('12,454.8-', '99G999D9S')

���棺to_char(interval, text) �Ѿ������ˣ�����д�Ĵ����ﲻӦ�������ˡ�����һ���汾��������ɾ����

�����ģ���ִ���� to_char ���ԣ����ú��������ʶ��һЩ�ض���ģʽ������ �Ѵ���ʽ������ֵ��ȷ�ظ�ʽ������Ӧ�����ݡ� �κβ�����ģ��ģʽ���ı����򵥵����ֿ�����ͬ������һ������ ģ���ִ���Գ� to_char ����κζ�������ģ��ģʽ��ʶҪ�鿴�����������ִ������ҽ��ڸ�λ�� Ѱ����ֵ��

Table 9-21 ��ʾ�� �������ڸ�ʽ�����ں�ʱ��ֵ��ģ�档

Table 9-21. ��������/ʱ���ʽ����ģʽ

ģʽ����
HHһ���Сʱ�� ��01-12��
HH12һ���Сʱ�� ��01-12��
HH24һ���Сʱ�� ��00-23��
MI���� ��00-59��
SS�� ��00-59��
MS���� (000-999)
US΢�� (000000-999999)
SSSS��ҹ����� ��0-86399��
AM �� A.M. �� PM �� P.M.�����ʶ����д��
am �� a.m. �� pm �� p.m.�����ʶ��Сд��
Y,YYY�����ŵ��꣨4 �͸���λ��
YYYY�꣨4�͸���λ��
YYY��ĺ���λ
YY��ĺ���λ
Y������һλ
IYYYISO �꣨4λ�����λ��
IYYISO ������ 3 λ
IYISO ������ 2 λ
IISO ������һλ
BC �� B.C. �� AD �� A.D.��Ԫ��ʶ����д��
bc �� b.c. �� ad �� a.d.��Ԫ��ʶ��Сд��
MONTHȫ����д�·������հ����Ϊ9�ַ���
Monthȫ����ϴ�Сд�·������հ����Ϊ9�ַ���
monthȫ��Сд�·������հ����Ϊ9�ַ���
MON��д��д�·�����3�ַ���
Mon��д��ϴ�Сд�·�����3�ַ���
monСд��д�·�����3�ַ���
MM�·ݺţ�01-12��
DAYȫ����д���������հ����Ϊ9�ַ���
Dayȫ����ϴ�Сд���������հ����Ϊ9�ַ���
dayȫ��Сд���������հ����Ϊ9�ַ���
DY��д��д��������3�ַ���
Dy��д��ϴ�Сд��������3�ַ���
dy��дСд��������3�ַ���
DDDһ��������ӣ�001-366��
DDһ����������ӣ�01-31��
Dһ��������ӣ�1-7��������1��
Wһ�������������1-5������һ�ܴӸ��µ�һ�쿪ʼ��
WWһ�����������1-53������һ�ܴӸ���ĵ�һ�쿪ʼ��
IWISO һ�������������һ���������ڵ�һ���
CC���ͣ�2 λ��
J�����գ��Թ�Ԫǰ4712��1��1������������
Q����
RM�������ֵ��·ݣ�I-XII��I=JAN������д��
rm�������ֵ��·ݣ�I-XII��I=JAN����Сд��
TZʱ���� ����д��
tzʱ���� ��Сд��

��һЩ���δʿ���Ӧ����ģ�����޸����ǵ���Ϊ�����磬 FMMonth ���� ���� FM ǰ׺�� Month ģʽ�� Table 9-22 ��ʾ�� ��������/ʱ���ʽ�������δ�ģʽ��

Table 9-22. ����/ʱ���ʽ����ģ��ģʽ���δ�

�����������
FM ǰ׺���ģʽ(�������հ׺���)FMMonth
TH ��׺��д˳������׺DDTH
th ��׺Сд˳������׺DDth
FX ǰ׺�̶���ʽȫ��ѡ����÷���֪��>FX Month DD Day
SP suffixƴдģʽ����δʵ�֣�DDSP

����/ʱ���ʽ�����÷���֪��

Table 9-23 ��ʾ�� ����������ֵ��ʽ����ģ��ģʽ��

Table 9-23. ������ֵ��ʽ����ģ��ģʽ

ģʽ����
9����ָ����ֵλ����ֵ
0��ǰ�����ֵ
. (���)����
, (����)���飨ǧ���ָ���
PR�������ڸ�ֵ
S�����ŵ���ֵ��ʹ���������ã�
L���ҷ��ţ�ʹ���������ã�
DС���㣨ʹ���������ã�
G����ָ�����ʹ���������ã�
MI��ָ����λ�õĸ��ţ�������� < 0��
PL��ָ����λ�õ����ţ�������� > 0��
SG��ָ����λ�õ���/����
RN�������֣������� 1 �� 3999 ֮�䣩
TH �� th������׺
V�ƶ�ָ��λ��С����������ע�⣩
EEEE��ѧ�����������ڻ�δʵ�֣�

���ָ�ʽ�����÷���֪��

Table 9-24 ��ʾ�� һЩʹ�� to_char �������÷���

Table 9-24. to_char ����

���ʽ���
to_char(current_timestamp, 'Day, DD  HH12:MI:SS')'Tuesday  , 06  05:39:18'
to_char(current_timestamp, 'FMDay, FMDD  HH12:MI:SS')'Tuesday, 6  05:39:18'
to_char(-0.1, '99.99')'  -.10'
to_char(-0.1, 'FM9.99')'-.1'
to_char(0.1, '0.9')' 0.1'
to_char(12, '9990999.9')'    0012.0'
to_char(12, 'FM9990999.9')'0012.'
to_char(485, '999')' 485'
to_char(-485, '999')'-485'
to_char(485,'9 9 9')' 4 8 5'
to_char(1485, '9,999')' 1,485'
to_char(1485, '9G999')' 1 485'
to_char(148.5, '999.999')' 148.500'
to_char(148.5, 'FM999.999')'148.5'
to_char(148.5, 'FM999.990')'148.500'
to_char(148.5, '999D999')' 148,500'
to_char(3148.5, '9G999D999')' 3 148,500'
to_char(-485, '999S')'485-'
to_char(-485, '999MI')'485-'
to_char(485, '999MI')'485 '
to_char(485, 'FM999MI')'485'
to_char(485, 'PL999')'+485'
to_char(485, 'SG999')'+485'
to_char(-485, 'SG999')'-485'
to_char(-485, '9SG99')'4-85'
to_char(-485, '999PR')'<485>'
to_char(485, 'L999')'DM 485
to_char(485, 'RN')'        CDLXXXV'
to_char(485, 'FMRN')'CDLXXXV'
to_char(5.2, 'FMRN')'V'
to_char(482, '999th')' 482nd'
to_char(485, '"Good number:"999')'Good number: 485'
to_char(485.8, '"Pre:"999" Post:" .999')'Pre: 485 Post: .800'
to_char(12, '99V999')' 12000'
to_char(12.4, '99V999')' 12400'
to_char(12.45, '99V9')' 125'