1

I want a to write an SQL query in Microsoft SQL Server in PyQt of the type:

list=['Engineer', 'Doctor', 'Lawyer']

select * from Occupations where OccupationName in (list)

I have read various posts online but they seem to be for SQL Lite or MySQL databases. How would I do this for Microsoft SQL?

Also, in MS SQL the string values need to be enclosed in double quotes instead of single quotes used in the list items. How would I do this?

Sarah
  • 161
  • 1
  • 3
  • 11

1 Answers1

0

this topic should help you! T-SQL split string

btw: You can write your params in CTE:

;with list as (
  select 'Engineer' as [occupation] union
  select 'Doctor' union
  select 'Lawyer'
)
select * from Occupations where OccupationName in (select occupationfrom from list)

or very simple like this:

 select * from Occupations where OccupationName in ('Engineer', 'Doctor', 'Lawyer')
Community
  • 1
  • 1
Dmitry Cat
  • 475
  • 3
  • 11
  • Hi @dmitrycat : That is just an example, my actual list is generated after the user chooses options from a check list. So, I need to run a query for whatever the user checks, so it has to be dynamic – Sarah Mar 07 '17 at 09:47
  • i understood a few later, http://stackoverflow.com/questions/10914576/t-sql-split-string i guess it's what you need – Dmitry Cat Mar 07 '17 at 09:48