这是我的数据集
我正在创建一个存储过程,该列在varchar中。我试图转换为日期时间,但没有成功做到这一点...我试图提取两个数字的月份,例如。 01,02,03做一个这样的表。
CREATE Proc qe
AS
BEGIN
SELECT LEFT(CREATED_TIME,4) AS 'YEAR' , COUNT(*) AS 'NO OF POSTS'
FROM Sheet1$
GROUP BY LEFT(CREATED_TIME,4)
/**SELECT LEFT(CREATED_TIME,7 (RIGHT CREATED_TIME,19)) AS 'MONTH',
COUNT(*) AS 'NO OF POSTS'
FROM Sheet1$
GROUP BY LEFT(CREATED_TIME,7 (RIGHT CREATED_TIME,19))**/
END
EXEC qe
如何提取月份,例如。 01,02,03它留在varchar?没有转换?我非常感谢你的帮助!!!
答案 0 :(得分:4)
如果这是一个字符串,你可以这样做:
SELECT SUBSTRING(CREATED_TIME,6,2);
仅仅是因为完整性:要从日期中得到两位数的月份,你可以这样:
SELECT REPLACE(STR(MONTH(GETDATE()),2),' ','0');
答案 1 :(得分:2)
您可以使用zero padding function in SQL Server 请检查引用的SQL教程
select dbo.udfLeftSQLPadding( DATEPART(mm,GETDATE()),2,'0')
答案 2 :(得分:1)
SELECT MONTH(CAST(create_time AS datetime))
答案 3 :(得分:1)
这样的事情:
select right('0'+cast(month_num as varchar(2)) ,2) as month, month_count
from
(
select datepart(month, cast(replace(created_time, '+0000','') as datetime)) as month_num
, COUNT(*) AS 'NO OF POSTS' as month_count
FROM Sheet1$
GROUP BY datepart(month, cast(replace(created_time, '+0000','') as datetime))
) temp