SQLite problem selecting two columns as one - sqlite

Basic table with empname and empdpt.
In a Sql Server table, I can do Select empname + ' ' + empdpt as expr1 no problem.
Can't do the same using Sqlite!!
When I try to combine two columns [with data], I get back a 0.
I've tried in sqliteman and sqliteadmin as well as Server Explorer in VS.

Try using the following:
SELECT ("test" || " " || "test2") AS expr1 ;
Update
If these are columns you can do something similar: SELECT (column1 || " " || column2) AS expr1 FROM your_table;

Select empname || " " || empdpt as expr1
The sqllite concat is the same as PostGreSQL ( || ) and not MYSQL or MSSQL 'CONCAT'

for those who are trying to use the (working) solution of #merkuru
SELECT (column1 || " " || column2) AS expr1 FROM your_table;
in eclipse or another editor:
you have to cancel the " with \
something like:
SELECT (column1 || \" \" || column2) AS expr1 FROM your_table;
that's works perfect

This worked for me in the where clause:
SELECT * FROM table_name WHERE(first_name || last_name) LIKE comparison_string;

For anyone like me. I had to add if statements to my query, do to some values equalling NULL as follows:
SELECT
(CASE WHEN table.column1 IS NULL THEN "" ELSE table.column1 END) || " " || (CASE WHEN table.column2 IS NULL THEN "" ELSE table.column2 END) as ColName
from table
note: I might mention my case uses SQLite with Python but should be the same for all SQLite cases

Related

Rename column if it has single quote

I need to rename column in my oracle database using pl/sql. For example you have table like this.
CREATE TABLE TEST(
id int,
"'test1" varchar(80),
"test2" varchar(80)
);
and you need to remove all quotes from it. I wrote anonymous block and there is the problem:
...
FOR column_rec IN (SELECT column_name FROM USER_TAB_COLUMNS WHERE TABLE_NAME=table_rec.TABLE_NAME) LOOP
new_column_name := column_rec.COLUMN_NAME;
new_column_name := REPLACE(new_column_name, chr(34), '');
new_column_name := REPLACE(new_column_name, chr(39), '');
...
EXECUTE IMMEDIATE
'ALTER TABLE '
|| table_rec.TABLE_NAME
|| ' RENAME COLUMN '
|| column_rec.COLUMN_NAME
|| ' TO '
|| new_column_name;
...
But if column_rec.COLUMN_NAME has just a single quote in it, this script will fail with exception ORA-01756, which means there is no closing quote. How can I avoid this exception?
Just use double quotes to wrap the columns' names even in your script; for example, this works:
alter table test rename column "'test1" to test1
Your script could be edited as:
EXECUTE IMMEDIATE
'ALTER TABLE '
|| table_rec.TABLE_NAME
|| ' RENAME COLUMN "' /* open the quote */
|| column_rec.COLUMN_NAME
|| '" TO ' /* and close */
|| new_column_name;
Also, notice that in this way all the renamed columns will be upper case; if this is what you need, well done, otherwise you have to wrap with double quotes even the new names:
...
|| '" TO "' /* and close, and reopen */
|| new_column_name || '"'; /* and close again */

create or rename dynamic table teradata

I want to create or rename table with current_date.
For example:
1) create table TABLENAME || CURRENT_DATE
2) rename TABLE_NAME TO TABLE_NAME_||CURRENT_DATE.
How can I do it? Could you give an example?
This will append the current date in YYYYMMDD format to the table name. If YYYYMMDD is already appended to the name it's replaced with the new date.
REPLACE PROCEDURE rename_table_yyyymmdd
(
IN db_name VARCHAR(128) CHARACTER SET Unicode,
IN tbl_name VARCHAR(128) CHARACTER SET Unicode, -- defaults to current database
OUT msg VARCHAR(600) CHARACTER SET Unicode
) SQL SECURITY INVOKER
BEGIN
DECLARE old_name VARCHAR(261) CHARACTER SET Unicode;
DECLARE new_name VARCHAR(261) CHARACTER SET Unicode;
DECLARE sql_stmt VARCHAR(600) CHARACTER SET Unicode;
SET old_name = '"' || Coalesce(db_name,DATABASE) || '"."'
|| Coalesce(tbl_name, '') || '"';
SET new_name = '"' || Coalesce(db_name,DATABASE) || '"."'
-- remove an existing "_YYYYMMDD" at the end of the table name
|| Coalesce(RegExp_Replace(tbl_name, '_[0-9]{8}$'),'')
|| '_' || To_Char(Current_Date, 'yyyymmdd') || '"';
SET sql_stmt = 'RENAME TABLE ' || old_name || ' AS ' || new_name || ';';
EXECUTE IMMEDIATE sql_stmt;
SET msg = 'Table ' || old_name || ' renamed to ' || new_name;
END;
CALL rename_table_yyyymmdd('myDB', 'tablename', msg);
CALL rename_table_yyyymmdd(null, 'tablename', msg);
No error handling, simply fails on errors, e.g. when you run it twice a day or the table doesn't exists or the user has no Drop Table right, etc.
Step 1:
create table dat
(
saledate CURRENT_DATE
)
Step 2:
CREATE TABLE database.new_table AS
database.dat WITH DATA;
You can use sysdate or getdate to get current date.

How to delete duplicate rows from all of my oracle database tables, using PL/SQL?

A generic way to remove perfectly duplicated rows from all tables owned by a certain user.
Here's the stored procedure for it. Don't forget to replace 'YOUR_USERNAME_HERE' with your username.
CREATE OR REPLACE PROCEDURE DELETE_DUPLICATES_FROM_DB
IS
TABLE_COLUMNS VARCHAR2(10000);
DELETE_STATEMENT VARCHAR2(10000);
CURSOR ALL_MY_TABLES IS SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER = 'YOUR_USERNAME_HERE';
BEGIN
FOR MY_TABLE IN ALL_MY_TABLES
LOOP
SELECT LISTAGG(COLUMN_NAME, ',') WITHIN GROUP (ORDER BY 1) INTO TABLE_COLUMNS FROM USER_TAB_COLUMNS WHERE TABLE_NAME = MY_TABLE.TABLE_NAME;
DELETE_STATEMENT := 'DELETE FROM ' || MY_TABLE.TABLE_NAME || ' WHERE ROWID NOT IN (SELECT MIN(ROWID) FROM ' || MY_TABLE.TABLE_NAME || ' GROUP BY ' || TABLE_COLUMNS || ');';
EXECUTE IMMEDIATE DELETE_STATEMENT;
DBMS_OUTPUT.PUT_LINE(DELETE_STATEMENT);--print the statements we run
END LOOP;
END;

How do I add opening and closing brackets to the beginning and end of the data string in Sqlite

I have a column of text in sqlite that I have to select and add brackets to the beginning and end of each string of data. EG. column has "hello" I need to select it, adding brackets so it looks like "(hello)"
I have done this before in Sqlite, but can't remember how I did it. Any ideas would be really appreciated. I am sure it will be pretty simple.
Use || to concatenate strings:
SELECT '(' || 'hello' || ')';
SELECT '(' || column_name || ')' FROM table_name WHERE condition;

How to insert into multiple tables from one table through procedures?

There are 4 tables
Table1(Column11, Column12, Column13)
Table2(Column21, Column22)
Table3(Column31, Column32,column33)
Table4(Column21, Column22)
And the following Mapping Table:
Table5(Sourcetable,Source column ,Destination table ,Destination column)
How to insert data from Table1 as source table and destination table as Table2,table3,table,4, Through Procedures?
I am using oracle 11g. Please help to achieve this!
If I understand your question, this is what you are looking for:
CREATE OR REPLACE PROCEDURE cpy_table
AS
CURSOR targ_dest_relation IS
SELECT sourcetable, sourcecolumn, destinationtable, destinationcolumn FROM table5;
BEGIN
FOR rec IN targ_dest_relation loop
execute immediate 'insert into ' || rec.destinationtable || '(' || rec.destinationcolumn || ') select ' || rec.sourcecolumn || ' from ' || rec.sourcetable;
END loop;
END;
/
SQL Fiddle

Resources