1

I have written the following code to fetch a date based on the users NLS_LANGUAGE variable. I am writing this code in BI Publisher inbuilt in Oracle Fusion but i doubt that it will matter. I have seen that it works fine for AMERICAN, SPANISH, etc but the same is not working for FRENCH or BRAZILIAN PORTUGUESE. The start_time field is of type TIMESTAMP. When i hardcode BRAZILIAN PORTUGUESE instead of the dynamic code, it works fine with tr.start_time. But when i use the dynamic code, it is giving this issue. Also if i replace tr.start_time with sysdate, then also it is working. I am getting the following error:

ORA-01877 string too long for the internal buffer

Please let me know where i am going wrong.

WITH nls AS (
 SELECT 'NLS_DATE_LANGUAGE = '''||value||'''' value 
 FROM v$nls_parameters 
 WHERE parameter = 'NLS_LANGUAGE')
SELECT TO_CHAR(tr.start_time,'Day,DD Mon YYYY',nls.value) date_from
FROM table_list

I have also tried:

WITH nls AS 
  (SELECT value 
  FROM v$nls_parameters 
  WHERE parameter = 'NLS_LANGUAGE')
select
TO_CHAR(tr.start_time,'Day,DD Mon YYYY','NLS_DATE_LANGUAGE='''||nls.value||'''') date_from
FROM table_list

The below code also doesnt work:

SELECT TO_CHAR(systimestamp ,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||value||'''') date_from FROM v$nls_parameters  WHERE parameter = 'NLS_LANGUAGE'

The below two codes are working fine:

TO_CHAR(tr.start_time,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||'BRAZILIAN PORTUGUESE'||'''')

TO_CHAR(sysdate,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||nls.value||'''')

Full code is given below:

WITH nls AS
  (SELECT value FROM v$nls_parameters WHERE parameter = 'NLS_LANGUAGE'
  )
SELECT trg.tm_rec_grp_id,
  tr.tm_rec_id,
  TRUNC(trg.start_time) start_time,
  TRUNC(trg.stop_time) stop_time,
  tr.measure,
  tr.unit_of_measure,
  TO_CHAR(tr.start_time,'HH24:MI') line_start_time,
  TO_CHAR(tr.stop_time,'HH24:MI') line_stop_time,
  (
  SELECT mda.daily_comment
  FROM hxt_tm_header th,
    hxt_tm_mtrx tm,
    hxt_tm_mtrx_dly_atrs mda,
    hxt_tm_mtrx_tbb_usgs mtu
  WHERE th.time_bldg_blk_id = :p_tm_rec_grp_id
  AND th.tm_header_id       = tm.tm_header_id
  AND tm.tm_mtrx_row_id     = mda.tm_mtrx_row_id
  AND tm.tm_mtrx_row_id     = mtu.tm_mtrx_row_id
  AND tr.tm_rec_id          =
    CASE
      WHEN TO_CHAR(tr.start_time,'Day') = 1
      THEN mtu.TM_BLDG_BLK_DTL_ID1 -- Monday
      WHEN TO_CHAR(tr.start_time,'Day') = 2
      THEN mtu.TM_BLDG_BLK_DTL_ID2
      WHEN TO_CHAR(tr.start_time,'Day') = 3
      THEN mtu.TM_BLDG_BLK_DTL_ID3
      WHEN TO_CHAR(tr.start_time,'Day') = 4
      THEN mtu.TM_BLDG_BLK_DTL_ID4
      WHEN TO_CHAR(tr.start_time,'Day') = 5
      THEN mtu.TM_BLDG_BLK_DTL_ID5
      WHEN TO_CHAR(tr.start_time,'Day') = 6
      THEN mtu.TM_BLDG_BLK_DTL_ID6
      WHEN TO_CHAR(tr.start_time,'Day') = 7
      THEN mtu.TM_BLDG_BLK_DTL_ID7 -- Sunday
    END
  ) comment_text,
  (
  SELECT comments
  FROM hxt_tm_header th
  WHERE th.time_bldg_blk_id = trg.tm_rec_grp_id
  ) header_comments,
  tr.resource_id,
  TO_CHAR(tr.start_time,'Day,DD Mon YYYY','NLS_DATE_LANGUAGE='''||nls.value||'''') date_from,
  tr.start_time time_entry_start_time,
  tr.date_to,
  hl.meaning status,
  hl.lookup_code status_value,
  tr.layer_code record_type,
  tra.tm_rep_atrb_id pjc_time_repos_atrb_id,
  tra.attribute_category pjc_attribute_category,
  tra.attribute_varchar1 pjc_billable_flag,
  tra.attribute_number1 pjc_project_id,
  pab.segment1
  ||'-'
  ||pat.name project_name,
  tra.attribute_number2 pjc_task_id,
  ptv.task_number
  ||'-'
  ||ptv.task_name task_name,
  tra.attribute_number4 pjc_project_unit,
  exp.pjc_expenditure_type expenditure_type_id,
  expt.sl_expenditure_type_name expenditure_type_name,
  tra2.attribute_varchar3 project_activities,
  hg.geography_name
  ||'-'
  ||hg.geography_code tax_area,
  tra.attribute_number7 pjc_work_type
FROM hwm_tm_rec_grp trg,
  hwm_tm_rec_grp trg2,
  hwm_tm_rec_grp_usages trgu,
  hwm_tm_rec tr,
  hwm_tm_statuses ts,
  hcm_lookups hl,
  hwm_tm_rep_atrbs tra,
  hwm_tm_rep_atrbs tra2,
  hwm_tm_rep_atrb_usages trau,
  hwm_tm_rep_atrb_usages trau2,
  pjf_projects_all_b pab,
  pjf_projects_all_tl pat,
  pjf_tasks_v ptv,
  hz_geographies hg,
  hwm_tm_rep_m_pjc_exp_atrbs_v exp,
  pjc_exp_types_ccw_v expt,
  HWM_TM_REP_M_PJC_DOC_ATRBS_V esys,
  nls
WHERE trg.tm_rec_grp_id               = :p_tm_rec_grp_id
AND trg2.parent_tm_rec_grp_id         = trg.tm_rec_grp_id
AND trg.latest_version                = 'Y'
AND trg2.latest_version               = 'Y'
AND trg.tm_rec_grp_version            = trg2.parent_tm_rec_grp_version
AND trg2.tm_rec_grp_id                = trgu.tm_rec_grp_id
AND trg2.tm_rec_grp_version           = trgu.tm_rec_grp_version
AND trgu.tm_rec_id                    = tr.tm_rec_id
AND trgu.tm_rec_version               = tr.tm_rec_version
AND trgu.latest_version               = 'Y'
AND tr.resource_id                    = :p_resource_id
AND tr.latest_version                 = 'Y'
AND COALESCE(tr.delete_flag,'N')      = 'N'
AND tr.tm_rec_id                      = exp.usages_source_id
AND TO_CHAR(expt.expenditure_type_id) = TO_CHAR(exp.pjc_expenditure_type)
AND pab.project_unit_id               = expt.project_unit_id
AND esys.usages_source_id             = tr.tm_rec_id
AND esys.usages_source_version        = tr.tm_rec_version
AND expt.system_linkage_function      = esys.pjc_document_type
AND exp.usages_source_version         =
  (SELECT MAX(eav.usages_source_version )
  FROM hwm_tm_rep_m_pjc_exp_atrbs_v eav
  WHERE eav.usages_source_id = tr.tm_rec_id
  )
AND ts.tm_bldg_blk_id      = trg.tm_rec_grp_id
AND ts.tm_bldg_blk_version = trg.tm_rec_grp_version
AND sysdate BETWEEN ts.date_from AND ts.date_to
AND hl.lookup_code = ts.status_value
AND hl.lookup_type = 'HWM_UI_STATUS'
AND EXISTS
  (SELECT 1
  FROM hwm_tm_status_def_b sdb
  WHERE ts.tm_status_def_id    = sdb.tm_status_def_id
  AND upper(sdb.status_def_cd) = 'D_TM_UI_STATUS'
  )
AND trau.usages_source_id       = tr.tm_rec_id
AND trau.latest_version         = 'Y'
AND trau.usages_source_version  = tr.tm_rec_version
AND trau2.usages_source_id      = tr.tm_rec_id
AND trau2.latest_version        = 'Y'
AND trau2.usages_source_version = tr.tm_rec_version
AND tra.tm_rep_atrb_id          = trau.tm_rep_atrb_id
AND tra2.tm_rep_atrb_id         = trau2.tm_rep_atrb_id
AND tra.attribute_category      = 'Projects'
AND tra2.attribute_category     = 'ORA_CUSTOM'
AND pab.project_id  = to_number(tra.attribute_number1)
AND pab.project_id  = pat.project_id
AND pat.language    = USERENV('LANG')
AND ptv.project_id  = pat.project_id
AND ptv.task_id     = to_number(tra.attribute_number2)
AND hg.geography_id = to_number(tra2.attribute_varchar1)
ORDER BY tr.start_time

Thanks in advance,

Anoop

prince
  • 854
  • 2
  • 9
  • 36
  • Why are you doing this? Simply run `TO_CHAR(tr.start_time,'Day,DD Mon YYYY')`, it takes automatically value from `NLS_DATE_LANGUAGE`. Also why do you query `NLS_LANGUAGE` instead of `NLS_DATE_LANGUAGE`? – Wernfried Domscheit Jan 05 '18 at 15:16
  • 1
    Is that the ACTUAL code you are running? Don't say "yes" because that cannot be true!) How can we help you when you DON'T share the ACTUAL code? (This is not the actual code, because: you reference `tr` but `tr` is not defined anywhere; you reference `nls` in the outer select, `nls` is defined in the `with` clause, but it is not included in the `FROM` clause of the outer query. Your query will not parse successfully - you will never get as far as the claimed error. –  Jan 06 '18 at 02:10
  • Yep, this isnt the actual code. Table list is a list of tables and tr is one of them. nls is also included in the table list. I removed the rest of the code as i felt it wont help much here. Besides its a big query. Let me try Wernfried's suggestion. – prince Jan 08 '18 at 04:36
  • My NLS_DATE_LANGUAGE by default is NUMERIC DATE LANGUAGE. So i have to use NLS_LANGUAGE instead to get the DAY properly. – prince Jan 08 '18 at 05:30
  • What do you mean by "My NLS_DATE_LANGUAGE by default is NUMERIC DATE LANGUAGE."? This sounds very weird. As mathguy already requested: Please post your real code. – Wernfried Domscheit Jan 08 '18 at 07:39
  • What do you get by `SELECT * FROM v$nls_parameters WHERE parameter like 'NLS%LANGUAGE'`? And again, why do you use `NLS_LANGUAGE` for `NLS_DATE_LANGUAGE`? It's really not clear for me. – Wernfried Domscheit Jan 08 '18 at 09:31
  • `TO_CHAR(...,'Day')` returns the weekday, e.g. `Monday`, so `WHEN TO_CHAR(tr.start_time,'Day') = 1` is literally wrong. – Wernfried Domscheit Jan 08 '18 at 09:38
  • [USERENV](https://docs.oracle.com/database/121/SQLRF/functions244.htm#SQLRF06157) is deprecated, you should use [SYS_CONTEXT](https://docs.oracle.com/database/121/SQLRF/functions199.htm#SQLRF06117) instead. Also you should prefer ANSI join syntax instead of old Oracle join syntax. – Wernfried Domscheit Jan 08 '18 at 09:41
  • 1
    Maybe have a look at this: https://laurentschneider.com/wordpress/2008/08/return-size-of-to_char.html – Wernfried Domscheit Jan 08 '18 at 10:03
  • TO_CHAR(...,'Day') returns 1-7 for me because NLS_DATE_LANGUAGE is NUMERIC DATE LANGUAGE for me. If it were AMERICAN (Sunday-Saturday) or some other language, i need not query the NLS_LANGUAGE as it is automatically taken care by USERENV . SYS_CONTEXT('USERENV', 'NLS_DATE_LANGUAGE') = NUMERIC DATE LANGUAGE for me. The base code works. Multi-language is the only new addition here. – prince Jan 08 '18 at 10:17
  • I tried casting the timestamp to date. It worked. I think that is inline with the link you posted. https://laurentschneider.com/wordpress/2008/08/return-size-of-to_char.html – prince Jan 08 '18 at 10:20

1 Answers1

1

Maybe try it like this:

TO_CHAR(tr.start_time,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||SYS_CONTEXT('USERENV', 'NLS_DATE_LANGUAGE')||'''') 

TO_CHAR(tr.start_time,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||SYS_CONTEXT('USERENV', 'LANGUAGE')||'''')

No idea what your problem is, for me is works fine.

BEGIN
    FOR aLang IN (SELECT * FROM V$NLS_VALID_VALUES WHERE parameter = 'LANGUAGE' ORDER BY VALUE) LOOP
    BEGIN
        DBMS_OUTPUT.PUT_LINE(aLang.VALUE || ' -> ' || TO_CHAR(SYSDATE,'fmDay, DD Mon YYYY', 'NLS_DATE_LANGUAGE='''||aLang.VALUE||'''') );
    EXCEPTION
        WHEN OTHERS THEN
            DBMS_OUTPUT.PUT_LINE(aLang.VALUE || ' -> ' || SQLERRM);
    end;
    end loop;   
end;

Result:

ALBANIAN -> E Premte, 5 Jan 2018
AMERICAN -> Friday, 5 Jan 2018
ARABIC -> الجمعة, 5 يناير 2018
ASSAMESE -> শুক্ৰবাৰ, 5 জানুৱাৰী 2018
AZERBAIJANI -> Cümə, 5 Yan 2018
BANGLA -> শুক্রবার, 5 জানুয়ারী 2018
BELARUSIAN -> Пятніца, 5 Сту 2018
BENGALI -> শুকবার, 5 জানু 2018
BRAZILIAN PORTUGUESE -> Sexta-Feira, 5 Jan 2018
BULGARIAN -> Петък, 5 Ян. 2018
CANADIAN FRENCH -> Vendredi, 5 Janv. 2018
CATALAN -> Divendres, 5 Gen. 2018
CROATIAN -> Petak, 5 Sij 2018
CYRILLIC KAZAKH -> Жұма, 5 Қаң 2018
CYRILLIC SERBIAN -> Петак, 5 Јан 2018
CYRILLIC UZBEK -> Жума, 5 Янв 2018
CZECH -> Pátek, 5 Led 2018
DANISH -> Fredag, 5 Jan 2018
DUTCH -> Vrijdag, 5 Jan 2018
EGYPTIAN -> الجمعة, 5 يناير 2018
ENGLISH -> Friday, 5 Jan 2018
ESTONIAN -> Reede, 5 Jaan 2018
FINNISH -> Perjantai, 5 Tammi 2018
FRENCH -> Vendredi, 5 Janv. 2018
GERMAN -> Freitag, 5 Jan 2018
GERMAN DIN -> Freitag, 5 Jan 2018
GREEK -> Παρασκευή, 5 Ιαν 2018
GUJARATI -> શુક્રવાર, 5 જાન્યુવારી 2018
HEBREW -> יום שישי, 5 ינואר 2018
HINDI -> शुक्रवार, 5 जनवरी 2018
HUNGARIAN -> Péntek, 5 Jan. 2018
ICELANDIC -> Föstudagur, 5 Jan 2018
INDONESIAN -> Jumat, 5 Jan 2018
IRISH -> Dé Haoine, 5 Ean 2018
ITALIAN -> Venerdì, 5 Gen 2018
JAPANESE -> 金曜日, 5 1月 2018
KANNADA -> ಶುಕ್ರವಾರ, 5 ಜನವರಿ 2018
KOREAN -> 금요일, 5 1월 2018
LATIN AMERICAN SPANISH -> Viernes, 5 Ene 2018
LATIN SERBIAN -> Petak, 5 Jan 2018
LATIN UZBEK -> Juma, 5 Yanvar 2018
LATVIAN -> Piektdiena, 5 Jan 2018
LITHUANIAN -> Penktadienis, 5 Sau 2018
MACEDONIAN -> Петок, 5 Јан 2018
MALAY -> Jumaat, 5 Jan 2018
MALAYALAM -> വെള്ളിആഴ്ച, 5 ജനവരി 2018
MARATHI -> शुक्रवार, 5 जानेवारी 2018
MEXICAN SPANISH -> Viernes, 5 Ene 2018
NORWEGIAN -> Fredag, 5 Jan 2018
ORIYA -> ଶୁକ୍ରବାର, 5 ଜାନୁଆରୀ 2018
POLISH -> Piątek, 5 Sty 2018
PORTUGUESE -> Sexta-Feira, 5 Jan 2018
PUNJABI -> ਸ਼ੁੱਕਰਵਾਰ, 5 ਜਨਵਰੀ 2018
ROMANIAN -> Vineri, 5 Ian 2018
RUSSIAN -> Пятница, 5 Янв 2018
SIMPLIFIED CHINESE -> 星期五, 5 1月 2018
SLOVAK -> Piatok, 5 Jan 2018
SLOVENIAN -> Petek, 5 Jan 2018
SPANISH -> Viernes, 5 Ene 2018
SWEDISH -> Fredag, 5 Jan 2018
TAMIL -> வெள்ளி, 5 ஜன. 2018
TELUGU -> శుక్రవారం, 5 ಜనవరి 2018
THAI -> ศุกร์, 5 ม.ค. 2018
TRADITIONAL CHINESE -> 星期五, 5 1月 2018
TURKISH -> Cuma, 5 Oca 2018
UKRAINIAN -> П'Ятниця, 5 Січ 2018
VIETNAMESE -> Thứ Sáu, 5 Tháng MộT 2018

Update:

It is also working manually (although it is useless):

ALTER SESSION SET NLS_LANGUAGE = 'BRAZILIAN PORTUGUESE';
Session altered.


SELECT TO_CHAR(SYSTIMESTAMP ,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||VALUE||'''') date_from 
FROM v$nls_parameters 
WHERE parameter = 'NLS_LANGUAGE';

DATE_FROM                                                                  
---------------------------------------------------------------------------
Segunda-Feira,08 Jan 2018                                                  
1 row selected.
Wernfried Domscheit
  • 54,457
  • 9
  • 76
  • 110
  • Hi Wernfried, When i hardcode BRAZILIAN PORTUGUESE instead of the dynamic code, it works fine with tr.start_time. But when i use the dynamic code, it is giving this issue. Also if i replace tr.start_time with sysdate, then also it is working. – prince Jan 08 '18 at 05:25
  • On which value (i.e. which date) does the error come? Does it work with `LOCALTIMESTAMP`? – Wernfried Domscheit Jan 08 '18 at 07:40
  • Seems this is an issue with timestamp. The following code has the same issue. SELECT TO_CHAR(systimestamp ,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||value||'''') date_from FROM v$nls_parameters WHERE parameter = 'NLS_LANGUAGE'; – prince Jan 08 '18 at 08:43
  • For me it is working. Please provide us your full working (resp. failing) code example. – Wernfried Domscheit Jan 08 '18 at 08:49
  • It is also working for me also in another instance. But the same is not working for me in the current one. . . The following code is not working for me. I think this isnt code specific because of this. The error is the same. . SELECT TO_CHAR(systimestamp ,'Day,DD Mon YYYY', 'NLS_DATE_LANGUAGE = '''||value||'''') date_from FROM v$nls_parameters WHERE parameter = 'NLS_LANGUAGE'; – prince Jan 08 '18 at 09:05