PostgreSQL 8.0.0 �����ĵ���PostgreSQL �й� ������ | ||||
---|---|---|---|---|
Prev | Fast Backward | Chapter 9. �����Ͳ����� | Fast Forward | Next |
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 | ���ִ�ת���� numeric | to_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 | ������һλ |
IYYY | ISO �꣨4λ�����λ�� |
IYY | ISO ������ 3 λ |
IY | ISO ������ 2 λ |
I | ISO ������һλ |
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������һ�ܴӸ���ĵ�һ�쿪ʼ�� |
IW | ISO һ�������������һ���������ڵ�һ��� |
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 |
����/ʱ���ʽ�����÷���֪��
FM ����ǰ�������β��Ŀհף� ���û��ʹ�����Ļ������������������Щ������հ������ɹ̶���ȵ�ģʽ��
���û��ʹ�� FX ѡ� to_timestamp �� to_date ��ת���ִ���ʱ����Զ���հס� FX ������Ϊģ����ĵ�һ���������� ���� to_timestamp('2000 JUN', 'YYYY MON') ����ȷ�ģ� to_timestamp('2000 JUN', 'FXYYYY MON') �᷵��һ��������Ϊto_timestamp ֻԤ�ϻ���һ���հס�
�� to_char ģ�� ���������ͨ�ı����������ǻᱻ��������� �����һ���ִ��ŵ�˫������ǿ�������ͳ�һ���ı��� ��ʹ���������ģʽ�ؼ���Ҳ��ˡ����磬�� '"Hello Year "YYYY'�� YYYY ����������ݴ��棬����Year�ﵥ�� �� Y ���ᡣ
����������������˫���ţ���ô����������� ǰ���˫��б�ܣ����� '\\"YYYY Month\\"'. ����Ҫ������б������Ϊ��б�����ִ��������Ѿ������⺬���ˡ���
�����ʹ�õ���ݳ��� 4 λ�ַ�����ô�� YYYY ���ִ���timestamp����date ��ת��ʱҪ�ܵ����ơ� ������� YYYY ����ʹ��һЩ�������ַ�����ģ�壬 ����������ǽ���Ϊ 4 λ���֡����磨���� 20000 �꣩�� to_date('200001131', 'YYYYMMDD') ���ᱻ���ͳ�һ�� 4 λ���ֵ���ݣ� ����������ʹ��һ�������ֵķָ������� to_date('20000-1131', 'YYYY-MMDD') �� to_date('20000Nov31', 'YYYYMonDD')��
���ִ�ת��Ϊtimestampʱ�� ���루MS�����루US���������ִ� ��С�������IJ���ת���ġ����� to_timestamp('12:3', 'SS:MS') ���� 3 ����, ���� 300����Ϊת���������� 12 + 0.3 �롣 ����ζ�Ŷ��ڸ�ʽ 'SS:MS'���ԣ�����ֵΪ 12:3 �� 12:30��12:300 ��������ͬ��Ŀ�� ���롣���������룬�����ʹ�� 12:003����ôת����������� 12 + 0.003 = 12.003 �롣
������һ�������ӵ� ���ӡ� to_timestamp('15:12:02.020.001230', 'HH:MI:SS.MS.US') �� 15 Сʱ��12 ���ӣ��� 2 �� + 20 ���� + 1230�� = 2.021230 �롣
to_char �����ڼ��ı�ţ����� 'D' ��ʽ��ģʽ���� extract �IJ�ͬ��
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 | ��ѧ�����������ڻ�δʵ�֣� |
���ָ�ʽ�����÷���֪��
ʹ��SG��PL �� MI ���ɵķ��Ų��������������棻 ���磬 to_char(-12, 'S9999')���� ' -12', ��to_char(-12�� 'MI9999')���� '- 12'�� Oracle ���ʵ�ֲ������� 9 ǰ��ʹ�� MI������Ҫ��9 �� MI ǰ�档
9 ������ 9 �ĸ��� ��ͬ������λ������ֵ�����ij����ֵλû�����֣������һ���հס�
TH ����ת��С�������ֵ��Ҳ����ת��С����
PL��SG �� TH �� PostgreSQL ��չ��
V ����ذ�����ֵ���� 10^n������ n �Ǹ��� V��������֡� to_char ��֧�ְ� V ��һ��С���������һ��ʹ�� ��Ҳ����˵��99.9V99 �Dz�����ģ���
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' |