SQLServer Timestamp compare to Date String

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
danjm
Participant
Posts: 34
Joined: Tue Sep 09, 2003 8:44 am
Location: Canada
Contact:

SQLServer Timestamp compare to Date String

Post by danjm »

Good morning everyone. I have someone here experiencing difficulty with dates. They want to compare a timestamp from a SQLServer DB to a date string (with no time). IE: If <timestamp field> > than 'June 30 1999' then...

Anyone have a straight forward solution to handling the timestamp field to remove the 'time' portion and leave only the 'date' portion that will compare properly?
Dan Marshall
Alberta Education
Edmonton, AB
kcbland
Participant
Posts: 5208
Joined: Wed Jan 15, 2003 8:56 am
Location: Lutz, FL
Contact:

Post by kcbland »

Always use a date format of YYYY-MM-DD HH24:MI:SS.000 throughout your ETL. As soon as a date comes into your job, you should make sure it's in this form. This allows you to greater than, equalities, and less than comparisons because it's a lexicographic comparison.

Since DataStage Server has no internal datatypes, you can use string manipulations. Therefore, to portion off the date, its simply LEFT(columnvalue, 10). Your lexicographic comparisons still work!
Kenneth Bland

Rank: Sempai
Belt: First degree black
Fight name: Captain Hook
Signature knockout: right upper cut followed by left hook
Signature submission: Crucifix combined with leg triangle
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Your problem really is that the form "June 30 1999" is not actually a date, but a character string.

So you're not comparing apples with apples.

In string terminology, you're trying to compare, for example:
"1999-08-31 15:03:40.024" > "June 30 1999"

Ask yourself, what is the result of this string comparison?

So you need to get this date into a consistent format. The ISO 8601 standard specifies a good one. For example:

Code: Select all

If Oconv(Iconv(TheDate, "MDY"), "D-YMD[4,2,2]") > Left(TheTimeStamp,10) Then ... Else ...
Of course, you could turn the Oconv(Iconv...)) into a Transform, so that it is a re-usable component.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
mhester
Participant
Posts: 622
Joined: Tue Mar 04, 2003 5:26 am
Location: Phoenix, AZ
Contact:

Post by mhester »

Good points Ray and Ken,

I think getting the date into a consitent format is the key, then as Ken states a "lexicographic" comparison can be made. Do beware though that the two strings -

10-Mar-2003

and

10-MAR-2003

are not the same with the first being greater than the second in a comparison. Decimal "a" is greater than "A" and this is what the comparison is really about - the ASCII or machine collating sequence.

For all those old Unix people, remember that the man pages for sort used to contain verbiage about the sort being lexicographic. Most versions of Unix now do not reference this since it was misleading.

Regards,

Michael Hester
roy
Participant
Posts: 2598
Joined: Wed Jul 30, 2003 2:05 am
Location: Israel

Post by roy »

Hi,
Another way might be:
1. when extracting the timestamp from SQL Server use the date data type instead of the timestamp for this column (this will give you the DS internal date format without the time portion).
2. convert the date you have via Iconv to DS internal date format.
3. compare the dates.

this is more clean in my opinion and no worries about lower/upper case letters.

if you don't like the scheeme changes use Iconv() to get the date in DS internal format to achieve step 1.

IHTH (I Hope This Helps)
Roy R.
Time is money but when you don't have money time is all you can afford.

Search before posting:)

Join the DataStagers team effort at:
http://www.worldcommunitygrid.org
Image
danjm
Participant
Posts: 34
Joined: Tue Sep 09, 2003 8:44 am
Location: Canada
Contact:

Thanks to everyone.

Post by danjm »

Thank you to everyone for your valuable input and the time you take to provide it. I'm sure all of this will be of assistance.
I will pass this on to the programmer who was asking. Thanks again, on their behalf.
Hope everyone has a good day. Happy computing!
Dan Marshall
Alberta Education
Edmonton, AB
Post Reply