在postgres中将日期格式从varchar更改为yyyy-mm-dd

时间:2018-01-16 07:32:35

标签: sql postgresql

SELECT
  to_char("date", 'YYYY/MM/DD') 
  "public".teacher_details.teacher_id,
  "public".teacher_details.first_name,
  "public"."TblFacultyMaster"."MastCode",
  "public"."TblFacultyMaster"."MastName",
  "public"."TblFacultyMaster"."DOB",
  "public".teacher_details.dob
FROM
  "public".teacher_details
INNER JOIN "public"."TblFacultyMaster" ON "public"."TblFacultyMaster".teacher_id = "public".teacher_details.teacher_id

我正在尝试将日期格式从dd / mm / yyyy转换为yyyy-mm-dd,类型为varchar,因为我的TblFacultyMaster表格格式为yyyy-mm-dd,而dd / mm / yyyy格式为teachers_deatil表 我想匹配常见的DOB,但两个表格中的格式不同

请帮助

提前致谢

3 个答案:

答案 0 :(得分:1)

datecolumn无法将字符串转换为字符串,尝试使用日期对varchar SELECT to_char("date"::date, 'YYYY/MM/DD') as date, "public".teacher_details.teacher_id, "public".teacher_details.first_name, "public"."TblFacultyMaster"."MastCode", "public"."TblFacultyMaster"."MastName", "public"."TblFacultyMaster"."DOB", "public".teacher_details.dob FROM "public".teacher_details INNER JOIN "public"."TblFacultyMaster" ON "public"."TblFacultyMaster".teacher_id = "public".teacher_details.teacher_id 进行类型转换,我想“日期”是您的那一列,请尝试  如下所示:

[tooltip]:before {
  content: attr(tooltip);
  position: absolute;
  opacity: 0;
  transition: all 0.5s ease;
}

[tooltip]:hover:before {
  opacity: 1;
  color: #ffffff;
  background: #333333;
  padding: 10px;
}





/* ################ */
/* No need for this */
/* ################ */

div {
  background: cyan;
  margin: 20px;
  padding: 10px;
}

body {
  background: white;
}

答案 1 :(得分:0)

SELECT
  to_char(to_date(dob, 'dd/mm/yyyy'),'YYYY/MM/DD'),  
  "public".teacher_details.teacher_id,
  "public".teacher_details.first_name,
  "public"."TblFacultyMaster"."MastCode",
  "public"."TblFacultyMaster"."MastName",
  "public"."TblFacultyMaster"."DOB",
  "public".teacher_details.dob
FROM
  "public".teacher_details
INNER JOIN "public"."TblFacultyMaster" ON "public"."TblFacultyMaster".teacher_id = "public".teacher_details.teacher_id

未经测试

答案 2 :(得分:0)

使用to_date()函数时,PostgreSQL会自动将其带到yyyy-MM-dd。见下文:

select to_date(dob, 'dd/mm/yyyy') as myDate;

这将产生以下结果:

enter image description here

我希望有所帮助。欢呼声。

编辑:

好的,我明白你要做的是什么,也许这会解决问题:

SELECT
  to_date("public".teacher_details.dob, 'dd/MM/yyyy'),
  "public".teacher_details.teacher_id,
  "public".teacher_details.first_name,
  "public"."TblFacultyMaster"."MastCode",
  "public"."TblFacultyMaster"."MastName",
  "public"."TblFacultyMaster"."DOB",
  "public".teacher_details.dob
FROM
  "public".teacher_details
INNER JOIN "public"."TblFacultyMaster" ON "public"."TblFacultyMaster".teacher_id = "public".teacher_details.teacher_id
WHERE to_date("public".teacher_details.dob, 'dd/MM/yyyy') = "public"."TblFacultyMaster".dob