SQL 将整数转换为日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23958852/
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
Converting Integer to Date
提问by user3109653
I am trying to convert an integer datatype into a date. Here is the coding that I have so far:
我正在尝试将整数数据类型转换为日期。这是我到目前为止的编码:
SELECT CONVERT(column_name, yyyymm) from table_name;
What do I need to add?
我需要添加什么?
回答by Kaf
Assuming your integer column (say, your_column) is representing year and month in yyyymm
format, this should work.
假设您的整数列(例如 your_column)以yyyymm
格式表示年和月,这应该有效。
First, convert your int
column to a varchar
and then add '01' to make it yyyymmdd
(ISO Format), then convert to datetime/date
.
首先,将您的int
列转换为 a varchar
,然后添加“01”使其成为yyyymmdd
(ISO 格式),然后转换为datetime/date
.
SELECT CONVERT(date, CONVERT(varchar(6), your_column) + '01') myDate
FROM TableName
回答by user2832577
try
尝试
SELECT CONVERT(date, convert(char(8),20180521))
or
或者
select FORMAT(CONVERT (date,convert(char(8),20180521)),'dd-MMM-yyyy')
回答by T McKeown
select DateAdd(second, someNbrCol, '1970-01-01')
The last argument you would have to provide as the starting date.
您必须提供的最后一个参数作为开始日期。