I try to execute query:
INSERT INTO table_name
(
timedate_array_field
)
VALUES
(
'{NULL, NULL}'
)
But I get error "Could not convert string to DateTime: 'null'".
Just add single quotes for the value:
INSERT INTO table_name (timedate_array_field)
VALUES ('{NULL, NULL}')
-> SQLfiddle
BTW, the data type is not called "DateTime" or "timedate" in Postgres, but timestamp.
Related
I have a column, which should be VARCHAR; I need to cast these as doubles. Some values, however, are Booleans and trigger the error Cannot cast false to DOUBLE. How do I prevent this is the WHERE cause?
What's the easiest way to accomplish the below in presto?
...
WHERE Type(col) != BOOL
Or
...
WHERE type(col) = VARCHAR
You can use try_cast and filter out nulls:
-- sample data1
WITH dataset (column) AS (
VALUES ('1'),
('not a double')
)
--query
select *
from (
select try_cast(column as double) as column
from dataset
)
where column is not null
Output:
column
1.0
Or use it in where (where try_cast(...) is not null)
I have a CSV file containing about 30m rows with a column that has a date type. But the problem is with its format. PSQL supports '-' delimiters for timestamp but my dates are using '/'. For example, the date should be '2021-02-01 00:00:00' but my date format is '2021/02/01 00:00:00'. Also, I can not open the CSV file and change it manually due to its large size. I am trying to import my data into a temporary table to replace the '/' with '-' and then inserting them to a new table and I am using the following command(it is not the real table and it is just an example):
CREATE TABLE TMP(
dt VARCHAR
)
CREATE TABLE other_tmp(
dt TIMESTAMP
)
INSERT INTO TMP VALUES('2020/01/02 22:33:11');
INSERT INTO other_tmp(dt)
SELECT dt,
REPLACE(dt, '/', '-')
FROM TMP
I get an error with replace function when I want to run it.
Does anybody know that how can I solve this problem? Or even is it possible to manipulate the column in the original table?
Try this,
In this case, your column "dt" is of type timestamp without time zone but the expression is of type text. So the text should cast to timestamp as below.
CREATE TABLE IF NOT EXISTS TMP
(
dt VARCHAR
);
CREATE TABLE IF NOT EXISTS other_tmp
(
dt TIMESTAMP
);
INSERT INTO TMP
VALUES ('2020/01/02 22:33:11'::TIMESTAMP);
INSERT INTO other_tmp(dt)
SELECT REPLACE(dt, '/', '-')::TIMESTAMP AS dt
FROM TMP;
Your first error is that you have two columns in the SELECT list, but only one column in the INSERT target.
To convert a string to a timestamp, use to_timstamp()
insert into other_tmp(dt)
select to_timestamp(dt, 'yyyy/mm/dd hh24:mi:ss')
from tmp;
Online example
I am trying to change type of columns from varchar to integer. When I run the following command
alter table audio_session
alter column module_type_cd type INT USING module_type_cd::integer,
alter column sound_type_cd type INT USING sound_type_cd::integer
I got the error:
ERROR: invalid input syntax for integer: "string"
SQL state: 22P02
The error does not make any sense. There is no "string" in the command at all. What did I do wrong?
The columns audio_session.module_type_cd or audio_session.sound_type_cd has at least one non-numeric value among all values of each row. So, it's impossible to convert to a completely numeric data type.
As an example :
create table Table1( col1 varchar(10), col2 varchar(10) );
insert into Table1 values('1','1');
insert into Table1 values('2','2');
insert into Table1 values('3','C3');
select CAST(coalesce(col1, '0') AS integer) as converted from Table1; -- this works
select CAST(coalesce(col2, '0') AS integer) as converted from Table1; -- but, this doesn't
SQL Fiddle Demo
There is no "string" in the command at all.
But must be in the data. The command you're going to use tries to cast all the columns values to integer. Check this:
select *
from audio_session
where module_type_cd = 'string' or sound_type_cd = 'string';
i have created a custom Postgres type with :
CREATE TYPE new_type AS (new_date timestamp, some_int bigint);
i have a table that store arrays of new_type like:
CREATE TABLE new_table (
table_id uuid primary key,
new_type_list new_type[] not null
)
and i insert data in this table with something like this:
INSERT INTO new_table VALUES (
'*inApplicationGeneratedRandomUUID*',
ARRAY[[NOW()::timestamp, '146252'::bigint],
[NOW()::timestamp, '526685'::bigint]]::new_type[]
)
and i get this error
ERROR: cannot cast type timestamp without time zone to new_type
What am I missing?
I've also tried array syntax that uses {} but nothing better.
The easiest way would probably be:
INSERT INTO new_table VALUES (
'9fd92c53-d0d8-4aba-8925-1bd648d565f2'::uuid,
ARRAY[ row(now(), 146252)::new_type,
row(now(), 526685)::new_type
] );
Note that you have to cast the row type to ::new_type.
As an alternative, you could also write:
INSERT INTO new_table VALUES (
'9fd92c53-d0d8-4aba-7925-1ad648d565f2'::uuid,
ARRAY['("now", 146252)'::new_type,
'("now", 526685)'::new_type
] );
Check PostgreSQL documentation about Composite Value Input.
I have a column in my table called startdate. It is in string format. Most of the fields are 'NULL'. I am copying this column to another table which data type is 'Date'.
How can I convert all the values from string to Date in SQL.
I have tried this code:
INSERT INTO Destination_Table [new_date]
SELECT CONVERT(DATE,[startdate],103)
FROM Source_Table
nullif([startdate],'NULL') returns [startdate] unless it equals to 'NULL' and then it returns NULL (a real NULL, not the string 'NULL')
INSERT INTO Destination_Table [new_date]
SELECT CONVERT(DATE,nullif([startdate],'NULL'),103)
from Source_Table
For learning purposes, here are some expressions with the same results:
nullif(x,y)
case when x=y then null else x end
case x when y then null else x end
It looks like you are using MSSQL. If you are using MSSQL 2012, the following code should work :
INSERT INTO Destination_Table [new_date]
SELECT IIF([startdate] = "NULL", null, CONVERT(DATE,[startdate],103))
FROM Source_Table
What this does, is use the IIF() method to check the value of [startdate] and if the value is the text "NULL", then return the actual null value which can be allowed in most fields unless you have null disabled on the Destination_Table.[new_date] field.
Since the Date field can only accept and store Date/Time/Date&Time/(actual null) information, the text "NULL" is not valid.
Following is the equivalent for MySQL
INSERT INTO Destination_Table [new_date]
SELECT IF([startdate] == 'NULL', null, CONVERT(DATE,[startdate],103))
FROM Source_Table
(although I am unsure MySQL allows a conversion code as a param to CONVERT() )