Importing a datetime field from csv/txt into Access - sql-server-2008

I have the output of a SQL Server 2008 query saved to a text file and I am trying to link it to an Access database (Access 2007-2010).
The text file contains two date columns in the SQL Server 2008 datetime format, like this:
EFFECTIVE START DATE:
---------------------
2013-07-01 00:00:00.000
EFFECTIVE END DATE:
-------------------
2014-06-30 00:00:00.000
In the Import wizard, I click Advanced and change the following:
Code Page = Western European (Window) instead of Unicode (UTF-8)
For the 2 datetime columns:
data type = Date/Time
date order = YMT
Date delimiter = -
Leading zeros in dates = ticked
While still in the import wizard the data looks fine but as soon as the import is finished and I open the table in Access, I see #Num! in the two date columns.
I have tried a combination of settings in the import wizard (leave the code page as Unicode, not change the date order, not ticking the leading zeros in dates, etc) but in the end I had to import the dates as text, which stops me from doing any calculations now.
Many thanks in advance for the help

in the end I had to import the dates as text, which stops me from doing any calculations now.
True, you can't manipulate the text fields as true dates, but you can run a make-table query to convert them to real Date/Time values like so:
SELECT
ID,
CDate(Left([Field1],InStr([Field1],".")-1)) AS Date1,
CDate(Left([Field2],InStr([Field1],".")-1)) AS Date2
INTO SqlDataWithRealDates
FROM SqlData;
The issue with the original import is that Access Date/Time values do not support fractional seconds so Access will never recognize the values as such if they include 00:00:00.000. The above query removes the trailing .000 before passing the strings to CDate().
Another alternative as suggested by Johnny Bones in the comments below is to alter the SQL Server query to use something like CONVERT(VARCHAR(10), [Field1], 101) AS Date1 to remove the time component from the strings that will eventually be imported.

I'm just adding this as an answer, since it ended up being the one that worked:
If you have control over the output from SQL Server, you can change the format of the date field in your view/stored procedure. Something like
SELECT CONVERT(VARCHAR(10), [Field1], 101) AS Date1
That will strip the time off it, and Access should recognize it.

I found if you use "M/d/yyyy HH:mm:ss" as the date format (.NET), Access will import the date/time correctly, at least for if you are in the United States. Note, the hour is the 24 hour time. It won't work in 12 hour time with the AM/PM indicator. This is different than Excel, which does work with the 12 hour time. If you include milliseconds, it won't work. You can leave the time off if you don't need it.

If your output is CSV file, change the output format to "MM/DD/YYYY", and drop the time from the date.
It will import just fine.
If you need to use the time, make another column for it, and strip out the date leaving only the time.
I was able to use hundreds of hours with two places.

Access does import correctly the date and time information in the data. You need not drop (or remove) the time information. I have not tested for fractional seconds. However, if you have duplicates in the data column you may need to fix it and also you should correctly set the date format (YMD, DMY, etc.)
Here is a recent link to a correctly imported text file.
http://hodentekmsss.blogspot.com/2017/02/importing-text-file-into-ms-access.html

I had faced the same issue while importing CSV file with datetime. What I had done to solve this issue was by splitting date and time and merge.
Convert(varchar, yourdatecolum, 101 ) + ' ' + Convert(varchar, yourdatecolumn, 108)

Related

Simple way to unify multiple date formats?

I need to import a large file of csv data into MySQL, and when I attempted to use MySQL's unix_timestamp function to import the dates, about half of the records didn't make it.
As far as I can tell, the datetime values are formatted with either a single first "month" digit or two of them, and the same goes with the day of the month (e.g. 6/6/2014 3:48PM vs. 12/16/2014 3:48PM) This throws off the import completely (well about half of the records won't import).
I'm trying to convert this into a unix_timestamp.
Now I know I could write a script with a regex to fix something like this, but I am wondering is there a simpler way to do a mass import like this? For the record, I am using my text editor to write the sql statements from the csv as "insert into" statements. This is where I tried to use date formatting but it seems to only accept one format.
Any way to do this with such a minor difference in input?
Actually, despite my comment, something like this might work:
COALESCE(STR_TO_DATE(val, "formatcandidate1")
, STR_TO_DATE(val, "formatcandidate2")
, STR_TO_DATE(val, "formatcandidate3")
, STR_TO_DATE(val, "formatcandidate4")
, [etc...]
) AS dateVal
There are online tools to do this kind of stuff
reports.zoho.com is one of them
In this tool you can import data applying a specific date format and skip the other rows.
and you can do the same for all the type of formats that are present in your file
and finally you can export the data with same date format for all the data
ask me any doubts if you have any regarding this :)

Trac Reporting export Change history with date time

am trying to export alle the change history of all my trac tickets via a tab delimited text file export.
I successed so far with the following SQL Statement:
SELECT
id AS Ticket,
tc.author as author,
tc.field as field,
tc.newvalue as comments_new
from ticket t
LEFT JOIN ticket_change tc ON ( t.id=tc.ticket )
But I need also the time and date of each entry. There is a field time in the table ticket_change but it is only the time without the date.
Seems like I have to use some functions within the SQL to get what I need. Can anybody help me to get there?
Every Help is very appreciated.
Best Regards
Ali
The time field is an integer representing epoch microseconds. You can use the Trac utility trac.util.datefmt.from_utimestamp to convert the value to datetime format. That only helps if you are working in Python though. In other languages, you can use recipes for converting from unix seconds. Note you must divide the value in Trac by 1e6 first, to convert from microseconds to seconds.

How to convert a string to date and extract values in Access query

I'm using Access DB 2007 - 2010; I've tried to import many CSV files but the timestamp column keeps failing to import correctly.
So I linked all of the CSV's to an Access DB and I'm trying to query all of the tables.
I'm trying to extract the year and day of the year from the time stamp (which is currently a string)
I'm trying to combine the Format with datepart functions and it keeps failing. (it just says error in the table)
The format function by itself works but I can't combine it with anything.
I'm basically trying to do this:
select datepart("y", Format(gmt, "dd-mmm-yyyy hh:nn:ss")) as DOY from Table1;
but it fails. I've also tried CDate and DateValue in different combinations but it all fails.
Does anyone know how to get this to work?
UPDATE
The format function isn't doing anything. The text remains the same no matter how I try to format it.
Here's a datetime sample: 05-Dec-2008 13:40:01.955
Access can't cope with the milliseconds in your date strings.
Use Left() to exclude them and feed the resulting substring to CDate().
SELECT CDate(Left(gmt, 20)) AS date_from_string
FROM Table1;
Once you have a valid Date/Time value, you can use Year(<Date/Time value>) or DatePart("yyyy", <Date/Time value>) to extract the year. And DatePart("y", <Date/Time value>) will give you the day of the year.
Just solve this issue, here is my code for your reference:
update tablename
set date=cdate(format(left(gmt,4)&"-"&right(gmt,2),"yyyy-mm"))

Importing .csv and converting to dates in mysql

I am trying to import a CSV file with date information in the format MMM-YY (e.g., Jan-92). My ultimate goal is to have the information in a date format in SQL. The format doesn't matter that much to me, but I was thinking something like dd-mm-yy (e.g., 16-01-92). I've tried a lot and looked around the forum, but can't figure it out.
Right now, I am loading the data from the CSV into a column "period_month" as a VARCHAR field.
Then, my conversion code looks like
UPDATE PERIODS
SET period_month = DATE(str_to_date(period_month,'%M-%Y'));
I end up with a field of VARCHAR type in the format yyyy-mm-dd, with the dd field set to '00'. (e.g., 1992-01-00)
I really just want this to be in a date format that I can export to another program like R for analysis.
Any help appreciated.
try
UPDATE PERIODS
SET period_month =DATE(str_to_date('Jan-92','%M-%Y')+1)
or
UPDATE PERIODS
SET period_month =DATE(str_to_date('period_month','%M-%Y')+1)
use date_format function of mysql query

MySqlImport - Import a date field not in the proper format

I have a csv file that has a date field in a format like (among other fields):
17DEC2009
When I do a mysqlimport, the other fields are imported properly, but this field remains 0000-00-00 00:00:00
How can I import this date properly? Do I have to run a sed/awk command on the file first to put it into a proper format? If so, what would that be like? Does the fact that the month is spelled out instead of a number matter?
STR_TO_DATE() enables you to convert a string to a proper DATE within the query. It expects the date string, and a format string.
Check the examples in the manual entry to figure out the correct format.
I think it should be along the lines of %d%b%Y (However the %b is supposed to produce Strings like Dec instead of DEC so you will have to try out whether it works).
I had this issue in the past. What I had to do was to utilize LOAD DATA and set the appropriate expression here -
[SET col_name = expr,...]
http://dev.mysql.com/doc/refman/5.1/en/load-data.html
Here is the approach I took to solve similar problem. My use case was bit complex with so many columns, but making here simple to present the solution.
I have Persons table with (Id int autogen, name varchar(100),DOB date), and few million of data(name,DOB) needs to be populated from CSV file.
Created additional column in persons table with name like (varchar_DOB varchar(25)).
Imported data using mysqlimport utility into columns(name,varchar_DOB).
Executed update query that updated DOB column using str_to_date(varchar_DOB,'format') function.
Now, I have expected data populated DOB column.
The same logic could be applied in doing even other kind of data formatting like double,time_stamp etc.