Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2006
    Posts
    9

    Unanswered: 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.

  2. #2
    Join Date
    Jan 2003
    Posts
    1,605
    Hi,
    there is also CAST function in DB2. Sintax: CAST (column_name AS data_type).
    Hope this helps,
    Grofaty

  3. #3
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Yes, DB2 has the CAST function. Read the SQL Reference manual on its proper usage.

    Andy

  4. #4
    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

  5. #5
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    What is the datatype of sta_dt?

    Andy

  6. #6
    Join Date
    Sep 2006
    Posts
    9

    cast function!!!

    Thanks andy for reply.

    Data type is Date

    Regards,
    Tushar

  7. #7
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    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

  8. #8
    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

  9. #9
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    Use || instead of + to concat.

    Andy

  10. #10
    Join Date
    Sep 2006
    Posts
    9

    THanks

    Thanks Andy It Worked. Thanks a lot.

  11. #11
    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

  12. #12
    Join Date
    Jan 2003
    Posts
    4,292
    Provided Answers: 5
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •