If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Who can help me to translate SQL Server query to DB2?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-10-07, 06:18
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
Who can help me to translate SQL Server query to DB2?

Hi everyone,

Who can help me to translate the SQL Server query to DB2 query?
The query were shown as below:
CAST( CONVERT( CHAR(2), MonthNumberOfYear) + '/ ' + '1/' + CalendarYear AS DATETIME)

I'm a new one to write query for DB2, I tried to used TIMESTAMP_FORMAT, but it was in vain.
Any reponse is appreciated^_^

Thanks!
Winnie
Reply With Quote
  #2 (permalink)  
Old 04-10-07, 08:14
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
It looks like you are trying to return the 1st of a particular month.

date(char(MonthNumberOfYear)||'/01/'||CalendarYear)

This is assuming that CalendarYear is a string type (char, varchar).

Andy
Reply With Quote
  #3 (permalink)  
Old 04-10-07, 15:36
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
If you have the date value already as DATE:
Code:
myDate - ( DAYS(myDate) - 1 )DAYS
Code:
$ db2 "values current_date - ( DAY(current_date) - 1 )DAYS"

1
----------
04/01/2007

  1 record(s) selected.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 04-10-07, 22:05
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
Hi ARWinner,

Why I always encouter the error:
The syntax of the string representation of a datetime value is incorrect. SQLSTATE: 22007, SQLCODE: -180 (Microsoft DB2 OLE DB Provider)

my query is:date(char("MonthNumberOfYear")||'/01/'||Char("CalendarYear"))

Thanks^_^
Winnie
Reply With Quote
  #5 (permalink)  
Old 04-11-07, 06:42
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Probably the CHAR function didn't return a clean result.

Do a
Code:
SELECT char("MonthNumberOfYear"), Char("CalendarYear") FROM ...
and see what the results are.

If the year is something like "2007.", get rid of the dot ".".

I also believe DB2 expects 2 digits for the month, so you must get "01" for the month "1" won't do.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #6 (permalink)  
Old 04-11-07, 06:47
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by winniewang
Why I always encouter the error:
The syntax of the string representation of a datetime value is incorrect. SQLSTATE: 22007, SQLCODE: -180 (Microsoft DB2 OLE DB Provider)

my query is:date(char("MonthNumberOfYear")||'/01/'||Char("CalendarYear"))

Thanks^_^
Winnie
First, a double-quote is not valid where you are using it. Next, CHAR('MonthNumberOfYear') is not a valid component in a date value - it is not a number as it should be but rather a string. What DB2 wants to convert to a date is this string

MonthNumberOfYear/01/CalendarYear

Which is, of course, an invalid datetime expression. So you should tell us where the numerical values for MonthNumberOfYear and CalendarYear come from.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #7 (permalink)  
Old 04-11-07, 07:29
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
And I’m sure “char(MonthNumberOfYear AS CHAR(2))” is correct. If I make it as a column. And if I change it into: date('12'||'/01/'||CalendarYear), it is OK. The MonthNumberOfYear's datatype is int, CalendarYear's datatype is string. Could you tell me why?
Reply With Quote
  #8 (permalink)  
Old 04-11-07, 07:59
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
It is not working because MonthNumberOfYear is less than 10 and the date string you are creating is like '4/11/2007' where it needs to be like '04/11/2007'.
so you need to detect this situation and adjust the query accordingly.

date(case when MonthNumber_of_Year < 10 then '0' else '' end || char(MonthNumberOfYear) || '/01/' || CalendarYear)

Andy
Reply With Quote
  #9 (permalink)  
Old 04-11-07, 13:16
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
@ARWinner: No, this is unnecessary. DB2 does not require the leading 0s.
Code:
$ db2 "values date('4/3/2007')"

1
----------
04/03/2007

  1 record(s) selected.
@winniewang: "CHAR(MonthNumberOfYear AS CHAR(2))" is not correct. It would have to be a CAST. You should really have a look at the string expression itself. What does the following give you?
Code:
$ db2 "values 'X' || CAST(MonthNumberOfYear AS CHAR(2) || '/01/' || CalendarYear) || 'X'"
Please show us the result of this.

Note: A simple CHAR(<int-value>) will convert the integer to a CHAR(8) with padding whitespaces to the right. In that case, RTRIM(CHAR(<int-value>)) would be required to get rid of those whitespaces.
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #10 (permalink)  
Old 04-11-07, 21:17
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
Hi Stolze,
You are right.But it still return error like this:The syntax of the string representation of a datetime value is incorrect. SQLSTATE: 22007, SQLCODE: -180 (Microsoft DB2 OLE DB Provider)
My query is:
date(Cast(MonthNumberOfYear AS char(2))||'/01/'||CalendarYear)

It seems that the the result of "Cast(MonthNumberOfYear AS char(2))" can not use as the date's parameter...

Thanks^_^
Winnie

Last edited by winniewang; 04-11-07 at 21:29.
Reply With Quote
  #11 (permalink)  
Old 04-12-07, 06:13
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Please answer my previous post and show us the result of the following SQL statement:
Code:
$ db2 "SELECT Cast(MonthNumberOfYear AS char(2)) || '/01/' || CalendarYear FROM ..."
Right now we can only give wild guesses...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #12 (permalink)  
Old 04-12-07, 07:10
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
the result like this: 7 /01/2001

Last edited by winniewang; 04-12-07 at 07:14.
Reply With Quote
  #13 (permalink)  
Old 04-12-07, 14:29
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
There is a space after the '7', which is not allowed for datetime expressions. This space originates from your CAST to a CHAR(2). So you will have to trim the string before the concatenation:
Code:
SELECT RTRIM(CHAR(MonthNumberOfYear)) || '/01/' || CalendarYear FROM ...
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #14 (permalink)  
Old 04-12-07, 21:13
winniewang winniewang is offline
Registered User
 
Join Date: Apr 2007
Posts: 63
hahaha...
You are right.Thanks a lot^_^

Thanks
Winnie
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On