简体   繁体   English

将varchar转换为日期103时出现SQL格式问题

[英]SQL Formatting issue when converting varchar to date 103

I have a date field I pull in from excel as a varchar type and I currently convert it using this line: 我有一个日期字段,我从excel中提取了一个varchar类型,目前使用此行将其转换:

convert(date,[Appt Date],103) as [Appt date]

this works but the result is a date in the following format: 这可行,但结果是采用以下格式的日期:

YYYY/MM/DD YYYY / MM / DD

According to other sites the 103 format at the end of the line should convert this varchar to UK formatting ( DD/MM/YYYY ). 根据其他站点,该行末尾的103格式应将此varchar转换为UK格式(DD / MM / YYYY)。

Can anyone advise why this is not happening? 谁能告诉我为什么这种情况没有发生?

By default, MSSQL server store dates in YYYY/MM/DD . 默认情况下,MSSQL服务器将日期存储在YYYY/MM/DD You can only change the language on the whole server, not individual databases. 您只能更改整个服务器上的语言,而不能更改单个数据库上的语言。

set language 'british english'

run this first before inserting. 在插入之前先运行此命令。

or another alternative is to use SET DATEFORMAT ( from MSDN ) 或另一种替代方法是使用SET DATEFORMAT来自MSDN

declare @dates table (orig varchar(50) ,parsed datetime)

SET DATEFORMAT ydm;

insert into @dates
select '2008-09-01','2008-09-01'

SET DATEFORMAT ymd;
insert into @dates
select '2008-09-01','2008-09-01'

select * from @dates

It should be VARCHAR not DATE 应该是 VARCHAR而不是 DATE

 
 
 
  
  SELECT convert(VARCHAR(20),[Appt Date],103) as [Appt date]
 
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM