admin管理员组文章数量:1125925
I'm working with a table called 'support', which has the field 'creation_date', which is a date type.
My goal is to convert creation_date to a text string in the format 'YYYY-MM-DD'.
When I run this code:
SELECT TO_CHAR(creation_date, 'YYYY-MM-DD') AS creation_date
FROM support
The output is still displaying in the viewer as "DATETIME" rather than as text:
If I use a different character (i.e., not dash) as separators, it outputs as expected (i.e., text). For example,
SELECT TO_CHAR(creation_date, 'YYYY~MM~DD') AS creation_date
FROM support
output like '2023~01~01', etc.
Funny enough, if I don't use any separators, the output is integer. For example,
SELECT TO_CHAR(creation_date, 'YYYYMMDD') AS creation_date
FROM support
outputs as '20230101', etc., which is displayed in the viewer as "INTEGER".
Have I misunderstood something about how to_char works? I would think it's output should always be text.
For reference, I'm running PostgreSQL through DataCamp's online learning platform. I suspect this issue may stem from some quirk of DataCamp, but I'm not sure.
I'm working with a table called 'support', which has the field 'creation_date', which is a date type.
My goal is to convert creation_date to a text string in the format 'YYYY-MM-DD'.
When I run this code:
SELECT TO_CHAR(creation_date, 'YYYY-MM-DD') AS creation_date
FROM support
The output is still displaying in the viewer as "DATETIME" rather than as text:
If I use a different character (i.e., not dash) as separators, it outputs as expected (i.e., text). For example,
SELECT TO_CHAR(creation_date, 'YYYY~MM~DD') AS creation_date
FROM support
output like '2023~01~01', etc.
Funny enough, if I don't use any separators, the output is integer. For example,
SELECT TO_CHAR(creation_date, 'YYYYMMDD') AS creation_date
FROM support
outputs as '20230101', etc., which is displayed in the viewer as "INTEGER".
Have I misunderstood something about how to_char works? I would think it's output should always be text.
For reference, I'm running PostgreSQL through DataCamp's online learning platform. I suspect this issue may stem from some quirk of DataCamp, but I'm not sure.
Share Improve this question edited Jan 9 at 3:12 Taylan Morcol asked Jan 9 at 2:38 Taylan MorcolTaylan Morcol 236 bronze badges 11 | Show 6 more comments1 Answer
Reset to default 0error : outputs as '20230101', etc., which is displayed in the viewer as "INTEGER" - (This may be due to Viewer Tool Interpretation, where the tool misinterprets string outputs containing only numeric characters as integers)
sample test for your reference:
1. SELECT id, creation_date, TO_CHAR(creation_date,'YYYY-MM-DD') AS formatted_date FROM support;
2. SELECT id, creation_date,TO_CHAR(creation_date, 'YYYYMMDD') AS formatted_date FROM support;
本文标签: SQL tochar not always returning text (PostgreSQLDataCamp)Stack Overflow
版权声明:本文标题:SQL: to_char not always returning text (PostgreSQL, DataCamp) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736677027a1947235.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
pg_typeof()
for each. ∀, typeTEXT
was returned. Also ran it on laptop (W2019 Server, PG17 -psql
- I believe that db<>fiddle usespsql
as its back-end) - same! Something is wrong with the DataCamp tool! Finally, I would strongly recommend against using anything butDATE
to store DATE fields - you can confuse the optimiser and it's not good practice and try performing date arithmetic or extracting useful data (Year, Month, Day...) or adding anINTERVAL
toTEXT
- nightmare! – Vérace Commented 2 days ago