Обсуждение: Create Type with typmod_in

Поиск
Список
Период
Сортировка

Create Type with typmod_in

От
Russell Keane
Дата:
<div class="WordSection1"><p class="MsoNormal">I’m trying to create a custom type (PostgreSQL 9.0) which will
essentiallyauto truncate a string to a certain length.<p class="MsoNormal"> <p class="MsoNormal">The type
(auto_trunc_char)will be used as follows:<p class="MsoNormal"> <p class="MsoNormal">Create table blah (<p
class="MsoNormal">               Some_name auto_trunc_char(40)<p class="MsoNormal">)<p class="MsoNormal"> <p
class="MsoNormal">Canthis be done purely in plpgsql?<p class="MsoNormal">If so, how?<p class="MsoNormal"> <p
class="MsoNormal">Iknow I have to declare the following (at least) but I’m not sure how to flesh out the functions:<p
class="MsoNormal"> <pclass="MsoNormal">create type auto_trunc_char (<p class="MsoNormal">                INTERNALLENGTH
=VARIABLE,<p class="MsoNormal">                INPUT = auto_trunc_char_in_function,<p class="MsoNormal">               
OUTPUT= auto_trunc_char_out_function,<p class="MsoNormal">                TYPMOD_IN = auto_trunc_char_modifier_in<p
class="MsoNormal">);<pclass="MsoNormal"> <p class="MsoNormal"> <p class="MsoNormal"><span
style="font-size:10.0pt">Regards,</span><pclass="MsoNormal"><span style="font-size:12.0pt;font-family:"Times New
Roman","serif";color:#365F91"> </span><pclass="MsoNormalCxSpMiddle"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><spanstyle="font-size:10.0pt;font-family:"Times New
Roman","serif";color:black">RussellKeane</span></b><b><span style="font-size:10.0pt;color:black"></span></b><p
class="MsoNormalCxSpMiddle"style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><span
style="font-size:10.0pt;color:black">INPS</span></b><pclass="MsoNormalCxSpMiddle"
style="mso-margin-top-alt:auto;mso-margin-bottom-alt:auto"><b><spanstyle="font-size:10.0pt;color:black"> </span></b><p
class="MsoNormal"><spanstyle="font-size:9.0pt"><a href="http://www.inps4.co.uk/news/enewsletter/"><span
style="color:blue">Subscribeto the Vision e-newsletter</span></a></span><p class="MsoNormal"><span
style="font-size:9.0pt"><ahref="http://www.inps4.co.uk/my_vision/helpline/support-bulletins"><span
style="color:blue">Subscribeto the Helpline Support Bulletin</span></a></span><p class="MsoNormal"><span
style="font-size:9.0pt;color:black"><imgborder="0" height="14" id="Picture_x0020_1"
src="cid:image001.png@01CC9AFA.4BF01AF0"width="14" />  </span><span style="font-size:9.0pt;color:#E36C0A"><a
href="http://www.inps4.co.uk/rss/helplineblog.rss"><spanstyle="color:#E36C0A">Subscribe to the Helpline Blog RSS
Feed</span></a></span><p class="MsoNormal"> </div><br /><hr /><font color="Black" face="Arial" size="2">Registered
name:In Practice Systems Ltd.<br /> Registered address: The Bread Factory, 1a Broughton Street, London, SW8 3QJ<br />
RegisteredNumber: 1788577<br /> Registered in England<br /> Visit our Internet Web site at www.inps.co.uk<br /> The
informationin this internet email is confidential and is intended solely for the addressee. Access, copying or re-use
ofinformation in it by anyone else is not authorised. Any views or opinions presented are solely those of the author
anddo not necessarily represent those of INPS or any of its affiliates. If you are not the intended recipient please
contactis.helpdesk@inps.co.uk<br /><br /></font> 

Re: Create Type with typmod_in

От
David Johnston
Дата:
On Nov 4, 2011, at 10:01, Russell Keane <Russell.Keane@inps.co.uk> wrote:

I’m trying to create a custom type (PostgreSQL 9.0) which will essentially auto truncate a string to a certain length.

 


Can this be done purely in plpgsql?

If so, how?

 


An explicit cast of a value to varchar(n) causes the value to be truncated to n characters.

E.g.,  '1234567'::varchar(5) -> '12345'

How to integrate that knowledge into a type I do not know.

David J.