Oracle 11g 中的 varchar2 中保留的空格字符用于在 SSRS 2008 中显示

发布于 2024-10-11 15:45:10 字数 2854 浏览 2 评论 0原文

是否有空格或其他非打印字符可以插入到 varchar2 列中,这些字符不会被修剪掉并导致 NULL?

我只想在该列中插入一个空白,以便它在 SSRS 2008 报告上不显示任何内容。该列是 PK 的一部分,因此不能为 NULL。当然,使用 '' 不起作用,因为这在 Oracle 中被视为 NULL,并且 ' ' 不起作用,因为它是 varchar2 并被修剪为 NULL。

那么是否有一个我可以插入的文字值,该值在 SSRS 中显示为空,但也可以插入到 Oracle 11g 中的不可空 varchar2 列中?

想了一下,我想像制表符这样的东西可以完成这项工作。但我期待您的建议。

更新

哎呀。猜猜修剪行为来自哪里?我自己的 RTRIM!对此感到抱歉。这么说吧,我因对 Oracle 缺乏经验而被误导,而且我对此感到沮丧,导致我确定错误是在产品中而不是在我的查询中。但是,嘿,这不完全是一个简单的查询。

INSERT INTO WeeklyInvoice (GUID, Mo, VendorName, CostCenter, WkNum, Amt)
SELECT
   ExecID,
   Mo,
   VendorName,
   CostCenter,
   WkNum,
   Amt
FROM (
   WITH CostCenters AS (
      SELECT REGEXP_SUBSTR(CostCenterList, '[^,]+', 1, LEVEL) CostCenter
      FROM DUAL
      CONNECT BY LEVEL <= Length(CostCenterList) - Length(Replace(CostCenterList, ',', '')) + 1
   ), Invoices AS (
      SELECT
         TRUNC(I.Invoice_Dte, 'MM') Mo,
         (TRUNC(I.Invoice_Dte, 'W') - TRUNC(I.Invoice_Dte, 'MM')) / 7 + 1 WkNum,
         I.Vendor,
         V.Vendor_VName,
         RTrim(D.Dis_Acct_Unit) CostCenter,
         D.To_Base_Amt
      FROM
         CostCenters CC
         CROSS JOIN prod.IcCompany C
         INNER JOIN prod.ApDistrib D
            ON C.Company = D.Company
            AND D.Dis_Acct_Unit = CC.CostCenter
         INNER JOIN prod.ApInvoice I
            ON D.Invoice = I.Invoice
            AND D.Vendor = I.Vendor
            AND D.Suffix = I.Suffix
            AND D.Company = I.Company
         INNER JOIN prod.ApVenMast V ON I.Vendor = V.Vendor
      WHERE
         D.Cancel_Seq = 0
         AND I.Cancel_Seq = 0
         AND I.Invoice_Dte >= ADD_MONTHS(FromDate, -2)
         AND I.Invoice_Dte < ToDate
   ), Months AS (
      SELECT ADD_MONTHS(FromDate, LEVEL - 1) Mo
      FROM DUAL
      CONNECT BY LEVEL <= MONTHS_BETWEEN(ToDate, ADD_MONTHS(FromDate, -2))
   ), Names AS (
      SELECT DISTINCT
         I.Mo,
         I.Vendor,
         I.Vendor_VName,
         I.CostCenter
      FROM Invoices I
      UNION ALL
      SELECT M.Mo, '0', 'No Paid Invoices', ' '
      FROM Months M
      WHERE
         NOT EXISTS (
            SELECT I.*
            FROM Invoices I
            WHERE I.Mo = M.Mo
         )
   ), Weeks AS (
      SELECT LEVEL WkNum FROM DUAL CONNECT BY LEVEL <= 5
   )
   SELECT
      N.Mo,
      N.Vendor_VName VendorName,
      N.CostCenter,
      W.WkNum,
      Sum(I.To_Base_Amt) Amt
   FROM
      Names N
      INNER JOIN Weeks W
         ON W.WkNum < 5
         OR EXTRACT (MONTH FROM (N.Mo + 28)) = EXTRACT (MONTH FROM N.Mo)
      LEFT JOIN Invoices I
         ON N.CostCenter = I.CostCenter
         AND N.Vendor = I.Vendor
         AND N.Mo = I.Mo
         AND W.WkNum = I.WkNum
   GROUP BY
      N.Mo,
      N.Vendor_VName,
      N.CostCenter,
      W.WkNum
) X;

Is there a space or other non-printing character I can insert to a varchar2 column that will not be trimmed away and result in a NULL?

I just want to insert a blank into the column so it displays nothing on an SSRS 2008 report. The column is part of a PK so it cannot be NULL. Of course using '' doesn't work as this is seen as NULL in Oracle, and ' ' doesn't work because it's varchar2 and trimmed to be NULL.

So is there a literal value I can insert instead that will display as nothing in SSRS but also can be inserted to a non-nullable varchar2 column in Oracle 11g?

Thinking about it for a moment, I guess something like a tab character could do the job. But I look forward to your suggestions.

UPDATE

Whoops. Guess where the trimming behavior came from? My own RTRIM! Sorry about that. Let's just say I was mislead by my inexperience with Oracle and my frustration over this leading me to determine the error was in the product rather than in my query. But hey, it's not exactly a simple query.

INSERT INTO WeeklyInvoice (GUID, Mo, VendorName, CostCenter, WkNum, Amt)
SELECT
   ExecID,
   Mo,
   VendorName,
   CostCenter,
   WkNum,
   Amt
FROM (
   WITH CostCenters AS (
      SELECT REGEXP_SUBSTR(CostCenterList, '[^,]+', 1, LEVEL) CostCenter
      FROM DUAL
      CONNECT BY LEVEL <= Length(CostCenterList) - Length(Replace(CostCenterList, ',', '')) + 1
   ), Invoices AS (
      SELECT
         TRUNC(I.Invoice_Dte, 'MM') Mo,
         (TRUNC(I.Invoice_Dte, 'W') - TRUNC(I.Invoice_Dte, 'MM')) / 7 + 1 WkNum,
         I.Vendor,
         V.Vendor_VName,
         RTrim(D.Dis_Acct_Unit) CostCenter,
         D.To_Base_Amt
      FROM
         CostCenters CC
         CROSS JOIN prod.IcCompany C
         INNER JOIN prod.ApDistrib D
            ON C.Company = D.Company
            AND D.Dis_Acct_Unit = CC.CostCenter
         INNER JOIN prod.ApInvoice I
            ON D.Invoice = I.Invoice
            AND D.Vendor = I.Vendor
            AND D.Suffix = I.Suffix
            AND D.Company = I.Company
         INNER JOIN prod.ApVenMast V ON I.Vendor = V.Vendor
      WHERE
         D.Cancel_Seq = 0
         AND I.Cancel_Seq = 0
         AND I.Invoice_Dte >= ADD_MONTHS(FromDate, -2)
         AND I.Invoice_Dte < ToDate
   ), Months AS (
      SELECT ADD_MONTHS(FromDate, LEVEL - 1) Mo
      FROM DUAL
      CONNECT BY LEVEL <= MONTHS_BETWEEN(ToDate, ADD_MONTHS(FromDate, -2))
   ), Names AS (
      SELECT DISTINCT
         I.Mo,
         I.Vendor,
         I.Vendor_VName,
         I.CostCenter
      FROM Invoices I
      UNION ALL
      SELECT M.Mo, '0', 'No Paid Invoices', ' '
      FROM Months M
      WHERE
         NOT EXISTS (
            SELECT I.*
            FROM Invoices I
            WHERE I.Mo = M.Mo
         )
   ), Weeks AS (
      SELECT LEVEL WkNum FROM DUAL CONNECT BY LEVEL <= 5
   )
   SELECT
      N.Mo,
      N.Vendor_VName VendorName,
      N.CostCenter,
      W.WkNum,
      Sum(I.To_Base_Amt) Amt
   FROM
      Names N
      INNER JOIN Weeks W
         ON W.WkNum < 5
         OR EXTRACT (MONTH FROM (N.Mo + 28)) = EXTRACT (MONTH FROM N.Mo)
      LEFT JOIN Invoices I
         ON N.CostCenter = I.CostCenter
         AND N.Vendor = I.Vendor
         AND N.Mo = I.Mo
         AND W.WkNum = I.WkNum
   GROUP BY
      N.Mo,
      N.Vendor_VName,
      N.CostCenter,
      W.WkNum
) X;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尘曦 2024-10-18 15:45:10

在我下面的测试中,单个空格未转换为空:

  SQL> CREATE TABLE t (col VARCHAR2 (10) NOT NULL);

  Table created.

  SQL> INSERT INTO t (col)
    2       VALUES (' ');

  1 row created.

  SQL> SELECT CASE col WHEN ' ' THEN 'I am a single space' ELSE 'I am not a space' END AS col FROM t;

  COL
  -------------------
  I am a single space

  1 row selected.

  SQL> SELECT LENGTH (col) FROM t;
  LENGTH(COL)
  -----------
            1

  1 row selected.

您是否在插入之前修剪值?

In my test below, the single space was not converted to a null:

  SQL> CREATE TABLE t (col VARCHAR2 (10) NOT NULL);

  Table created.

  SQL> INSERT INTO t (col)
    2       VALUES (' ');

  1 row created.

  SQL> SELECT CASE col WHEN ' ' THEN 'I am a single space' ELSE 'I am not a space' END AS col FROM t;

  COL
  -------------------
  I am a single space

  1 row selected.

  SQL> SELECT LENGTH (col) FROM t;
  LENGTH(COL)
  -----------
            1

  1 row selected.

Are you trimming the values before inserting?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文