Insert into enum type using IF statement psql - sql

Im copying data in from a csv file, I want insert a value which depends on if the data in the csv file reads yes or no
CREATE TYPE colour_type as ENUM ('red', 'blue');
CREATE TABLE colour_table
(
colour colour_type
);
CREATE TABLE Dummy
(
colour_type_dummy varchar(40)
);
\copy TABLE FROM 'colours.csv' WITH (FORMAT CSV, HEADER);
-- This is the part where I cant figure out the syntax for the IF statement
INSERT INTO colour_table (colour)
SELECT 'red' IF colour_type_dummy = 'yes' else 'blue' FROM Dummy

You can try to use CASE WHEN expression
INSERT INTO colour_table (colour)
SELECT CASE WHEN colour_type_dummy = 'yes' THEN 'red'::colour_type ELSE 'blue'::colour_type END
FROM Dummy
sqlfiddle

Related

Hive replacing a value with null value in hive

I have a hive table where instead of NULL, it represents null record as string with value 'N/A',
Is there any query that will convert that 'N/A' into NULL value datatype in hive.
You can try if or case when or decode like below -
select
case when mycol='N/A' then NULL else mycol end as mycol,
if(mycol='N/A', NULL,mycol) as mycol_if
from mytable
The "CASE WHEN" syntax may be helpful.
Create hive table and import data.
hive> create table mytable(
name string,
mycol string
)
row format delimited fields terminated by "\t";
hive> load data local inpath '/opt/tempdata/mytable.txt' into table mytable;
sql syntax:
hive> select
name,
case mycol when 'N/A' then NULL else mycol end as mycol
from mytable
Hope it helps you.

how can I reference another column inside postgres insert query?

I have the following table:
CREATE TABLE tab (
id SERIAL PRIMARY KEY,
code TEXT NOT NULL,
data TEXT
)
In some cases, I'd like to insert a new row ensuring that the code column is generated by the id column. In other cases the code is provided by the user.
For example:
INSERT INTO tab(code, data) VALUES ('code: ' || id::TEXT, 'my data');
The expected result is something like:
id
code
data
1
code: 1
abc
2
code: 2
xyz
INSERT INTO tab(code, data) VALUES ('user.provided.code', 'my data');
The expected result is something like:
id
code
data
1
code: 1
abc
2
code: 2
xyz
3
user.provided.code
xyz
Is it possibile in one statement?
It sounds like you want to default the coder to something based on the id. Unfortunately, this doesn't work in Postgres:
create table tab (
id integer primary key generated always as identity,
code text not null default ('code '||id::text),
data text
);
One option is a single statement that does both an insert and update:
with i as (
insert into tab (code, data)
values ('', 'my data'
returning *
)
update tab
set code = 'code: ' || id::TEXT
where tab.id in (select i.id from i);
Another is to use a trigger that assigns the value.
Use INSERT INTO .. SELECT as follows:
INSERT INTO tab(code, data)
select 'code: ' || id::TEXT, 'my data' from tab;
Note: In newly added data(above insert), you are missing to add id column data or I think it is auto generated.

How to insert table in Databricks using magic SQL operator

I have create the following SQL table in databricks (using the magic %sql) as follows:
%sql
CREATE TABLE mytable (
id INT
,name STRING
,met_area_name STRING
,state STRING
,type STRING
) USING CSV
I am now trying insert data into the table using the following command:
%sql
INSERT INTO TABLE mytable VALUES (id,name,type)
SELECT DISTINCT criteria1, criteria2, 'b'
FROM tablex
WHERE somecriteria1 = 0
ORDER BY somecriteria2;
However, I'm getting the following error:
Error in SQL statement: ParseException:
mismatched input 'FROM' expecting <EOF>(line 2, pos 2)
== SQL ==
INSERT INTO TABLE mytable VALUES (id,name,type)
FROM tablex
--^^^
WHERE somecriteria1 = 0
ORDER BY somecriteria2
I'm sure there is something very obvious that I'm missing, but I can't see it.
Any assistance much appreciated.
Cheers

Sqlite: LIKE matches value, but equality doesn't

I have very simple SQLite table:
CREATE TABLE IF NOT EXISTS `settings` (`Name` TEXT PRIMARY KEY, `Value` TEXT);
I use 2 following queries:
SELECT `Value` FROM `settings` WHERE `Name` LIKE 'MainTabControl.active';
(returns 1 row)
and
SELECT `Value` FROM `settings` WHERE `Name` = 'MainTabControl.active';
(returns 0 rows)
Row with Name column value MainTabControl.active definitely exists (I see it when do SELECT *), doesn't have any spaces at beginning and end, and has the same case of all characters.
What is reason of different behavior of equality operator and LIKE?
You may be running into sqlite's type system. Full details are on http://www.sqlite.org/datatype3.html, but the relevant bit is this:
A TEXT value is less than a BLOB value
My guess is that you (intentionally or not) stored the name column as a BLOB rather than TEXT. This will not be = to the text value, but will be LIKE it.
$ sqlite3
SQLite version 3.7.15 2012-10-15 18:02:57
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table settings (name, value) ;
sqlite> insert into settings values ('MainTabControl.active','textname') ;
sqlite> insert into settings values (cast ('MainTabControl.active' as blob),'blobname') ;
sqlite> select value from settings where name = 'MainTabControl.active' ;
textname
sqlite> select value from settings where name like 'MainTabControl.active' ;
textname
blobname
sqlite> select value, typeof(name) from settings where name like 'MainTabControl.active' ;
textname|text
blobname|blob
sqlite>
The case sensitivity of the LIKE operator is determined
by the PRAGMA case_sensitive_like command.
The default behavior of the LIKE operator is to ignore case for
ASCII characters.
sqlite> SELECT 'A' = 'a';
0
sqlite> SELECT 'A' LIKE 'a';
1
sqlite> PRAGMA case_sensitive_like = TRUE;
sqlite> SELECT 'A' LIKE 'a';
0
If we set it to TRUE, the result is equal to the = operator.
see http://www.sqlite.org/pragma.html#pragma_case_sensitive_like

TSQL Insert the column default value from Case Statement

I'd like to use the column's default value in an stored procedure insert, so that I don't have to repeat the default value in multiple places (it could change... DRY principle).
The T-SQL INSERT operation has a handy 'default' keyword that I can use as follows:
Declare #newA varchar(10)
Set #newA = 'Foo2'
-- I can use "default" like so...
Insert into Table_1 (
A,
B)
Values (
#newA,
default)
However, If I need to do something conditional, I can't seem to get the case statement to return 'default'.
-- How do I use 'default' in a case statement?
INSERT INTO Table_1 (
A,
B )
VALUES (
#newA,
CASE WHEN (#newA <> 'Foo2') THEN 'bar' ELSE default END)
-- > yeilds "Incorrect syntax near the keyword 'default'."
I could insert the default, and then update as needed like so:
INSERT INTO Table_1 (
A,
B )
VALUES (
#newA,
default)
UPDATE Table_1
SET B = CASE WHEN (A <> 'Foo2') THEN 'bar' ELSE B END
WHERE ID = SCOPE_IDENTITY()
But I'd really like somebody to tell me "There's a better way..."
Here's a table definition for this example if it helps...
CREATE TABLE dbo.Table_1 (
ID int NOT NULL IDENTITY (1, 1),
A varchar(10) NULL,
B varchar(10) NULL )
GO
ALTER TABLE dbo.Table_1 ADD CONSTRAINT DF_Table_1_A DEFAULT 'A-Def' FOR A
GO
ALTER TABLE dbo.Table_1 ADD CONSTRAINT DF_Table_1_B DEFAULT 'B-Def' FOR B
GO
default only works from within a VALUES() block, which does not seem to be an acceptable value in a CASE statement; you could use an if statement to determine what to insert:
DECLARE #newA varchar(10) = 'Foo2'
IF (#newA <> 'Foo2')
BEGIN
INSERT INTO Table_1 (A, B)
SELECT #newA, 'bar'
END
ELSE
BEGIN
--If you are using default values, you do not have to specify the column
INSERT INTO Table_1 (A)
SELECT #newA
END
I think this is better than updating after an insert, so that you only insert correct data into your table. It also keeps the number of INSERTS/UPDATES to 1. You should also be careful when you using ##IDENTITY due to scoping. Consider looking into SCOPE_IDENTITY().