[英]SQL Server - How to convert varchar to date
I have a table containing StartDate in the format dd/mm/yyyy and yyyy-mm-dd. 我有一个包含StartDate的表,格式为dd / mm / yyyy和yyyy-mm-dd。 I want to convert this varchar column to DATE type in the format DD/MM/YYYY. 我想将此varchar列转换为DD / MM / YYYY格式的DATE类型。
I have tried the below. 我已经尝试了以下。
select CONVERT(varchar(20),StartDate,103) AS [FormattedDate]
and 和
CONVERT(VARCHAR(20),(CAST([StartDate] AS DATE)),103)
I get the error -Conversion failed when converting date and/or time from character string. 我从字符串转换日期和/或时间时收到错误-转换失败。
Pls suggest. 请建议。
SQL Server is actually quite good about figuring out formats for a date conversion with no formatting argument. 对于没有格式参数的日期转换,SQL Server实际上非常有用。 However, it is going to assume MM/DD/YYYY
for the second format and generate an error. 但是,它将假定MM/DD/YYYY
为第二种格式,并产生错误。
So, you can use try_convert()
and coalesce()
: 因此,您可以使用try_convert()
和coalesce()
:
select coalesce(try_convert(date, startdate, 103),
convert(date, startdate)
)
Here is a SQL Fiddle. 这是一个SQL Fiddle。
Then, you should go into your data and fix the column. 然后,您应该进入数据并修复该列。 Here is one method: 这是一种方法:
update t
set startdate = coalesce(try_convert(date, startdate, 103),
convert(date, startdate)
);
alter table t alter column startdate date;
You can add additional formatting for the result set by turning the date back into a string, using convert()
. 您可以使用convert()
日期改回字符串,从而为结果集添加其他格式。
if you only have the date string in dd/mm/yyyy or yyyy-mm-dd 如果您只有日期字符串为dd / mm / yyyy或yyyy-mm-dd
select case when substring(StartDate, 3, 1) = '/'
then convert(date, StartDate, 103)
else convert(date, StartDate, 121)
end
Here's an example that first tries to convert the VARCHAR from a 'yyyy-mm-dd' format to the 'dd/mm/yyyy' format. 这是一个示例,首先尝试将VARCHAR从“ yyyy-mm-dd”格式转换为“ dd / mm / yyyy”格式。
If that doesn't work out, then it just assumes it's already in the 'dd/mm/yyyy' format. 如果仍无法解决问题,则仅假设它已经是“ dd / mm / yyyy”格式。
And then defaults to the first 10 characters from the string. 然后默认为字符串中的前10个字符。
declare @TestTable table (StartDate varchar(10), DateFormatUsed varchar(10));
insert into @TestTable (StartDate, DateFormatUsed) values
(convert(varchar(10),GetDate() ,103), 'dd/mm/yyyy')
,(convert(varchar(10),GetDate(), 20), 'yyyy-mm-dd')
;
select t.*,
coalesce(convert(varchar(10), try_convert(date,StartDate,20),103), left(StartDate,10)) as [FormattedDate]
from @TestTable t;
But try_convert is only available since MS SQL Server 2012. 但是try_convert仅在MS SQL Server 2012之后可用。
For MS SQL Server 2008 we can use a CASE WHEN with a LIKE to check the format. 对于MS SQL Server 2008,我们可以使用CASE WHEN和LIKE来检查格式。
declare @TestTable table (StartDate varchar(30), DateFormatUsed varchar(30));
insert into @TestTable (StartDate, DateFormatUsed) values
(convert(varchar(10),GetDate(), 103), 'dd/mm/yyyy')
,(convert(varchar(10),GetDate(), 20), 'yyyy-mm-dd')
,(convert(varchar(19),GetDate(), 20), 'yyyy-mm-dd hh:mi:ss')
;
select t.*,
(case
when StartDate like '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]%'
then convert(varchar(10), convert(date, left(StartDate, 10), 20), 103)
else left(StartDate, 10)
end) as [FormattedDate]
from @TestTable t;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.