pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Fixing faulty dates - select on day part of a date field
7+ messages / 4 participants
[nested] [flat]

* Fixing faulty dates - select on day part of a date field
@ 2001-11-29 11:31 Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 12:06 ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
  2001-11-29 12:13 ` Re: Fixing faulty dates - select on day part of a date field Patrik Kudo <kudo@partitur.se>
  0 siblings, 2 replies; 7+ messages in thread

From: Gary Stainburn @ 2001-11-29 11:31 UTC (permalink / raw)
  To: pgsql-sql

Hi all,

I've got a problem with dates on one of my tables. I've been inserting dates 
in the format 'dd/mm/ccyy' which for the days 13-31 for each month worked 
fine.

The problem I have is that for the days 01-12 for each month, the date was 
interpretted as 'mm/dd/ccyy'.  Now I know about it I need to fix it.

Two questions.

1) how can I select on part of a date?  I need to select all records where 
the day is not > 12.
2) can I do this in a single update, i.e. can I do something around

update calls set xdate =  ???? where ??????;

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     



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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2001-11-29 12:06 ` Markus Bertheau <twanger@bluetwanger.de>
  2001-11-29 14:00   ` Re: Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  1 sibling, 1 reply; 7+ messages in thread

From: Markus Bertheau @ 2001-11-29 12:06 UTC (permalink / raw)
  To: Gary Stainburn <gary.stainburn@ringways.co.uk>; +Cc: pgsql-sql

On Thu, 2001-11-29 at 12:31, Gary Stainburn wrote:
> I've got a problem with dates on one of my tables. I've been inserting dates 
> in the format 'dd/mm/ccyy' which for the days 13-31 for each month worked 
> fine.
> 
> The problem I have is that for the days 01-12 for each month, the date was 
> interpretted as 'mm/dd/ccyy'.  Now I know about it I need to fix it.
> 
> Two questions.
> 
> 1) how can I select on part of a date?  I need to select all records where 
> the day is not > 12.

select date_part('part', attribute)

where part is one of week, day, year ans so on

standards compliant way is

select extract(part from attribute)

so:

select * from table where date_part('day', attribute) < 13;

> 2) can I do this in a single update, i.e. can I do something around
> 
> update calls set xdate =  ???? where ??????;

What exactly do you want to achieve?

Markus Bertheau

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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 12:06 ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
@ 2001-11-29 14:00   ` Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 14:36     ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
  2001-11-30 01:34     ` Re: Fixing faulty dates - select on day part of a date field Christopher Kings-Lynne <chriskl@familyhealth.com.au>
  0 siblings, 2 replies; 7+ messages in thread

From: Gary Stainburn @ 2001-11-29 14:00 UTC (permalink / raw)
  To: Markus Bertheau <twanger@bluetwanger.de>; +Cc: pgsql-sql

On Thursday 29 November 2001 12:06 pm, Markus Bertheau wrote:
> On Thu, 2001-11-29 at 12:31, Gary Stainburn wrote:
> > I've got a problem with dates on one of my tables. I've been inserting
> > dates in the format 'dd/mm/ccyy' which for the days 13-31 for each month
> > worked fine.
> >
> > The problem I have is that for the days 01-12 for each month, the date
> > was interpretted as 'mm/dd/ccyy'.  Now I know about it I need to fix it.
> >
> > Two questions.
> >
> > 1) how can I select on part of a date?  I need to select all records
> > where the day is not > 12.
>
> select date_part('part', attribute)
>
> where part is one of week, day, year ans so on
>
> standards compliant way is
>
> select extract(part from attribute)
>
> so:
>
> select * from table where date_part('day', attribute) < 13;
>
> > 2) can I do this in a single update, i.e. can I do something around
> >
> > update calls set xdate =  ???? where ??????;
>
> What exactly do you want to achieve?

Thanks for the information.  Basically what I want to be able to do is 
correct the dates that are wrong by swapping the day and month parts.

For example, a date entered as 23/11/2000 is correctly stored as 2000/11/23 
while a date entered as 07/11/2000 (7th Nov 2000) is incorrectly stored as 
2000/07/11.  I need to be able to access it using the select you gave above, 
and then re-store the date as 2000/11/07.

something like:

update calls set date_part('day',xdate) = date_part('month',xdate),           
       date_part('month',xdate) = date_part('day',xdate) 
       where date_part('day',xdate) < 13 and xdate < '2001/11/08';

(2001/11/08 is when I found/fixed the insert problem)

>
> Markus Bertheau

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     



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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 12:06 ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
  2001-11-29 14:00   ` Re: Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2001-11-29 14:36     ` Markus Bertheau <twanger@bluetwanger.de>
  1 sibling, 0 replies; 7+ messages in thread

From: Markus Bertheau @ 2001-11-29 14:36 UTC (permalink / raw)
  To: Gary Stainburn <gary.stainburn@ringways.co.uk>; +Cc: pgsql-sql

On Thu, 2001-11-29 at 15:00, Gary Stainburn wrote:
> On Thursday 29 November 2001 12:06 pm, Markus Bertheau wrote:
> > On Thu, 2001-11-29 at 12:31, Gary Stainburn wrote:
> > > I've got a problem with dates on one of my tables. I've been inserting
> > > dates in the format 'dd/mm/ccyy' which for the days 13-31 for each month
> > > worked fine.
> > >
> > > The problem I have is that for the days 01-12 for each month, the date
> > > was interpretted as 'mm/dd/ccyy'.  Now I know about it I need to fix it.
> > >
> > > Two questions.
> > >
> > > 1) how can I select on part of a date?  I need to select all records
> > > where the day is not > 12.
> >
> > select date_part('part', attribute)
> >
> > where part is one of week, day, year ans so on
> >
> > standards compliant way is
> >
> > select extract(part from attribute)
> >
> > so:
> >
> > select * from table where date_part('day', attribute) < 13;
> >
> > > 2) can I do this in a single update, i.e. can I do something around
> > >
> > > update calls set xdate =  ???? where ??????;
> >
> > What exactly do you want to achieve?
> 
> Thanks for the information.  Basically what I want to be able to do is 
> correct the dates that are wrong by swapping the day and month parts.
> 
> For example, a date entered as 23/11/2000 is correctly stored as 2000/11/23 
> while a date entered as 07/11/2000 (7th Nov 2000) is incorrectly stored as 
> 2000/07/11.  I need to be able to access it using the select you gave above, 
> and then re-store the date as 2000/11/07.
> 
> something like:
> 
> update calls set date_part('day',xdate) = date_part('month',xdate),           
>        date_part('month',xdate) = date_part('day',xdate) 
>        where date_part('day',xdate) < 13 and xdate < '2001/11/08';
> 
> (2001/11/08 is when I found/fixed the insert problem)

update calls set xdate = date_part('year', data) || '-' ||
date_part('day', data) || '-' || date_part('month', data) where ...;

|| is a string concatenation operator.
The query above swaps month and day.

The query only handles dates. If xdate also contains time information
you have to add it.

Markus Bertheau

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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 12:06 ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
  2001-11-29 14:00   ` Re: Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2001-11-30 01:34     ` Christopher Kings-Lynne <chriskl@familyhealth.com.au>
  2001-11-30 09:19       ` Re: Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  1 sibling, 1 reply; 7+ messages in thread

From: Christopher Kings-Lynne @ 2001-11-30 01:34 UTC (permalink / raw)
  To: Gary Stainburn <gary.stainburn@ringways.co.uk>; Markus Bertheau <twanger@bluetwanger.de>; +Cc: pgsql-sql

> update calls set date_part('day',xdate) =
> date_part('month',xdate),
>        date_part('month',xdate) = date_part('day',xdate)
>        where date_part('day',xdate) < 13 and xdate < '2001/11/08';
>
> (2001/11/08 is when I found/fixed the insert problem)

You should always use the ISO date format - it avoids all these problems,
and is unambiguous.  It is: YYYY-MM-DD

Chris




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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-29 12:06 ` Re: Fixing faulty dates - select on day part of a date field Markus Bertheau <twanger@bluetwanger.de>
  2001-11-29 14:00   ` Re: Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
  2001-11-30 01:34     ` Re: Fixing faulty dates - select on day part of a date field Christopher Kings-Lynne <chriskl@familyhealth.com.au>
@ 2001-11-30 09:19       ` Gary Stainburn <gary.stainburn@ringways.co.uk>
  0 siblings, 0 replies; 7+ messages in thread

From: Gary Stainburn @ 2001-11-30 09:19 UTC (permalink / raw)
  To: Christopher Kings-Lynne <chriskl@familyhealth.com.au>; +Cc: pgsql-sql

Hi Chris,

I do now, hence the explicit date check of 2001/11/08 which is when I fixed 
the script.  BTW, it doesn't seem to matter whether I use '/' or '-'.

Gary

On Friday 30 November 2001 1:34 am, Christopher Kings-Lynne wrote:
> > update calls set date_part('day',xdate) =
> > date_part('month',xdate),
> >        date_part('month',xdate) = date_part('day',xdate)
> >        where date_part('day',xdate) < 13 and xdate < '2001/11/08';
> >
> > (2001/11/08 is when I found/fixed the insert problem)
>
> You should always use the ISO date format - it avoids all these problems,
> and is unambiguous.  It is: YYYY-MM-DD
>
> Chris
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html

-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     



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

* Re: Fixing faulty dates - select on day part of a date field
  2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
@ 2001-11-29 12:13 ` Patrik Kudo <kudo@partitur.se>
  1 sibling, 0 replies; 7+ messages in thread

From: Patrik Kudo @ 2001-11-29 12:13 UTC (permalink / raw)
  To: Gary Stainburn <gary.stainburn@ringways.co.uk>; +Cc: pgsql-sql

Hi

1. You could use date_part(). Try these things

 select date_part('day', now());
 select date_part('month', now());
 select date_part('year', now());

2. Yeah, should be able to do it in one update

Regards,
Patrik Kudo

On Thu, 29 Nov 2001, Gary Stainburn wrote:

> Hi all,
>
> I've got a problem with dates on one of my tables. I've been inserting dates
> in the format 'dd/mm/ccyy' which for the days 13-31 for each month worked
> fine.
>
> The problem I have is that for the days 01-12 for each month, the date was
> interpretted as 'mm/dd/ccyy'.  Now I know about it I need to fix it.
>
> Two questions.
>
> 1) how can I select on part of a date?  I need to select all records where
> the day is not > 12.
> 2) can I do this in a single update, i.e. can I do something around
>
> update calls set xdate =  ???? where ??????;
>
> --
> Gary Stainburn
>
> This email does not contain private or confidential material as it
> may be snooped on by interested government parties for unknown
> and undisclosed purposes - Regulation of Investigatory Powers Act, 2000
>
> ---------------------------(end of broadcast)---------------------------
> TIP 5: Have you checked our extensive FAQ?
>
> http://www.postgresql.org/users-lounge/docs/faq.html
>




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


end of thread, other threads:[~2001-11-30 09:19 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2001-11-29 11:31 Fixing faulty dates - select on day part of a date field Gary Stainburn <gary.stainburn@ringways.co.uk>
2001-11-29 12:06 ` Markus Bertheau <twanger@bluetwanger.de>
2001-11-29 14:00   ` Gary Stainburn <gary.stainburn@ringways.co.uk>
2001-11-29 14:36     ` Markus Bertheau <twanger@bluetwanger.de>
2001-11-30 01:34     ` Christopher Kings-Lynne <chriskl@familyhealth.com.au>
2001-11-30 09:19       ` Gary Stainburn <gary.stainburn@ringways.co.uk>
2001-11-29 12:13 ` Patrik Kudo <kudo@partitur.se>

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