postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式
数据库中:字符串 转换为 时间格式
二者区别:
to_data 转换为 普通的时间格式
to_timestamp 转换可为 时间戳格式
出错场景: 比较同一天 日期大小的时候,很容易出错
例如:
select current_timestamp from pub_employee
结果如下:

select current_timestamp <= to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') flag from pub_employee
语句中的2018-03-12 18:47:35 要比 current_timestamp当前的时间 大两个小时,
但是结果如下:

结果是 false
原因是:select to_date('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee
的结果如下:并不是时间戳

正确的写法
select current_timestamp <= to_timestamp('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') flag from pub_employee
结果:
为true
因为:select to_timestamp('2018-03-12 18:47:35','yyyy-MM-dd hh24:mi:ss') from pub_employee

============================================================
to_date:
方式一:正确
select to_date('2018-03-08','yyyy-MM-dd') from pub_employee
方式二:
select to_date('2018-03-08 18:55:33','yyyy-MM-dd') from pub_employee
方式三:
select to_date('2018-03-08 18:55:33','yyyy-MM-dd hh24:mi:ss') from pub_employee
使用to_date 返回的都是以下结果:

to_timestamp:
方式一:
select to_timestamp('2018-03-08','yyyy-MM-dd') from pub_employee
方式二:
select to_timestamp('2018-03-08 18:55:33','yyyy-MM-dd') from pub_employee
方式一和二都是以下格式,虽然都是时间戳,但是后面一截是0

方式三:正确
select to_timestamp('2018-03-08 18:55:33','yyyy-MM-dd hh24:mi:ss') from pub_employee

---------------------
作者:大bug
来源:CSDN
原文:https://blog.csdn.net/sky_limitless/article/details/79527665
版权声明:本文为博主原创文章,转载请附上博文链接!
postgresql数据库的 to_date 和 to_timestamp 将 字符串转换为时间格式的更多相关文章
- python 将列表嵌套字典的unicode字符串转换为str格式的字符串的方法
今天在进行django开发的过程中遇到了一个非常棘手的问题, 因为需求原因, 需要将一份数据存为json格式到数据库中, 如下面这种格式: list_1 = [{"name":&q ...
- mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期格式
mysql内置函数,在mysql里面利用str_to_date()把字符串转换为日期格式 示例:分隔符一致,年月日要用%号 select str_to_date('2008-4-2 15:3:28', ...
- 使用ParseExact方法将字符串转换为日期格式
实现效果: 知识运用: DateTime结构的ParseExact方法 public static DateTime ParseExact(string s,string format,IFormat ...
- JQuery 字符串转时间格式
//字符串转时间格式 function getDate(strDate) { var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$) ...
- jansen字符串转换为xml格式
/// <summary> /// json字符串转换为Xml对象 /// </summary> /// <param name="sJson"> ...
- c++ 标准 字符串转换为时间 时间大小比较 判断有效期 简洁办法
c# php delphi java 等各种语言 对字符串转换为日期 然后与当前日期进行比较 是非常容易的 因为有现成的函数可用 标准 c++ 硬是找不到 合适的代码可用 于是 百度了很多 没百出个结 ...
- angularJs中将字符串转换为HTML格式
首先定义一个filter: .filter( 'to_trusted', ['$sce', function ($sce) { return function (text) { return $sce ...
- PHP 将字符串转换为字符集格式UTF8/GB2312/GBK 函数iconv()
iconv()介绍 iconv函数可以将一种已知的字符集文件转换成另一种已知的字符集文件 iconv('要转化的格式',‘转化后的格式’,‘转化的数据’); 但是转化是经常出错,一般需要在转成的编码 ...
- python json格式字符串转换为字典格式
不废话,看代码 #_*_ coding:utf- _*_ import os import json course=open('C:\\Users\\ly199\\Desktop\\list.txt' ...
随机推荐
- 【php设计模式】桥接模式
定义: 将抽象与实现分离,使它们可以独立变化.它是用组合关系代替继承关系来实现,从而降低了抽象和实现这两个可变维度的耦合度. 角色: 抽象化(Abstraction)角色:定义抽象类,并包含一个对实现 ...
- 03 - Mongodb数据查询 | Mongodb
1.基本查询 ①方法find():查询 db.集合名称.find({条件文档}) ②方法findOne():查询,只返回第一个 db.集合名称.findOne({条件文档}) ③方法pretty(): ...
- otool随笔测试
otool 工具 查看库/反编译等二进制信息 1 依赖库查询 otool -L Payload/XXX.app/XXX 2 查看该应用是否砸壳 otool -l Payload/XXX.app/XXX ...
- ESP8266 UDP通信
#include "driver/uart.h" #include "espconn.h" void ICACHE_FLASH_ATTR user_rf_pre ...
- 深入SpringBoot注解原理及使用
首先,先看SpringBoot的主配置类: @SpringBootApplication public class StartEurekaApplication { public static voi ...
- 安卓App自动升级
procedure _InstallApk(Apk: string); var LFile: JFile; LIntent: JIntent; begin LFile := TJFile.JavaCl ...
- 【异常】Cannot run program "git" (in directory "/mnt/software/azkaban-3.79.0"): error=2, No such file or directory
1 安装azkaban异常 cloudera-scm@cdh4 azkaban-3.79.0]$ ./gradlew build -x test Parallel execution with con ...
- 13_Hive优化
Hive优化 要点:优化时,把hive sql当做map reduce程序来读,会有意想不到的惊喜. 理解hadoop的核心能力,是hive优化的根本. 长期观察hadoop处理数据的过程,有几个显著 ...
- YOLO---YOLOv3 with OpenCV安装与使用
Yolo v3+Opencv3.4.2安装记录 @wp20180930 目录 一.环境要求 (1)python版本的查看 (2)opencv版本的查看 二.文件下载 三.数据自测 四.问题与解决 (1 ...
- 内网监控zabbix
告警 告警方式:linkedsee 类型:使用脚本linkedsee.sh [root@zabbix-server ~]# cat linkedsee.sh #! /bin/bash SERVICE_ ...