PostgreSQL 9.3:将“DD-MM-YYYY”转换为“YYYY-MM-DD”

时间:2015-12-15 06:50:19

标签: postgresql postgresql-9.3

我有以下日期的varchar类型,可以在我的函数中转换为格式YYYY-MM-DD

我将v_Date作为参数传递给函数,而函数内部则希望执行转换。

脚本

DO
$$
DECLARE v_Date varchar := '16-01-2010 00:00:00';
    v_SQL varchar;
BEGIN
    v_SQL := 'SELECT to_date('''|| v_Date ||''',''YYYY-MM-DD'')';

    RAISE INFO '%',v_SQL;

    EXECUTE v_SQL;
END;
$$

上面的脚本准备了以下脚本:

INFO:  SELECT to_date('16-01-2010 00:00:00','YYYY-MM-DD')

这给了我结果:

0021-07-02

但预期结果应为:

2010-01-16

1 个答案:

答案 0 :(得分:5)

  

选择to_date(' 16-01-2010 00:00:00',' YYYY-MM-DD')

您的模式- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 5; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 4;//add your own number, might be totalNumberOfNewsItems/5 } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { int reqIndex = indexPath.section*(5) + indexPath.row; // get data from array like news[reqIndex]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // return your ad view based on section value } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 44; // change to your required value } 与输入yyyy-mm-dd不匹配。您需要使用dd-mm-yyyy hh:mi:ss将该输入转换为正确的日期(您可以省略其他时间部分)

dd-mm-yyyy

SELECT to_date('16-01-2010 00:00:00','dd-mm-yyyy'); 值的格式化输出取决于您的SQL客户端和(或)LC_TIME的当前设置。为确保您获得date输出,您必须使用yyyy-mm-dd格式化结果date

to_char()
相关问题