"TO_DATE" 函数不改变格式
"TO_DATE" function doesn't change format
我的 rails 应用程序中有 SQL 查询,如下所示
sql = "SELECT id,email,first_name,last_name,street_name,street_number,postal_code,city,region,country,to_date(cast(birth_date as TEXT), 'YYYY'),sex,deceased
FROM users
WHERE users.user_role <> 0 AND users.aasm_state = 'complete'
ORDER BY id"
想法是在字段中只显示年份 birth_date。但是使用该函数时,只是列名发生了变化,日期格式仍然保持在(年、月、日)形式。
我使用 PG 并尝试将其重写为 to_date(birth_date::TEXT,'YYYY')
,但它也不起作用。
我认为你想要 date function to_char()
。此函数可用于将日期或时间戳格式化为给定格式的字符串:
to_char(birth_date , 'YYYY')
我的 rails 应用程序中有 SQL 查询,如下所示
sql = "SELECT id,email,first_name,last_name,street_name,street_number,postal_code,city,region,country,to_date(cast(birth_date as TEXT), 'YYYY'),sex,deceased
FROM users
WHERE users.user_role <> 0 AND users.aasm_state = 'complete'
ORDER BY id"
想法是在字段中只显示年份 birth_date。但是使用该函数时,只是列名发生了变化,日期格式仍然保持在(年、月、日)形式。
我使用 PG 并尝试将其重写为 to_date(birth_date::TEXT,'YYYY')
,但它也不起作用。
我认为你想要 date function to_char()
。此函数可用于将日期或时间戳格式化为给定格式的字符串:
to_char(birth_date , 'YYYY')