pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Datetime conversion in WHERE clause
2+ messages / 2 participants
[nested] [flat]

* Datetime conversion in WHERE clause
@ 2004-09-09 14:23  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 1 reply; 2+ messages in thread

From: Philippe Lang @ 2004-09-09 14:23 UTC (permalink / raw)
  To: pgsql-sql

Hello,

I'm converting a varchar to a date in a search routine which looks like:

----------------------------------------------
CREATE FUNCTION public.search_data(varchar)
  RETURNS SETOF foo1 AS
'
  SELECT DISTINCT

  foo1.*

  FROM foo1
  LEFT JOIN foo2
  ON foo2.fk = foo1.pk

  WHERE
     lower(foo1.text_data1) LIKE lower($1)
  OR lower(foo1.text_data2) LIKE lower($1)
  OR foo1.date_data = $1::text:date

  OR lower(foo2.text_data3) LIKE lower($1)
  OR lower(foo2.text_data4) LIKE lower($1)
'
  LANGUAGE 'sql' VOLATILE;
----------------------------------------------

The problem with the ::text::date conversion is that the whole function
fails if the search string parameter cannot be converted into a valid
date, like 'test%', for example, which is valid for the other text
fields.

Is there a way to force the date conversion to fail sliently, and simply
return a null in case the parameter is not a valid date?

Thanks

Philippe Lang



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

* Re: Datetime conversion in WHERE clause
@ 2004-09-09 18:42  Josh Berkus <josh@agliodbs.com>
  parent: Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 0 replies; 2+ messages in thread

From: Josh Berkus @ 2004-09-09 18:42 UTC (permalink / raw)
  To: Philippe Lang <philippe.lang@attiksystem.ch>; pgsql-sql

Philippe,

> Is there a way to force the date conversion to fail sliently, and simply
> return a null in case the parameter is not a valid date?

You'd need to write a custom function, using an external language that allows 
you to test for valid date values.   I prefer using PL/perlU with 
Date::Manip, but use what you like.

-- 
Josh Berkus
Aglio Database Solutions
San Francisco



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


end of thread, other threads:[~2004-09-09 18:42 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-09-09 14:23 Datetime conversion in WHERE clause Philippe Lang <philippe.lang@attiksystem.ch>
2004-09-09 18:42 ` Josh Berkus <josh@agliodbs.com>

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