SQL DB2 从 YYYYMMDD 转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30530112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DB2 Convert from YYYYMMDD to Date
提问by Chry007
I have a column that stores a date as char in the format 'YYYYMMDD'. Now I want to convert it to a real date. I tried
我有一列以“YYYYMMDD”的格式将日期存储为字符。现在我想将其转换为真实日期。我试过
select cast (DATEFIELD as DATE) as MyDate
But it only returns the old YYYYMMDDformat labeled as 'DATE'. What am I doing wrong?
但它只返回YYYYMMDD标记为“DATE”的旧格式。我究竟做错了什么?
回答by Stavr00
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as "MyDate"
回答by Gnqz
Simply convert it.
简单地转换一下。
SELECT TIMESTAMP_FORMAT("DATEFIELD",'YYYYMMDD') as MyDate
FROM <your_table>
回答by Matt
Use the CONVERTfunction and the style 112to get the output in YYYYMMDD
使用CONVERT函数和样式112获取输出YYYYMMDD
SELECT CONVERT(DATEFIELD, 112) as MyDate
FROM yourtable
Or style 100for mon dd yyyy hh:mi
或样式100的mon dd yyyy hh:mi
SELECT CONVERT(DATEFIELD, 100) as MyDate
FROM yourtable

