Converting Date to Char

Using MaxDB Version Version: 7.7.04.28
I am seeing difficulties with the CHAR(a,t) function. I have an SQL statement where I would like to convert the current date to a string and pick only the year information.
If I understand the documentation correctly, this should work: CHAR(NOW(),'YYYY')
However, I find that the second parameter is not a year but a locale.
This works: CHAR(NOW(),USA) and gives 2009-10-09
So to get the year information I am doing this: SUBSTR(CHAR(NOW(), USA),1,4)
But this looks like  a hack. The clean way would be to use CHAR(NOW(),'YYYY'). How come this does not work?
Many thanks for any help.

> I am seeing difficulties with the CHAR(a,t) function. I have an SQL statement where I would like to convert the current date to a string and pick only the year information.
>
> If I understand the documentation correctly, this should work: CHAR(NOW(),'YYYY')
You don't understand this command correctly, sorry.
> However, I find that the second parameter is not a year but a locale.
> This works: CHAR(NOW(),USA) and gives 2009-10-09
Yes, sure it works.
The documentation says that exactly this would happen when you use the command as you did.
Problem in sight?
> So to get the year information I am doing this: SUBSTR(CHAR(NOW(), USA),1,4)
> But this looks like  a hack. The clean way would be to use CHAR(NOW(),'YYYY'). How come this does not work?
What you do there is a hack, but it's not about the data extraction model.
There is a specific function for what you want to to: YEAR().
Have a look at
[Extraction (extraction_function)|http://maxdb.sap.com/doc/7_7/45/583ea6484365d1e10000000a1553f6/content.htm]
and the bunch of SQL Tutorials on this topic you also find in the documentation.
Just as a general hint: don't try to do Oracle with any database that is not Oracle.
Examples:
sqlcli db77=> select year(now()) from dual
| EXPRESSION1        |
| ------------------ |
|               2009 |
1 row selected (168.846 msec)
sqlcli db77=> select month(now()) from dual
| EXPRESSION1        |
| ------------------ |
|                 10 |
1 row selected (1406 usec)
sqlcli db77=> select day(now()) from dual
| EXPRESSION1        |
| ------------------ |
|                 10 |
1 row selected (1234 usec)
sqlcli db77=> select dayofweek(now()) from dual
| EXPRESSION1        |
| ------------------ |
|                  6 |
1 row selected (1671 usec)
sqlcli db77=> select weekofyear(now()) from dual
| EXPRESSION1        |
| ------------------ |
|                 41 |
1 row selected (885 usec)
sqlcli db77=> select now() from dual
| EXPRESSION1                |
| -------------------------- |
| 2009-10-10 08:39:17.890000 |
1 row selected (28.149 msec)
regards,
Lars
Edited by: Lars Breddemann on Oct 10, 2009 8:40 AM

Similar Messages

  • Converting data to char in double quotes.

    I need to create a comma separated txt data load file. Each column has to be within double quotes.
    "Material","Plant","SOrg" .....
    Currently I am doing something like this. ..
    Loop at table, for each field, concatenate " field value ".
    But I find this solution to be cumbersome as I have to change the data type for each field to character. Is there a better way to accomplish this?
    Thank you.

    You can create simple Method or Subroutine or a Macro for this purpose.
    If you have lot of fields, you can even use RTTS to get the field names and field-symbols instead of writing all the fields.
    TYPES:
      BEGIN OF lty_data,
        fld1 TYPE char10,
        fld2 TYPE dmbtr,
      END   OF lty_data.
    DATA: li_data TYPE STANDARD TABLE OF lty_data.
    DATA: lwa_data LIKE LINE OF li_data.
    DATA: lv_char TYPE char255.
    DEFINE _con.
      lv_char = &1.
      condense lv_char.
      concatenate &2 '"' lv_char '"' into &2 .
    END-OF-DEFINITION.
    lwa_data-fld1 = 'c1'.
    lwa_data-fld2 = '12345.23'.
    APPEND lwa_data TO li_data.
    lwa_data-fld1 = 'c2'.
    lwa_data-fld2 = '72345.23'.
    APPEND lwa_data TO li_data.
    DATA: lv_output TYPE string.
    LOOP AT li_data INTO lwa_data.
      _con lwa_data-fld1 lv_output.
      CONCATENATE lv_output ',' INTO lv_output.
      _con lwa_data-fld1 lv_output.
      WRITE: / lv_output.
      CLEAR: lv_output.
    ENDLOOP.
    Regards,
    Naimesh Patel

  • Not able to convert date in char with interval

    When i run below
    SQL> select to_date('20120229','YYYYMMDD') + interval '1' day from dual;
    TO_DATE('
    01-MAR-12
    How can i change this output to YYYYMMDD format.
    Desired output - *20120301*
    I tried with to_char making on above query,It didn't work !

    Hi,
    Not really a SQL Developer tool question. Next time, please try the SQL and PL/SQL forum in the Oracle Database section:
    PL/SQL
    But this will do the trick:
    select to_date('20120229','YYYYMMDD') + interval '1' day from dual;
    select to_char(to_date('20120229','YYYYMMDD') + 1, 'YYYYMMDD') as mydateformat from dual; produces this
    TO_DATE('20120229','YYYYMMDD')+INTERVAL'1'DAY
    01-MAR-12                
    MYDATEFORMAT
    20120301     Regards,
    Gary
    SQL Developer Team

  • Performance question when compare date with date or char with char

    Hello from Germany (Frankfurt) !
    I am working on Oracle 9.2.0.8.0
    and have to solve following problem:
    Comparison of a date and char fields.
    Now two ways to do it.
    Either I compare char with char and convert date to char,
    or I compare date with date and convert char to date.
    Now the performace question. Which operation takes more effort for the database?
    So why not to try and to see the results?
    First create table:
    CREATE TABLE TEST (
    char_date VARCHAR2(8),
    real_date DATE
    NOLOGGING;
    Then insert 1.000.000 rows
    BEGIN
    for x in 1..1000000
    loop
    insert into test (char_date, real_date) VALUES('19990101', TO_DATE('2006.01.01', 'YYYY.MM.DD'));
    end loop;
    COMMIT;
    END;
    Collect statistics
    EXEC dbms_stats.gather_table_stats('TESTER', 'TEST');
    Now run some selects for date to char conversion:
    Elapsed: 00:00:00.00
    SQL> select * from test t where TO_DATE(char_date, 'YYYYMMDD') > real_date;
    no rows selected
    Elapsed: 00:00:03.02
    SQL> select * from test t where TO_DATE(char_date, 'YYYYMMDD') > real_date;
    no rows selected
    And some selects for char to date conversion:
    Elapsed: 00:00:03.02
    SQL> select * from test t where char_date > TO_CHAR(real_date, 'YYYYMMDD');
    no rows selected
    Elapsed: 00:00:02.05
    SQL> select * from test t where char_date > TO_CHAR(real_date, 'YYYYMMDD');
    no rows selected
    Elapsed: 00:00:02.05
    SQL>
    As you see when I compare char with char and convert date to char it seems to be faster (almost 1 second)
    Is the test correct?
    I still not sure, what gets better performance...
    Any idea?
    Thanks!

    Depends on whether you want the right results or not.
    Why don't you run the following two queries and see if the difference in results tells you anything?:
    with t as (select to_date('01/02/2007', 'dd/mm/yyyy') date_col from dual
               union all
               select to_date('02/02/2007', 'dd/mm/yyyy') from dual
               union all
               select to_date('03/02/2007', 'dd/mm/yyyy') from dual
               union all
               select to_date('03/03/2006', 'dd/mm/yyyy') from dual)
    select *
    from   t
    where  date_col < to_date('04/03/2006', 'dd/mm/yyyy');
    with t as (select to_date('01/02/2007', 'dd/mm/yyyy') date_col from dual
               union all
               select to_date('02/02/2007', 'dd/mm/yyyy') from dual
               union all
               select to_date('03/02/2007', 'dd/mm/yyyy') from dual
               union all
               select to_date('03/03/2006', 'dd/mm/yyyy') from dual)
    select *
    from   t
    where  to_char(date_col) < '04/03/2006';

  • Converting a 6 char string to date

    Please help..
    How can I convert a 6 char date (ddmmyy) to date format
    that can be passed as input to function module?

    Hi,
    Try FM,
    CONVERT_DATE_TO_INTERNAL
    Run this sample code,
    DATA:
      my_date(6) TYPE c VALUE '300708',
      w_date     TYPE sy-datum.
    CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
      EXPORTING
        date_external                  = my_date
    *   ACCEPT_INITIAL_DATE            = ACCEPT_INITIAL_DATE
    IMPORTING
       date_internal                  = w_date
    EXCEPTIONS
       date_external_is_invalid       = 1.
    WRITE: w_date.
    Regards
    Adil

  • How to convert DATE variable to CHAR variable

    dear all,
    How to convert DATE variable to CHAR variable
    thanq

    Depending on your final goal here are 2 options.
    DATA: datein        TYPE d.
    DATA: dateout(10)   TYPE c.
    DATA: dateout2(8)   TYPE c.
    datein = sy-datum.
    WRITE datein TO dateout MM/DD/YYYY.
    dateout2 = datein.
    WRITE:/ datein, dateout, dateout2.
    and the output
    05022008 05/02/2008 20080502    

  • OWB 10gR1 Convert Date format (mm-dd-yyyy) to (yyyy-mm-dd)

    Hi All,
    In OWB 10gR1, I have flatfiles with Date fields having the format (mm-dd-yyyy). I want to load this data into the Staging table in (yyyy-mm-dd) format. How could I do this conversion in my staging mapping ?
    Using external tables is not an option.
    An detailed answer will be extremely helpful.
    Thanks community.

    Wich is this field's datatype in database? Is it date or char? If you are going to store it as a date datatype, you don't have to worry about the date format.
    Have you tried using the Expression Operator to perform the date string conversion in your mapping? Sorry about this lame drawing, but I think it will help you understand:
    source_table ------------ALL_COLUMNS_EXCEPT_DATE_DATATYPE-------TARGET_TABLE
                 |                                                 |
                 +-------EXPR{TO_DATE(DATE_FIELD, 'DATE_FORMAT')}--+In this example, you'll use an expression operator to convert the date string in the format you want. Map other attributes directly to your target table (or other transformations you need to perform).
    This is the generated script of a dummy plan I've built here:
    OPTIONS ( ERRORS=50, BINDSIZE=50000, ROWS=200, READSIZE=65536)
    LOAD DATA
      CHARACTERSET WE8MSWIN1252
      INFILE '{{LOC_FILE.RootPath}}load.csv'
    INTO TABLE "LOAD_TARGET"
      APPEND
      REENABLE DISABLED_CONSTRAINTS
      FIELDS
        TERMINATED BY ';'
        OPTIONALLY ENCLOSED BY '"'
      "COD" POSITION (1) INTEGER EXTERNAL ,
    "NOME" CHAR ,
    "DATA" CHAR "to_date( :\"DATA\" , 'yyyy-mm-dd')"
      )Regards,
    Marcos

  • Converting date format yyyy-mm-dd to mm/dd/yyyy

    Hi,
    I need help converting the above date format. In the database the date is stored as a VARCHAR with the format yyyy-mm-dd
    but I would like it changed to DATE with the format mm/dd/yyyy. I tried the following and I keep getting invalid month
    select To_date(date_value, 'mm/dd/yyyy') as date_value from table
    When I try to convert it to char first before converting to date I get an invalid number error
    select to_date(to_char(date_value, 'mm/dd/yyyy'), 'mm/dd/yyyy') as date_value from table
    What do I do?
    Thanks for your help!!!

    Hi,
    The second argument of TO_DATE tells what format the first argument is already in.
    So you want:
    select  To_date (date_value, 'yyyy-mm-dd') as date_value
    from    table_x;Format is a quality of strings, not DATEs. Once you have converted your string to a DATE, is is stored in the same internal format as all the other DATEs. Use TO_CHAR to display it in any format you like.
    For example:
    SELECT     TO_CHAR ( TO_DATE (date_value, 'yyyy-mm-dd')
              , 'mm/dd/yyyy'
    FROM     table_x;Things are much easier if you store dates in DATE columns.

  • Converting date data

    HAI
    I have date data(ZDATE) in the form of MM/DD/YYYY format in oracle source system ...
    I want this date data(ZDATE) in the form of YYYYMMDD format in BW.
    SO hw can change this format. I think , it can be done by writing the ABAP code in TS.
    So pls give the ABAP code for converting the date format from MM/DD/YYYY to YYYYMMDD
    rizwan

    Hi
    data : year char(4)
    data : month char(2)
    data : date char(2)
    data : findat char(8)
    year = tran_structure-ZDATE+6(4)
    month = tran_structure-ZDATE+0(2)
    date = tran_structure-ZDATE+3(2)
    Concatenate year month date into findat.
    tran_structure-zdate = findat.
    Hope it helps you...
    Thanks
    Teja

  • Problem with converting data types

    Hi Experts,
    I am stuck up with a little problem, i need your help
    i am converting all the data types to CHAR but i am not getting the expected data out.........
    My code is as below...where do i go wrong...
    LOOP AT <T_ITAB> ASSIGNING <FIELD>.
    DO.
       ASSIGN COMPONENT SY-INDEX OF STRUCTURE <FIELD> TO <RECORD>.
       IF SY-SUBRC = 0.
          LV_FIELD = <RECORD>.
          N = STRLEN( LV_FIELD ).
          IF N = '8'.
               IF LV_FIELD IS NOT INITIAL.
                      LV_DATE = LV_FIELD.
                      CALL FUNCTION 'DATE_CHECK_PLAUSIBILITY'
                              EXPORTING
                              DATE                     = LV_DATE
                             EXCEPTIONS
                                    PLAUSIBILITY_CHECK_FAILED = 1
                                    OTHERS                     = 2.
                             IF SY-SUBRC <> 0.
                                     MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
                                     WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
                             ENDIF.
                         IF SY-SUBRC EQ 0.
                            CONCATENATE LV_FIELD+6(2) LV_FIELD+4(2) LV_FIELD+0(4) INTO LV_FIELD SEPARATED BY '.'.
                        ENDIF.
                ENDIF.
             ENDIF.
                IF LV_RECORD IS INITIAL.
                         LV_RECORD = LV_FIELD.
                ELSE.
                       CONCATENATE LV_RECORD LV_FIELD INTO LV_RECORD SEPARATED BY '  * '.
               ENDIF.
           ELSE.
         EXIT." DO
        ENDIF.
       ENDDO.
      APPEND LV_RECORD TO LT_OUTPUT.
    CLEAR LV_RECORD.
    ENDLOOP.
    THE TABLE LT_OUTPUT     LIKE     THENV(CHAR 255)
    My output looks some thing like this:
    04  36876   15.09.2011  39600   1999
    06  36960   15.09.2011  39600   2632
    07  36874   15.09.2011  39541   9232
    My expected output
    04    36.876  15.09.2011    39.600  1.999
    06    36.960  15.09.2011    39.600  2.632
    07    36.874  15.09.2011    39.541  9.232
    Regards
    sam

    At run time you can see all the data........ like what i have shown...
    but if you clearly see, DATE will be in the internal format..but if you print it, it will be in dd:mm:yyyy
    can you suggest me if i have a dynamic field symbol (table data) ,,,, How can i convert data types dynamically..
    if it is a static internal table i am achieving with WRITE TO statement.....but i have huge data in field symbols...
    Instead of all these , please specify the exact problem your are facing . What is it with date field ? . In SAP while printing the internal format will be converted to external. What is your requirement with this date field?
    My output looks some thing like this:
    04 36876 15.09.2011 39600 1999
    06 36960 15.09.2011 39600 2632
    07 36874 15.09.2011 39541 9232
    My expected output
    04 36.876 15.09.2011 39.600 1.999
    06 36.960 15.09.2011 39.600 2.632
    07 36.874 15.09.2011 39.541 9.232
    I dont see any problems mentioned in your date field. Both your actual and expected outputs reflects the same in date field.
    In SCN you will only get solutions if your question is precise.
    Kesav

  • LSMW - Read Data vs Convert Date for DATES

    Hi everybody,
    I have a problem with my LSMW for a <b>date field</b>.
    In the .txt file it is defined as 01.01.1900.
    (I can change it, e.g. make 01011900 but at the end SAP wants to have 01.01.1900 as input)
    In the 'Maintain Source Fields' of my LSMW I defined it as a DDMY(010) field.
    When 'Reading the Data' you can choose to check Date Value -> YYYYMMDD
    When I <u>check</u> it, it gives:
    For 'Display Read Data': 19000101
    And after 'Convert Data' : 01011900
    When I <u>don't check</u> it, it gives:
    For 'Display Read Data': 01.01.1900
    And after 'Convert Data' : 191.01.0 (?!?)
    The fact is that in SAP it needs to be in following format: 01.01.1900 (so DD.MM.YYYY) so none of both solutions is right.
    Can anybody tell me how I should configure this LSMW that I can transfer dates properly?
    Thank you very much!
    Kind Regards,
    Caroline

    Hi Caroline,
    before doing upload change ur system settings to yyyy.mm.dd (just go to menu bar click on System->user profile->own data in this go for default ) and save it and do log off and log in ,
    and then go for source fields and specify the Date field length as 8 char,
    and then in mapping step for that particular field  write the code as per the below
    just change as per ur Target structure and Source structure and field name .
    Make sure that in ur input file the Date field is in the format of yyyy.mm.dd
    if not as91_ztable-AKTIV is initial.
    replace '.' with '' into as91_ztable-AKTIV.
    replace '.' with '' into as91_ztable-AKTIV.
    condense as91_ztable-AKTIV no-gaps.
    BALTD-AKTIV = AS91_ZTABLE-AKTIV.
    else.
    baltd-aktiv = '/'.
    endif.
    Thanks
    Naveen khan
    Message was edited by:
            Pattan Naveen

  • How to convert ASCII to CHAR?

    Hi, experts.
    Now I want to convert ASCII to CHAR, can you give me some methods? And I also want to know what's my ABAP Version.
    Thanks in advance!

    *After look so bad code that no work i did my own code, a gift for all you:*
    FORM CHARACTER_ASCII   using    p_letra
                                                 changing p_nro   type i.
    field-symbols: <n> type x.
    assign p_letra to <n> casting.
    move <n> to p_nro.
    ENDFORM.
    FORM ASCII_CHARACTER using    p_nro type i
                                               changing p_letra.
    DATA: c    TYPE c,
          x(4) TYPE x.
    FIELD-SYMBOLS: <fc>  TYPE c.
    x = p_nro.
    ASSIGN x to <fc> CASTING TYPE c.
    MOVE <fc>+1(1) to p_letra.
    ENDFORM.

  • Convert Date to Calendar week in Query Designer

    Hi experts,
    Is it possible to convert date to week in the query designer?
    Thanks in advance for your help.
    Best Regards,
    Rose

    Hi,
    Can you please explain more about virtual char you mentioned?
    What i am actually doing is i am using a formula variable (getting the value from customer exit) and i based the calculation of other keyfigures basing on this value.  The input value.. (data type date ) does not filter my query output.. it is only purely used for calculating other keyfigure values.  The data types (Dimension Settings ) available in formula variable are only quantity, amount, pice and number, date and time..  there is no week there.  So currently I am using the date.  If I can convert date to week then.. I can make my query a lot simpler.
    Thanks for your reply...
    Regards,
    Rose

  • Converting raw to char

    Hi SDN,
    I have to convert raw to char.
    data : lv_raw(16) type RAW.
    data : lv_char(16) type c.
    lv_raw = some value.
    i want to convert this value in lv_raw to lv_char in character format.
    please let me know.
    i tried the FM BANK_API_PP_UT_CHAR16_TO_RAW16 but no use.
    Regards,
    Rahul Wagh

    HI Vikranth,
    i tried that FM also but it is not giving me correct value.
    my hex value = 16D7A4FCA7442DDA3AD93C9A726597E4
    the character value for this should come as = test1234
    but FM given by u gives me junk values.
    is there any other way for this
    Regards,
    Rahul

  • Converting quantity to char

    How to convert quantity to char without using FM's like C14W_CHAR_NUMBER_CONVERSION,
    C14W_NUMBER_CHAR_CONVERSION,
    CHAR_INT_CONVERSION as these FM's do not exist in my server?

    Hi...
    this is my program...try this code....
    Parameters:
      p_Number(9) type c.                  " Input string.
    data:
      w_number(9) type c,                  " Input number
      w_strlen(9) type c,                  " Strlen
      w_numc(9)   type n,                  " Digit string
      w_2bit(2)   type c.                  " first 2 positions
      w_number = p_number.
      w_numc = p_number.
      write: 'The given number in words:  '.
      do 5 times.
        case sy-index.
          when 1.
            w_2bit = w_numc+0(2).
          when 2.
            w_2bit = w_numc+2(2).
          when 3.
            w_2bit = w_numc+4(2).
          when 4.
            w_2bit+0(1) = 0.
            w_2bit1(1) = w_numc6(1).
          when 5.
            w_2bit = w_numc+7(2).
        endcase.                           " CASE SY-INDEX.
       if w_2bit+0(2) ne 0.
         if w_2bit le 19.
           case w_2bit.
            when '01'.
              write 'ONE'.
            when '02'.
               write 'TWO'.
            when '03'.
               write 'THREE'.
            when '04'.
               write 'FOUR'.
            when '05'.
               write 'FIVE'.
            when '06'.
               write 'SIX'.
            when '07'.
               write 'SEVEN'.
            when '08'.
               write 'EIGHT'.
            when '09'.
               write 'NINE'.
            when '10'.
               write 'TEN'.
            when '11'.
               write 'LEVEN'.
            when '12'.
               write 'TWELVE'.
            when '13'.
               write 'THIRTEEN'.
            when '14'.
               write 'FOURTEEN'.
            when '15'.
               write 'FIFTEEN'.
            when '16'.
               write 'SIXTEEN'.
            when '17'.
               write 'SEVENTEEN'.
            when '18'.
               write 'EIGHTEEN'.
            when '19'.
               write 'NINETEEN'.
           endcase.                        " CASE 2BITS POSITIONS 1,2.
        else.
          case w_2bit+0(1).
            when '2'.
               write 'TWENTY'.
            when '3'.
               write 'THIRTY'.
            when '4'.
               write 'FOURTY'.
            when '5'.
               write 'FIFTY'.
            when '6'.
               write 'SIXTY'.
            when '7'.
               write 'SEVENTY'.
            when '8'.
                write 'EIGHTY'.
            when '9'.
               write 'NINETY'.
          endcase.                         " CASE W_2BIT+0(1)
        if w_2bit+1(1) ne 0.
           case w_2bit+1(1).
             when '1'.
                write 'ONE'.
             when '2'.
                write 'TWO'.
             when '3'.
                write 'THREE'.
             when '4'.
                write 'FOUR'.
             when '5'.
                write 'FIVE'.
             when '6'.
                write 'SIX'.
             when '7'.
                write 'SEVEN'.
             when '8'.
                write 'EIGHT'.
             when '9'.
                write 'NINE'.
             when others.
                write ' '.
            endcase.                       " CASE W_2BIT+1(1)
         endif.                            " IF W_2BIT+1(1)
      endif.                               " IF 2BIT LE 19
      case sy-index.
          when 1.
            write 'CRORES'.
          when 2.
            write 'LAKHS'.
          when 3.
            write 'THOUSANDS'.
          when 4.
            write 'HUNDREDS'.
       endcase.                            " CASE SY-INDEX
    endif.                                " IF 2BIT NE 0

Maybe you are looking for

  • When I create a 5 x7 canvas, why are my pictures getting cut off?

    So, I'm pretty new to Elements 12, but I do understand things about ratios and what not.  But I am still not understanding why when I create a 5 x 7 canvas, create something on it, then try to print, it either leaves space on the edges or if I do cro

  • B/w photo with isolated color

    How can one turn a photo into black/white but have one element within the photo show in color? For an example, you can log into stock.xchng and check out the following photo: http://www.sxc.hu/index.phtml I'd be using Fireworks 8... THanks for all yo

  • Applications Will Not Open

    Many of my applications will not open. After clicking on them, they sometimes bounce before notifying me that the application quit unexpectedly. I can't get iTunes to open at all before it crashes.

  • Can't update 9.0.0

    I have Acrobat Pro Extended 9.0.0.  The machine is a Windows 7 Pro 64 bit.  The program is properly installed and registered.  I would like to update it to the latest version of 9.  I am NOT interested in upgrading to later versions X or XI because I

  • Os 10.4.7, reboots instead of waking from sleep

    periodically my mac reboots instead of waking from sleep (with mouse or keyboard), once every few days. is this a problem? how can i change that? should i worry about it?