postgresql数据类型转换

上传人:宝路 文档编号:2754108 上传时间:2017-07-27 格式:DOCX 页数:9 大小:167.17KB
返回 下载 相关 举报
postgresql数据类型转换_第1页
第1页 / 共9页
postgresql数据类型转换_第2页
第2页 / 共9页
postgresql数据类型转换_第3页
第3页 / 共9页
postgresql数据类型转换_第4页
第4页 / 共9页
postgresql数据类型转换_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《postgresql数据类型转换》由会员分享,可在线阅读,更多相关《postgresql数据类型转换(9页珍藏版)》请在金锄头文库上搜索。

1、postgresql 数据类型转换,日期操作函数各种数据类型(日期/时间、integer、floating point 和 numeric)转换成格式化的字符串以及反过来从格式化的字符串转换成指定的数据类型。下面列出了这些函数,它们都遵循一个公共的调用习 惯:第一个参数是待格式化的值,而第二个是定义输出或输出格式的模板。函数 返回类型 描述 例子to_char(timestamp, text) text 把时间戳转换成字串 to_char(current_timestamp, HH12:MI:SS)to_char(interval, text) text 把时间间隔转为字串 to_char(i

2、nterval 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_times

3、tamp(text, text) timestamp 把字串转换成时间戳 to_timestamp(05 Dec 2000, DD Mon YYYY)to_timestamp(double) timestamp把 UNIX 纪元转换成时间戳 to_timestamp(200120400)to_number(text, text) numeric 把字串转换成 numeric to_number(12,454.8-, 99G999D9S)用于日期/时间格式化的模式:模式 描述HH 一天的小时数(01-12)HH12 一天的小时数(01-12)HH24 一天的小时数(00-23)MI 分钟(00-

4、59)SS 秒(00-59)MS 毫秒(000-999)US 微秒(000000-999999)AM 正午标识(大写 )Y,YYY 带逗号的年(4 和更多位)YYYY 年(4 和更多位)YYY 年的后三位YY 年的后两位Y 年的最后一位MONTH 全长大写月份名(空白填充为 9 字符)Month 全长混合大小写月份名(空白填充为 9 字符)month 全长小写月份名(空白填充为 9 字符)MON 大写缩写月份名(3 字符)Mon 缩写混合大小写月份名(3 字符)mon 小写缩写月份名(3 字符)MM 月份号(01-12)DAY 全长大写日期名(空白填充为 9 字符)Day 全长混合大小写日期名

5、(空白填充为 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)(第一周从该年的第一天开始)下面是 PostgreSQL 中支持的时间/ 日期操作符的列表:操作符 例子 结果+ date 2001-09-28 + integer 7 date 2001-10-05+ date 2001-09-2

6、8 + interval 1 hour timestamp 2001-09-28 01:00+ date 2001-09-28 + time 03:00 timestamp 2001-09-28 03:00+ interval 1 day + interval 1 hour interval 1 day 01:00+ timestamp 2001-09-28 01:00 + interval 23 hours timestamp 2001-09-29 00:00+ time 01:00 + interval 3 hours time 04:00- - interval 23 hours int

7、erval -23:00- date 2001-10-01 - date 2001-09-28 integer 3- date 2001-10-01 - integer 7 date 2001-09-24- date 2001-09-28 - interval 1 hour timestamp 2001-09-27 23:00- time 05:00 - time 03:00 interval 02:00- time 05:00 - interval 2 hours time 03:00- timestamp 2001-09-28 23:00 - interval 23 hours times

8、tamp 2001-09-28 00:00- interval 1 day - interval 1 hour interval 23:00- timestamp 2001-09-29 03:00 - timestamp 2001-09-27 12:00 interval 1 day 15:00* interval 1 hour * double precision 3.5 interval 03:30/ interval 1 hour / double precision 1.5 interval 00:40postgresql 字符串转整数 int、integerpostgresql 字符

9、串转整数 int、integercode1234567-把1234转成整数select cast(1234 as integer ) ;-用 substring 截取字符串,从第 8 个字符开始截取 2 个字符:结果是 12select cast(substring(1234abc12,8,2) as integer)-使用 to_number 函数来转换成整数-to_number(text, text) 返回的类型 numeric 把字串转换成 numeric to_number(12,454.8-, 99G999D9S)select to_number(12121,999999999)用于

10、数值格式化的模板模式:模式 描述9 带有指定数值位数的值0 带前导零的值.(句点) 小数点,(逗号) 分组 (千) 分隔符PR 尖括号内负值S 带符号的数值L 货币符号D 小数点G 分组分隔符MI 在指明的位置的负号(如果数字 0)SG 在指明的位置的正/负号postgresql 获取当前时间 ,操作当前时间current_date 今天的日期例子:sql: select current_dateresult:2012-12-17current_time 现在的时间例子:sql: select current_timeresult:18:16:09.984+08current_timestam

11、p 日期和时间例子:sql: select current_timestampresult:2012-12-17 18:17:03.015+08postgresql windows 下修改帐号密码 (图文)重新设置 postgres 用户的密码方法:1、关闭数据库服务2、进入数据库的工作空间目录 (如果是建库是没有另外指定,应该就是 postgresql安装目录下的 data 目录)3、编辑修改文件 pg_hba.conf, 把连接权限设置的 md5 加密方式 改成 trust以我的为例,原本设置是# IPv4 local connections: host all all 127.0.0.1

12、/32 md5修改为# IPv4 local connections: host all all 127.0.0.1/32 trust 4、重新新启动 postgresql 数据库服务5、可以不用密码就能用 postgres 用户登陆,然后执行修改用户密码的操作 alter user postgres with password foobar;6、修改 pg_hba.conf 文件,改回到 md5 方式,再重启数据库服务设置完成。postgresql 日期计算,时间加减 方法在 PostgreSQL 中可以直接对时间进行加减运算:、SELECT now():timestamp + 1 year

13、; -当前时间加1 年SELECT now():timestamp + 1 month; -当前时间加一个月SELECT now():timestamp + 1 day; -当前时间加一天SELECT now():timestamp + 1 hour; -当前时间加一个小时SELECT now():timestamp + 1 min; -当前时间加一分钟SELECT now():timestamp + 1 sec; -加一秒钟select now():timestamp + 1 year 1 month 1 day 1 hour 1 min 1 sec; -加 1 年 1 月 1 天 1 时

14、1 分 1 秒SELECT now():timestamp + (col | day):interval FROM table -把 col 字段转换成天 然后相加PostgreSQL 替换字符串方法及字符串操作函数下面是 PostgreSQL 中提供的字符串操作符列表:替换字符的例子:update ab set a=replace(a,aaa,0)把 a 字段里面的 aaa字符串替换成 0函数返回类型描述 例子 结果string | string text 字串连接 Post | greSQL PostgreSQLbit_length(string) int 字串里二进制位的个数 bit_l

15、ength(jose) 32char_length(string) int 字串中的字符个数 char_length(jose) 4convert(string using conversion_name)text使用指定的转换名字改变编码。 convert(PostgreSQL using iso_8859_1_to_utf8)PostgreSQLlower(string) text 把字串转化为小写 lower(TOM) tomoctet_length(string) int 字串中的字节数 octet_length(jose) 4overlay(string placing string from int for int)text 替换子字串overlay(Txxxxas placing hom from 2 for 4)Thomasposition(substring in string)int 指定的子字串的位置 positi

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 中学教育 > 试题/考题

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号