pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
converting from number to text
4+ messages / 3 participants
[nested] [flat]

* converting from number to text
@ 1999-06-16 09:31 Remigiusz Sokolowski <rems@gdansk.sprint.pl>
  1999-06-16 11:25 ` Re: [SQL] converting from number to text Herouth Maoz <herouth@oumail.openu.ac.il>
  1999-06-16 11:34 ` Re: [SQL] converting from number to text Anatoly K. Lasareff <tolik@icomm.ru>
  0 siblings, 2 replies; 4+ messages in thread

From: Remigiusz Sokolowski @ 1999-06-16 09:31 UTC (permalink / raw)
  To: pgsql-sql

Hi!
I need convert number(float or integer) to text, but my version of
postgres (i.e. 6.3.2) seems to have not such a function?
Is it may be in newer versions or is there any work-around?
TIA
	Rems
p.s. concept is to have field, which contain date in timestamp form
(number of seconds from 'epoch'), but in which I could insert also value
'until cancellation' or any other text.

-------------------------------------------------------------------*------------
Remigiusz Sokolowski      e-mail: rems@gdansk.sprint.pl           * *		
-----------------------------------------------------------------*****----------




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] converting from number to text
  1999-06-16 09:31 converting from number to text Remigiusz Sokolowski <rems@gdansk.sprint.pl>
@ 1999-06-16 11:25 ` Herouth Maoz <herouth@oumail.openu.ac.il>
  1999-06-16 11:46   ` Re: [SQL] converting from number to text Remigiusz Sokolowski <rems@gdansk.sprint.pl>
  1 sibling, 1 reply; 4+ messages in thread

From: Herouth Maoz @ 1999-06-16 11:25 UTC (permalink / raw)
  To: Remigiusz Sokolowski <rems@gdansk.sprint.pl>; pgsql-sql

At 12:31 +0300 on 16/06/1999, Remigiusz Sokolowski wrote:


> I need convert number(float or integer) to text, but my version of
> postgres (i.e. 6.3.2) seems to have not such a function?
> Is it may be in newer versions or is there any work-around?
> TIA
> 	Rems
> p.s. concept is to have field, which contain date in timestamp form
> (number of seconds from 'epoch'), but in which I could insert also value
> 'until cancellation' or any other text.

I think a better approach to your problem would be to add a status field.
The status field will contain anysuch additional information, and the date
field will only contain a date when the given status demands it. How you
display it in a report is a different matter - You can write a function
that queries the status field and substitutes it with the date value if the
status is so-and-so.

Herouth

--
Herouth Maoz, Internet developer.
Open University of Israel - Telem project
http://telem.openu.ac.il/~herutma





^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] converting from number to text
  1999-06-16 09:31 converting from number to text Remigiusz Sokolowski <rems@gdansk.sprint.pl>
  1999-06-16 11:25 ` Re: [SQL] converting from number to text Herouth Maoz <herouth@oumail.openu.ac.il>
@ 1999-06-16 11:46   ` Remigiusz Sokolowski <rems@gdansk.sprint.pl>
  0 siblings, 0 replies; 4+ messages in thread

From: Remigiusz Sokolowski @ 1999-06-16 11:46 UTC (permalink / raw)
  To: Herouth Maoz <herouth@oumail.openu.ac.il>; +Cc: pgsql-sql

> > I need convert number(float or integer) to text, but my version of
> > postgres (i.e. 6.3.2) seems to have not such a function?
> > Is it may be in newer versions or is there any work-around?
> > TIA
> > 	Rems
> > p.s. concept is to have field, which contain date in timestamp form
> > (number of seconds from 'epoch'), but in which I could insert also value
> > 'until cancellation' or any other text.
> 
> I think a better approach to your problem would be to add a status field.
> The status field will contain anysuch additional information, and the date
> field will only contain a date when the given status demands it. How you
> display it in a report is a different matter - You can write a function
> that queries the status field and substitutes it with the date value if the
> status is so-and-so.

This is very good idea
	Thanks :-)
	Rem

-------------------------------------------------------------------*------------
Remigiusz Sokolowski      e-mail: rems@gdansk.sprint.pl           * *		
-----------------------------------------------------------------*****----------




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: [SQL] converting from number to text
  1999-06-16 09:31 converting from number to text Remigiusz Sokolowski <rems@gdansk.sprint.pl>
@ 1999-06-16 11:34 ` Anatoly K. Lasareff <tolik@icomm.ru>
  1 sibling, 0 replies; 4+ messages in thread

From: tolik@icomm.ru @ 1999-06-16 11:34 UTC (permalink / raw)
  To: Remigiusz Sokolowski <rems@gdansk.sprint.pl>; +Cc: pgsql-sql

>>>>> "fRS" == Remigiusz Sokolowski <rems@gdansk.sprint.pl> writes:

 fRS> Hi!
 fRS> I need convert number(float or integer) to text, but my version of
 fRS> postgres (i.e. 6.3.2) seems to have not such a function?
 fRS> Is it may be in newer versions or is there any work-around?
 fRS> TIA
 fRS> Rems
 fRS> p.s. concept is to have field, which contain date in timestamp form
 fRS> (number of seconds from 'epoch'), but in which I could insert also value
 fRS> 'until cancellation' or any other text.

I send text of four function for int|float <-> text conversion.

 ==============================
Headers of the functions (you must replace DESTLIB to the real name of 
your .so file):

void * text2float(text *t);
/*sql create function text2float(text) returns float as 'DESTLIB' language 'c'*/

text* float2text(float8 *f, int prec);
/*sql create function float2text(float, int) returns text as 'DESTLIB' language 'c'*/

int4  text2int(text* t);
/*sql create function text2int(text) returns int as 'DESTLIB' language 'c'*/

text* int2text(int4 i);
/*sql create function int2text(int) returns text as 'DESTLIB' language 'c'*/
 ===============================

Bodies of the functions:

void * text2float(text *t)
{
  int l;
  char *c;
  float8 * ret;

  if(!t)
    return NULL;

  l = VARSIZE(t) - VARHDRSZ + 1;
  c = palloc(l);

  memset(c, 0, l);
  memcpy(c, VARDATA(t), l);
  ret = palloc(sizeof(float8));
  *ret = strtod(c, NULL);
  return ret;
}

text* float2text(float8 *f, int prec)
{
  char s[40];
  char fmt[10];
  text* ret;
  int l;

  if(!f)
    return NULL;

  sprintf(fmt, "%%.%dlf", prec);
  sprintf(s, fmt, *f);

  l = VARHDRSZ + strlen(s);
  ret = palloc(l);
  VARSIZE(ret) = l;
  memcpy(VARDATA(ret), s, strlen(s));

  return ret;
}

int4  text2int(text* t)
{
  char* buf;
  int   l;

  if (!t)
    return 0;

  l = VARSIZE(t) - VARHDRSZ;
  buf = palloc(l + 1);
  memset(buf, 0, l + 1);
  memcpy(buf, VARDATA(t), l);

  return atoi(buf);
}

text* int2text(int4 i)
{
  text *ret;
  char s[20];
  int l;

  sprintf(s, "%d", i);
  l = VARHDRSZ + strlen(s);
  ret = palloc(l);
  VARSIZE(ret) = l;
  memcpy(VARDATA(ret), s, l - VARHDRSZ);

  return ret;
}



 ===============================

-- 
Anatoly K. Lasareff              Email:       tolik@icomm.ru 
Senior programmer



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~1999-06-16 11:46 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-06-16 09:31 converting from number to text Remigiusz Sokolowski <rems@gdansk.sprint.pl>
1999-06-16 11:25 ` Herouth Maoz <herouth@oumail.openu.ac.il>
1999-06-16 11:46   ` Remigiusz Sokolowski <rems@gdansk.sprint.pl>
1999-06-16 11:34 ` Anatoly K. Lasareff <tolik@icomm.ru>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox