在SQL Server中将DDMMYYYY转换为YYYYMMDD日期格式? [英] Convert DDMMYYYY to YYYYMMDD date format in sql server?

查看:209
本文介绍了在SQL Server中将DDMMYYYY转换为YYYYMMDD日期格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表中的日期为"26052016" ,格式为 DDMMYYYY 我想将此日期转换为"YYYYMMDD" 格式.

I have a date in table as "26052016" in format DDMMYYYY I want to convert this date to "YYYYMMDD" format.

任何想法
我尝试过这种方法

Any idea
I have tried this method

select CONVERT(varchar(8),[doc-date],112) FROM C034_PDK_ParallelBillingSourceExtract

但这给了我相同的日期.

But this is gives me the same date as a result.

请帮助我

推荐答案

我可以找到这种方式,我不知道是否存在其他方式.

I can find this way, i don't know if any other way exist or not..

declare @date nvarchar(max)='01052016'

select convert(varchar(8),cast(CONCAT(SUBSTRING(@date,3,2),'/',SUBSTRING(@date,1,2),'/',SUBSTRING(@date,5,4)) as date),112)as [YYYYMMDD]

清除代码:

declare @date nvarchar(max)='01052016'
declare @date1 date
set @date1 =cast(CONCAT(SUBSTRING(@date,3,2),'/',SUBSTRING(@date,1,2),'/',SUBSTRING(@date,5,4)) as date)

select convert(varchar(8),@date1,112)as [YYYYMMDD]

如果您使用的是 Sql版本<2012 ,那么您需要跳过 CONCAT 并使用 + 进行字符串补充.

If you are using Sql version< 2012 then you need to skip CONCAT and use + for string concatination.

这篇关于在SQL Server中将DDMMYYYY转换为YYYYMMDD日期格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆