Length Function

21 views
Skip to first unread message

Dushyant Bhatia

unread,
Jan 15, 2021, 5:49:55 AM1/15/21
to Better Oracle functions support
Hello

I can see that the length funtion works only for character data type. How can I make it work for any other data type.?

Please advise.

Dushyant

Pavel Stehule

unread,
Jan 15, 2021, 6:08:47 AM1/15/21
to orafce-...@googlegroups.com
Hi

pá 15. 1. 2021 v 11:49 odesílatel Dushyant Bhatia <dushy...@gmail.com> napsal:
Hello

I can see that the length funtion works only for character data type. How can I make it work for any other data type.?

Your query is too generic. Can you send an example, what you want?

Regards

Pavel

Please advise.

Dushyant

--
You received this message because you are subscribed to the Google Groups "Better Oracle functions support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orafce-genera...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orafce-general/01da16e9-5abc-499e-8cbd-ea29a49ec3d2n%40googlegroups.com.

Dushyant Bhatia

unread,
Jan 15, 2021, 7:39:27 AM1/15/21
to Better Oracle functions support
Hey Pavel

Thanks for your quick response. The issue is something like this. 

If I execute

select length ('1') from dual;

It returns to me the correct value

but if i try to execute 

select length (1) from dual;

it throws an error.

Please let me know if you need more information

Thanks & Regards
Dushyant

Pavel Stehule

unread,
Jan 15, 2021, 9:08:19 AM1/15/21
to orafce-...@googlegroups.com


pá 15. 1. 2021 v 13:39 odesílatel Dushyant Bhatia <dushy...@gmail.com> napsal:
Hey Pavel

Thanks for your quick response. The issue is something like this. 

If I execute

select length ('1') from dual;

It returns to me the correct value

but if i try to execute 

select length (1) from dual;

it throws an error.

Please let me know if you need more information

length is Postgres function, and it exists only for text types. Oracle is not type strict, and allows almost all possible conversions implicitly. Postgres is working differently.

you need to explicitly cast to text. This is preferred way

postgres=# select length(10::text);
┌────────┐
│ length │
╞════════╡
│      2 │
└────────┘
(1 row)

or you can write own SQL function

CREATE OR REPLACE FUNCTION length(anyelement)
RETURNS text AS $$
  SELECT length($1::text)
$$ LANGUAGE sql;

postgres=# select length(10);
┌────────┐
│ length │
╞════════╡
│ 2      │
└────────┘
(1 row)

Regards

Pavel





Thanks & Regards
Dushyant

On Friday, 15 January 2021 at 16:38:47 UTC+5:30 Pavel Stehule wrote:
Hi

pá 15. 1. 2021 v 11:49 odesílatel Dushyant Bhatia <dushy...@gmail.com> napsal:
Hello

I can see that the length funtion works only for character data type. How can I make it work for any other data type.?

Your query is too generic. Can you send an example, what you want?

Regards

Pavel

Please advise.

Dushyant

--
You received this message because you are subscribed to the Google Groups "Better Oracle functions support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orafce-genera...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/orafce-general/01da16e9-5abc-499e-8cbd-ea29a49ec3d2n%40googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Better Oracle functions support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orafce-genera...@googlegroups.com.

Dushyant Bhatia

unread,
Jan 16, 2021, 12:46:19 AM1/16/21
to Better Oracle functions support
Thanks for this. I have a similar question on Decode. Will create a new thread for that.
Reply all
Reply to author
Forward
0 new messages