<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Forum PostgreSQL - [SQL] Zmiana typu kolumny]]></title>
	<link rel="self" href="http://forum.postgresql.org.pl/extern.php?action=feed&amp;tid=1232&amp;type=atom"/>
	<updated>2013-03-22T11:07:39Z</updated>
	<generator>PunBB</generator>
	<id>https://forum.postgresql.org.pl/viewtopic.php?id=1232</id>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4113#p4113"/>
			<content type="html"><![CDATA[dla potomnych:)

ALTER TABLE tabela
     ALTER COLUMN kolumna TYPE integer
     USING CAST(kolumna AS integer);]]></content>
			<author>
				<name><![CDATA[KotonPL]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1529</uri>
			</author>
			<updated>2013-03-22T11:07:39Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4113#p4113</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4112#p4112"/>
			<content type="html"><![CDATA[puszczam ta funkcje i pozniej cos takiego, ktore niestety czysci mi dane z kolumny....
ALTER TABLE tabela
    ALTER COLUMN kolumna TYPE integer
    USING pc_chartoint(kolumna);]]></content>
			<author>
				<name><![CDATA[KotonPL]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1529</uri>
			</author>
			<updated>2013-03-21T15:13:11Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4112#p4112</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4111#p4111"/>
			<content type="html"><![CDATA[Myślę że chodziło o podstawienie
[code]
... USING pc_chartoint(..)
[/code]

Funkcja musi być wczesniej utworzona.]]></content>
			<author>
				<name><![CDATA[rski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=26</uri>
			</author>
			<updated>2013-03-21T14:06:30Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4111#p4111</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=4110#p4110"/>
			<content type="html"><![CDATA[mam podobny problem, ale nie wiem jak "zagniezdzic" powyzsza funkcje...
wyskakuje blad
 "BŁĄD:  błąd składni w lub blisko "CREATE"
LINE 3:     USING CREATE OR REPLACE FUNCTION pc_chartoint(chartoconv..."

pomocy:)]]></content>
			<author>
				<name><![CDATA[KotonPL]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1529</uri>
			</author>
			<updated>2013-03-21T13:57:52Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=4110#p4110</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3450#p3450"/>
			<content type="html"><![CDATA[Dziękuję Ci bardzo, właśnie o funkcję "pc_chartoint" się wszystko rozchodziło :-)]]></content>
			<author>
				<name><![CDATA[Shavei]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1368</uri>
			</author>
			<updated>2012-02-16T12:47:46Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3450#p3450</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3449#p3449"/>
			<content type="html"><![CDATA[[code]ALTER TABLE tabela
    ALTER COLUMN kolumna TYPE integer
    USING int4(kolumna);[/code]

lub zamiast int4() dodaj sobie taką funkcję

[code]
CREATE OR REPLACE FUNCTION pc_chartoint(chartoconvert character varying)
  RETURNS integer AS
$BODY$
SELECT CASE WHEN trim($1) SIMILAR TO '[0-9]+' 
        THEN CAST(trim($1) AS integer) 
    ELSE NULL END;

$BODY$
  LANGUAGE 'sql' IMMUTABLE STRICT;[/code]

i wtedy podajesz ją w using zamiast int4(). W przypadku jeśli w tabeli są jakieś dane nieintegerowe, to zamieni je na null. Czy jest Ci to potrzebne i oczekujesz takiego zachowania to nie wiem.]]></content>
			<author>
				<name><![CDATA[AsYlum]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1362</uri>
			</author>
			<updated>2012-02-16T12:40:44Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3449#p3449</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3448#p3448"/>
			<content type="html"><![CDATA[Tylko, że to skasuje mi dane w polu "kolumna", a to zdecydowanie nie wchodzi w grę :-)

EDIT:

Ewentualnie mogę to zrobić przez skrypt PHP, jeżeli w Postgresie nie ma takiej możliwości. Chociaż wolałbym w SQL.]]></content>
			<author>
				<name><![CDATA[Shavei]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1368</uri>
			</author>
			<updated>2012-02-16T12:19:30Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3448#p3448</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Odp: [SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3447#p3447"/>
			<content type="html"><![CDATA[Typ możesz zmienić bezprośrednio przez alter'a np.:

[code]ALTER TABLE tabela ALTER COLUMN kolumna TYPE integer USING NULL;[/code]

Jeśli w kolumnie kolumna znajdują się jakieś wpisy nie-liczbowe, to using na końcu je skasuje (zamiast NULL można podać dowolną wartość akceptowalną dla typu integer np. 0), ew. można zrobić wcześniej Delete na takich wpisach. Wartość domyślną można nadać/zdjąć także przez alter'a np.:

[code]ALTER TABLE tabela ALTER COLUMN kolumna SET DEFAULT 0;
ALTER TABLE
\d tabela
                          Table "public.tabela"
 Column  |  Type   |                      Modifiers                      
---------+---------+-----------------------------------------------------
 id      | integer | not null default nextval('tabela_id_seq'::regclass)
 kolumna | integer | default 0
Indexes:
    "tabela_pkey" PRIMARY KEY, btree (id)

ALTER TABLE tabela ALTER COLUMN kolumna DROP DEFAULT;
ALTER TABLE
\d tabela
                          Table "public.tabela"
 Column  |  Type   |                      Modifiers                      
---------+---------+-----------------------------------------------------
 id      | integer | not null default nextval('tabela_id_seq'::regclass)
 kolumna | integer | 
Indexes:
    "tabela_pkey" PRIMARY KEY, btree (id)[/code]]]></content>
			<author>
				<name><![CDATA[gszpetkowski]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1223</uri>
			</author>
			<updated>2012-02-16T12:14:43Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3447#p3447</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[[SQL] Zmiana typu kolumny]]></title>
			<link rel="alternate" href="https://forum.postgresql.org.pl/viewtopic.php?pid=3446#p3446"/>
			<content type="html"><![CDATA[Szperałem na Forum, ale niestety nie znalazłem rozwiązania (być może źle szukałem). Próbuję zmienić typ pola w tabeli. Na początku miałem zaćmienie i zrobiłem pole typu "character varying" zamiast "integer", dla danych, które będą liczbowe. 

Zauważyłem to dopiero dzisiaj i szybko chciałem to naprawić, a tu zonk :-) Próbowałem zrobić coś takiego:
[code]ALTER TABLE tabela ADD COLUMN kolumna_temp INTEGER DEFAULT 0;
UPDATE tabela SET kolumna_temp = kolumna;[/code]
A później po prostu usunąć "kolumna" i zmienić nazwę "kolumna_temp" na "kolumna". Ale Postgres krzyczy, że "kolumna" jest typu "character varying" i nie zrobi UPDATE do kolumny typu "integer". Niby poprawnie, ale jak to obejść?

Próbowałem dodawać "::integer", ale to nic nie zmienia.

Cały problem wynika z tego, że w polu "kolumna", mam zapisany "wzrost" i gdy próbuję zrobić zapytanie typu:
[code]SELECT * FROM tabela WHERE kolumna BETWEEN 150 AND 180[/code]
Wyrzuca błąd, co w sumie oczywiste, gdy typ kolumny jest zły.

Jak zmienić ten skubany typ kolumny? :-)]]></content>
			<author>
				<name><![CDATA[Shavei]]></name>
				<uri>https://forum.postgresql.org.pl/profile.php?id=1368</uri>
			</author>
			<updated>2012-02-16T11:51:15Z</updated>
			<id>https://forum.postgresql.org.pl/viewtopic.php?pid=3446#p3446</id>
		</entry>
</feed>
