Tim Coker
Tim Coker

Reputation: 6524

Comparing date value from DB with date literal

I'm getting a date value out of a DB2 database using ADODB objects. This date is sometimes set to 1/1/0001. This is an invalid datetime in SQL Server. I need to check for this value and set it to a valid SQL Server date (1/1/1900 for example). The object coming from the ADODB looks to be a time.struct_time. What's the best way get 1/1/0001 as a time.struct_time object and check it for equality to the same value coming from the DB2 database?

EDIT:

So I figured out a bit more of what's going on. I'd already tried what d0nut suggested, but the comparison still failed. When printing what time.strptime('1/1/0001', '%d/%m/%Y') returns, you get 1/1/2001, not 1/1/0001. When I try to call time.asctime(timeFromDb2) (even with valid date values from the DB), I get this exception message: argument must be 9-item sequence, not time. My guess is that the time object I'm getting from the DB is invalid, but because the script in question does nothing with it besides pass it on to another ADODB Recordset object connected to SQL Server, everything is working. Does anyone have any suggestions on where to go from here? The code that retrieves the date from DB2 and stores it in the SQL Server DB is pasted below. This has been working for years until today when we started getting a value of 1/1/0001 from DB2. the Inventory and Coils objects are both created via a call to win32com.client.Dispatch("ADODB.Recordset")

self.Inventory.Fields("promised_date").Value   = self.Coils.Fields("csaxdz").Value

Upvotes: 1

Views: 630

Answers (2)

agf
agf

Reputation: 176930

You should either be able to convert it to a string:

time.strftime('%d/%m/%Y', badtimeobj)

and compare it that way, or

try:
    time.asctime(timeFromDb2)
except:
    invalid = True
else:
    invalid = False

or the equivalent, if all other dates work in asctime.

Upvotes: 1

d0nut
d0nut

Reputation: 610

>>> time.strptime('1/1/0001', '%d/%m/%Y')
time.struct_time(tm_year=1, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)

That will get you a struct which you should be able to easily compare.

Upvotes: 0

Related Questions