Selecting rows where cells contain multiple values - sql

Using Oracle SQL Developer 1.5.5.
I have a data table and I'm trying to select rows from the table (TABLENAME) where a column (COLUMN5) in the table contains multiple values. For example using the following command:
select * from TABLENAME where COLUMN5=''101','221','429''
I'm doing this because I want to select all rows in the table where the cell values in column 5 are '101','221',229'
An added complication is that in the table each of the values is within single quotation marks, separated by a comma. When run the above command I get an error message which says " SQL command not ended properly" I think this is something to do with the single quote marks and the multiple values because when i run the following command to select rows when column 5 is '443', I get a result:
select * from TABLENAME where COLUMN5='''443'''
Note that these values of '443' have single quotation marks around them in the table.
My question is what command will allow me to select rows with the multiple values in column 5? Help!
Let me know if you require any clarification

I think you can use FIND_IN_SET like below:
select * from accounts where FIND_IN_SET('101','101,102')
FIND_IN_SET ignore the single quotes

you just need to escape the quotes
create table so_col5 (a integer, col5 varchar2(25));
insert into so_col5 values (1, 'abcd');
insert into so_col5 values (2, 'abcd');
insert into so_col5 values (3, 'abcd');
Insert into SO_COL5 (A,COL5) values (4,'''101'',''221'',''229''');
commit;
select * from so_col5 where col5 = '''101'',''221'',''229''';
It took me a few tries, testing selecting the string from dual, but then I remembered I could just cheat - and export the result from the GRID as an INSERT Script and copy/paste the quoted string for that value and use that in my SELECT.
You're using v1.5, which is ANCIENT. Highly suggest you upgrade for an optimal experience.
You can also use the Q function (docs) which you can pass a string and Oracle handles the quote escaping for you. Here's a scenario you can play with on LiveSQL.

Related

Insert comma delimited ids into a table

I need to insert records into a SQL Server table. The client send ids I need to insert into a specific column in the table:
(2,4,123,1357,1234,5657,753);
Using this function I'm splitting the comma delimited string, but not sure how to insert it to the table along with the other columns
I need to create something that will generate inserts such as:
insert into table_name (id,column_2,column_3) values (2, column_s_some_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (4, column_s_some_other_value, column_3_some_value);
insert into table_name (id,column_2,column_3) values (123, column_s_some_value, column_3_some_value);
ETC...
How can I achieve that?
the split function is a Table-Valued Function, which means it can be treated as a table, and you can do an INSERT..SELECT
insert into table_name (id,column_2,column_3)
SELECT s.item, column_s_some_value, column_3_some_value
FROM Split(#input_string, ',') s
{JOINS if needed to get other column values}
If you do want to split strings, the most efficient way to get that done is through the use of the tally table as outlined here by Jeff Moden. I'd strongly suggest you use this method in just about any version of SQL Server.

SQL How to insert null value

I want to insert data into one table from another table. In some of the columns I don't have data, so I want to set column to null. I don't know how I should do this?
This is the SQL:
INSERT INTO _21Appoint(
PCUCODE,PID,SEQ,
DATE_SERV,APDATE,
APTYPE,APDIAG,D_UPDATE,CID
) SELECT (
NULL,NULL,NULL,
treatment_date,appointment_date,
typeap_id,appointment_id,NULL,patient_id
) FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
Your insert is essentially correct. Just don't put the column list in parentheses:
INSERT INTO _21Appoint
(PCUCODE,PID,SEQ,DATE_SERV,APDATE,APTYPE,APDIAG,D_UPDATE,CID)
SELECT NULL,NULL,NULL,treatment_date,appointment_date,typeap_id,appointment_id,NULL,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
In Postgres (unlike other DBMS) putting a column list in parentheses makes the result a single "record", rather then individual columns. And therefore the select only returns a single column, not multiples and thus it doesn't match the column list for the insert
another option is to simply leave out the columns completely:
INSERT INTO _21Appoint
(DATE_SERV,APDATE,APTYPE,APDIAG,CID)
SELECT treatment_date,appointment_date,typeap_id,appointment_id,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;

To insert single quote values into the table using "insert into"

I tried to insert some data from one table (tableA)
into another table (tableB) using bulk insert but cannot succeeded because the tableA data has got one value-> BERMUDA 23''-24''
when it tries to enter this value it raise error as
String or binary data would be truncated.
My query is
insert into tableA
select size from tableB
i try with replace the single quote with empty space but as its size we should enter the data with single quote only.
Please suggest the way out.
you can remove single quote with replace function
insert into tableA
select replace(size,'''','') from tableB

derby sql insert

I'm trying to insert values in my database
This is the statement I try to execute:
insert into leverancier (id,naam,straat,nr,postcode,plaats,telefoon)
values (1,"stef","bosstraat",88,9240,"Zele",null);
I get the following error:
ERROR 42X04: Column 'stef' is either not in any table in the FROM list or appears within a join specification and is outside the scope of the join specification or appears in a HAVING clause and is not in the GROUP BY list. If this is a CREATE or ALTER TABLE statement then 'stef' is not a column in the target table.
What is the problem?
To insert a string, like "stef", don't use double quotes but single quotes: 'stef'. Here's how the statement should be:
INSERT INTO leverancier
(id, naam, straat, nr, postcode, plaats, telefoon)
VALUES
(1,'stef', 'bosstraat', 88, 9240, 'Zele', NULL);
The error you get Column 'stef' is either not in any table ... is because double quotes are used for table and column names. So reading "stef", the parser assumes you are referring to a column named stef.

SQL: Insert multiple sets of values in one statement?

Is it possible to insert multiple sets of values to a SQLite table in one statement?
I was trying:
INSERT INTO the_table VALUES (1,2,'hi'),(2,0,'foo');
with the different ()s representing different insert sets, but I get an error.
Are there only three columns in your table? If not, you could try defining the column names you are setting like so:
INSERT INTO the_table
(column1 ,column2 ,column3)
VALUES (1 ,2 ,'hi' )
,(2 ,0 ,'foo' )
This convention was introduced in SQL Server 2008 known as the Table Value Constructor. See MSDN's INSERT page for a look at the overall syntax. Also, the INSERT statement can be easily formatted for better readability.
You can do
INSERT INTO the_table
SELECT 1,2,'hi'
UNION
SELECT 2,0,'foo';
I was found that syntax in MSDN but after trying I can't do that too, than I note that in the bottom of the page was written that there is an error in the page :) where is link http://msdn.microsoft.com/en-us/library/ms174335.aspx see the bottom How to insert multiple rows