简体   繁体   中英

SQL date not converting in OPENROWSET query but does work as admin user

select cast('01/05/2014' as date)

returns: 2014-05-01

But I have a CSV file with dates (all in May) written in UK format exactly like the above. In fact, I'm using the top 10 records which all have the date: 01/05/2014

My problem is that when I run the below code, the results are all:

2014-01-05 00:00:00.000 | 2014-01-05 | 2014-01-05 | 2014-01-05 | 2014-01-05 | Jan  5 2014 12:00AM  | 05/01/2014

This is the code:

SELECT top 10
sdispdate
,convert(date, sdispdate, 103)
,cast(sdispdate as date)
,convert(date, cast (sdispdate as date), 103)
,cast(convert(date, sdispdate, 103) as date)
,cast(sdispdate as varchar(50))
,convert(varchar(20), sdispdate, 103)
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\Import;HDR=Yes;FORMAT=Delimited(,)', 'SELECT * FROM [MyDataFile.csv]')

So my problem is that I cannot convert the already UK date into a UK date. I feel that my issue must have something to do with OPENROWSET - any ideas?

UPDATE : I've just run the same query on another SQL 2008R2 server (same as mine) and got the correct result. Both servers show us_english for language and mdy for dateformat when running DBCC USEROPTIONS

UPDATE 2 : I've just run SSMS on my machine as the domain admin and it works... the DBCC USEROPTIONS are identical between me and administrator. I am also a sysadmin. My machine is all Australian with no other regional settings. And if I connect to my other server mentioned in my first update as myself or administrator it works. This can only mean that there is a setting unique to my login on this new server only. What else can I check??? I've been on this issue for over 4 hours!!!

Gordatron was right, ACE was shafting me before SQL got it. This meant I couldn't even make the data SUBSTRING etc.

My solution was to simply add a schema.ini to the same folder as the import file. The contents of the are just:

[MyDataFile.csv]
DateTimeFormat=dd-MMM-yyyy

And that's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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