cmpmd2
cmpmd2

Reputation: 165

SQL Where Clause Single Quotes

I’m trying to get single quotes around a large amount of text. Example

SELECT
DiagnosisID
FROM DiagnosisTable
WHERE DiagnosisID IN (
E01.8, E02, E03.2, E03.3, E03.8, E03.9, E89.0, 
I21.01, I21.02, I21.09, I21.11, I21.19, I21.21, I21.29, I21.3, I21.4,     
I22.0, I22.1, I22.2, I22.8, I22.9 
G30.0, G30.1, G30.8, G30.9 

Theres about 2,300 different ID’s. Is there another way to place ‘ ‘ around the ID's? Is there another function to use?

Thanks

Upvotes: 1

Views: 1481

Answers (3)

jabs
jabs

Reputation: 1848

Here's what I'd do. It's not graceful or elegant, but it suits my purposes.

Create a table to hold the IDs.

create table MyID (ID  varchar(10))

Create a spreadsheet and add list into column B. In column A, type the following:

Insert into MyID values ('

In column C type:

)'

Copy all of the columns down to the end of the list. In column D, concatenate them A+B+C+D to get a string. Paste the string into your SQL window and insert the lines. Then, use it in a query:

`Select D.* 
FROM DiagnosisTable D 
join MyID M on D.DiagnosisID = M.ID`

Upvotes: 0

NS009
NS009

Reputation: 17

Assuming your data is in column A, add a formula to column B

= "'" & A1 & "'"

and copy the formula down. copy column B then paste value in the where clause of the SQL query .

Upvotes: 0

Zohar Peled
Zohar Peled

Reputation: 82504

Copy your quety to any text editor (yes, even notepad) that have a find & replace function. Replace , with ', ' and add the firat and last ' manually.

If you already started to add them manually, first replace ' with an empty string.

Upvotes: 3

Related Questions