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 > CAST FUNCTIOn???

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-13-06, 07:45
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
CAST FUNCTIOn???

Is their any Function to convert datatype like in Sql Server there is function called CAST in which we can convert datatypes. Like for e.g

E.g
select distinct cast(year(sta_dt) as varchar(05))+'-'+ cast(year(end_dt) as varchar(05)) as yr_expn from co_mast which returns year 2005-2006.

Which command we Need to ISSUE in DB2.
Reply With Quote
  #2 (permalink)  
Old 09-13-06, 08:01
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
there is also CAST function in DB2. Sintax: CAST (column_name AS data_type).
Hope this helps,
Grofaty
Reply With Quote
  #3 (permalink)  
Old 09-13-06, 08:02
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Yes, DB2 has the CAST function. Read the SQL Reference manual on its proper usage.

Andy
Reply With Quote
  #4 (permalink)  
Old 09-13-06, 08:25
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
Cast Function????

Thanks for prompt reply again..

But unfortunately it's not working. For e.g

if i have quries for "select cast(year(sta_dt) as varchar(20)) from co_mast"
it gives me error. How should i use this command in DB2???

Regards,
Tushar
Reply With Quote
  #5 (permalink)  
Old 09-13-06, 08:31
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
What is the datatype of sta_dt?

Andy
Reply With Quote
  #6 (permalink)  
Old 09-13-06, 08:35
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
cast function!!!

Thanks andy for reply.

Data type is Date

Regards,
Tushar
Reply With Quote
  #7 (permalink)  
Old 09-13-06, 08:47
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
The problem is that the YEAR function returns an INT and INT is not allowed to be cast as VARCHAR (I do not know why). Anyway, you can cast it to a CHAR first, then a VARCHAR, or just CHAR if that is OK.

Andy
Reply With Quote
  #8 (permalink)  
Old 09-13-06, 09:01
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
String??

THanks andy.

It worked ..but now problem is that suppose i want to create string. for e.g.

select cast(year(sta_dt) as char(20))+'-'+cast(year(end_dt) as char(20)) from co_mast it gives me error!! what is synatax to combine to string like in above example..

Regards,
Tushar
Reply With Quote
  #9 (permalink)  
Old 09-13-06, 09:05
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Use || instead of + to concat.

Andy
Reply With Quote
  #10 (permalink)  
Old 09-13-06, 09:15
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
THanks

Thanks Andy It Worked. Thanks a lot.
Reply With Quote
  #11 (permalink)  
Old 09-14-06, 01:35
tushar149 tushar149 is offline
Registered User
 
Join Date: Sep 2006
Posts: 9
keywords???

Hi
Suppose i have query like this

select * from "user" where "user"=''. wher user is the keyword in this case when i am issuing this command from command editor of DB2..its working fine but suppose i want to issue this command from front end i.e from any front end parser..its gives me error.. Can u plz help me..

Because we have used "used" keyword many times in our project.

Actually we are converting our project into DB2 from Sql serever.
So plz kindly help and oblige.

Regards,
Tushar
Reply With Quote
  #12 (permalink)  
Old 09-14-06, 08:06
ARWinner ARWinner is offline
Registered User
 
Join Date: Jan 2003
Posts: 3,575
Tushar,
First of all, if you have a different question, you should start a new thread. You will get more help that way. I sort of understand what you want, but I want to be 100% sure, so could you post the DDL of the table/view you are querying and the exact query you want with a description of what the query is supposed to do? This will clarify thing for me.

Andy
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