473,416 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

How to convert odd date format from database

!NoItAll
297 100+
I am converting about 600-thousand records from Microsoft SQL. The original database designer is, lets just say, "uncooperative."
I have several fields that represent date, times, etc. I cannot figure out how the date is stored.
  • I know the field [a varbinary(16)]
  • I can get the value
  • I have several known date sample records

Here is an example:
The following record I know for certain corresponds to the date of 8/9/2002 (month-day-year).
The value in the date field (which is a varbinary(16)) in the database is:

0xDD3FC20120A42A940000000000000000 - I know this is the date field.

I have tried the following:

Select Convert(char(16), date) from...

I then get the number with the following loop
(looking only at the 4 most significant bytes)

Expand|Select|Wrap|Line Numbers
  1.     For I = 4 To 1 Step -1
  2.  
  3.         lClockTicks = lClockTicks * 256#
  4.         lClockTicks = lClockTicks + Asc(Mid$(xDate, I, 1&))
  5.  
  6.     Next I
  7.  
I get 19,742,630 hoping to find something like a Unix Epoch, but that number does not represent the correct date when treated like an Epoch.

Sooo - I am lost.

Anyone have any advice?
Oct 8 '10 #1
8 3346
NeoPa
32,556 Expert Mod 16PB
I looked for various patterns in the data but saw none. Clearly your first task is to identify the form the date is stored in. Without this there can be no progress elsewhere.

The date (9th August 2002) in an Access date format resolves to an integer value of 37477, which when converted to Hex shows as &H9265. I couldn't see this anywhere in the hex characters shown, nor could I find anything that may resolve to 2002 (or &H07D2). Sorry.
Oct 9 '10 #2
!NoItAll
297 100+
Thanks for looking at my problem Neopa!
I figured out that the problem is that the data being returned by SQL is actually incorrect! I haven't figured out that problem yet - I will likely start a new thread for that. Somehow the data in the date field is being returned incorrectly.
Here's what I mean:
In the query analyzer the data shows up as:

0xDD3FC20120A42A940000000000000000

However with the following select statement:

Expand|Select|Wrap|Line Numbers
  1. select convert(char(8), itemdate) as mydate from item_data where itemid=12345678
  2.  
Instead of getting

<DD><3F><C2><01><20><A4><2A><94>

I get this mess

<A6><3F><2D><01><20><F1><2A><F6>

I think it has something to do with an incorrect collation setting because every character in the extended ASCII range (>7F) is returned translated. All of the characters in the lower ASCII range (01 to 7F) are returned properly.
The current Collation name is: SQL_Latin1_General_CP850_CI_AS
I think I need something that is designed for UTF8 - but I'm not sure...

By the way - the date is calculated as the number of seconds since 1/1/1601... Sheesh - it seems to me that we are lucky to know the actual year of any documents that old - let alone the date/time/second.
Des
Oct 11 '10 #3
NeoPa
32,556 Expert Mod 16PB
!NoItAll:
By the way - the date is calculated as the number of seconds since 1/1/1601... Sheesh - it seems to me that we are lucky to know the actual year of any documents that old - let alone the date/time/second.
:-D

Is this integral or floating point. I couldn't get the number to resolve to an integer at all using Access. I remembered it is stored as Big-Endian, but it couldn't handle that many hex digits. The number of seconds since 1/1/1601 would be circa 12,673,670,400 or &H2E368DD0. It doesn't seem to fit the data you have, whichever way the data were read. I can only assume this is then stored as a long (high precision) floating point value.
Oct 11 '10 #4
NeoPa
32,556 Expert Mod 16PB
!NoItAll:
convert(char(8), itemdate)
I expect your conversion is due to using char instead of something more concrete. If you're still interested in seeing the data this way I suggest you have a look for another type to convert it to. I'm afraid I don't know them well enough from here to say which would be better, but I could look for you if it were still important.
Oct 11 '10 #5
!NoItAll
297 100+
The problem is that the number is too big for VB6. VB6 lacks the precision to convert this directly and calculate the date. Rounding causes VB6 to be way off. Because of this I first pull the field in as a series of chars. Taking the first 4 chars I convert those to a number and using a calculated divisor (201.165676116943) I am able to get the year/month/day. The last two bytes will then get me the time of day in seconds in a separate calculation.
It took a lot of trial and error to get to this point - but it has actually proven reliable.
So - I do need those chars - but the issue here is why are certain chars (chars > 7F) being "translated" - how to I tell SQL to stop doing that? This wasn't happening is several earlier projects - but now it is. I still suspect it has something to do with collation. How can I change the collation?
Oct 12 '10 #6
NeoPa
32,556 Expert Mod 16PB
I don't doubt you need the data. What I am suggesting is that char() is probably not the type to convert it to (for exactly the reason you're falling over). Have you done as I suggested and explored alternatives from somewhere like Data Types (Transact-SQL)?
Oct 12 '10 #7
!NoItAll
297 100+
I see where you are going - I think...
There are several other conversion types to explore. I have tried all of the numeric ones in my SQL (decimal, numeric, float, bigint, etc) but all produce "0" (Zero) or an error.
There is a datetime - but that always returns "1901-01-01 00:00:00" - which I suspect is a default when it gets confused. I tried DateTimeOffset - but that just returns an error and timestamp returns 4 question marks.
I did figure out how to change the collation!
This has fixed my problem. I executed the following commands:

select * from ::fn_helpcollations()

This gave me a list of all the available collations. I chose a straight binary one - assuming no translation would take place with this in place.

Latin1_General_BIN

So I then executed the following command (available from SQL 2000 and up)

alter database [my_database] collate Latin1_General_BIN

Voila! I am now getting the chars untranslated and my calculations now produce the correct dates.

I think you are right though - there is likely a much better way to do it then my hack hack hack (it gets a three hack award). With someone skilled in SQL - it's quite possible, I'm sure, to create a function to return the date as an actual ANSI date. I will explore that when I take my SQL Class...
Oct 12 '10 #8
NeoPa
32,556 Expert Mod 16PB
!NoItAll:
There is a datetime - but that always returns "1901-01-01 00:00:00" - which I suspect is a default when it gets confused.
That's simply the date reflected by the value zero (0).

I was thinking along the lines of bit fields, but I lost my job a few weeks ago so don't have access to any SQL testing any more I'm afraid. It's frustrating, as I may have to stop helping in here and go back to focusing exclusively on the Access stuff.
Oct 12 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: ohaya | last post by:
Hi, I'm working with a date string with a format as follows: Sat Dec 25 21:32:59 EST 2004 and I want to compare it to another date string of the format: 7/28/2004
2
by: Annie D via AccessMonster.com | last post by:
Hi, I am using the following SQL statement courtesy of access monster user: 'Terry Kreft ' to convert dates from a text format (20020331 00:00:00) to a recognised date/time format (31/03/2002)....
5
by: dubing | last post by:
Hello everyone, There is a field of DATETIME type in our Access 2000 database. PHP is used to query the data and display the query results on the web. Does Access provide any function that can...
1
by: prabhunew2005 | last post by:
I am using postgres,php in web designing. I have to convert date entered in MM-DD-YYYY format to YYYY-MM-DD format. I have to use resulting date format in my search sql. I used todate method. But i...
3
by: AA Arens | last post by:
I created at a Word filke containing field and the data is from my database (via ODBC). One of the fields is Date. The format in the database is dd-mm-yyyy (I checked it in VB code), but in the...
2
by: ManickavasagamSathiskumar | last post by:
hai I have one text box in asp page it for date entry field(dd/mm/yyyy)format user entered. But when i store in database it will store (mm/dd/yyyy) format how to change in asp.
7
chandru8
by: chandru8 | last post by:
I read the date from Notepad and convert it into date using Dateserial function. I try to convert it into my desired format like fomat( ,"dd/mm/yy") but I am unable to do that. Dim fs As New...
4
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it...
3
Chittaranjan
by: Chittaranjan | last post by:
Hi All, I am not sure how to change the date format in PostgreSQL. I have the dates stored in the database as yyyy-mm-dd HH:MM:SS+TimeZone to get the GMT time (Ex: 2008-02-28 14:03:23+05:30). But...
2
by: anoop s | last post by:
Hi, In my web application I want fetch data from fixed width text file. I am using OLEDB connection for retrieving data. But the problem is the date format is dd/mm/yyyy in text file. Can I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.