Gettting an SQL snippet from a query

Given:

select cde from mytable;

A1%
BV7%
CV5%

What SQL will give me the following as a result (SQL snippet):

where cde like 'A1%' or cde like 'BV7%' or cde like 'CV5%'

Thanks.
0
Utf
4/23/2010 11:21:01 AM
sqlserver.programming 1873 articles. 0 followers. Follow

3 Replies
621 Views

Similar Articles

[PageSpeed] 25

AA2e72E (AA2e72E@discussions.microsoft.com) writes:
> Given:
> 
> select cde from mytable;
> 
> A1%
> BV7%
> CV5%
> 
> What SQL will give me the following as a result (SQL snippet):
> 
> where cde like 'A1%' or cde like 'BV7%' or cde like 'CV5%'
 
It is far from clear what you are asking for, but if I make a blind guess
that your table contains search patters that you want to build a query 
string from, this could be a solution:

   SELECT 'WHERE ' + convert(nvarchar(MAX), 
          (SELECT 'cde LIKE ' + cde AS [text()]
           FROM   mytable
           FOR XML PATH(''), TYPE)

The solution requires SQL 2005 or later. 



-- 
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
0
Erland
4/23/2010 12:02:29 PM
Here is another solution. You should be able to run the result of this
batch as SQL query just by copying and pasting.

  set nocount on
  create table #t(sp varchar(20))
  insert into #t values ('A1%')
  insert into #t values ('BV7%')
  insert into #t values ('CV5%')
  
  print 'SELECT * FROM my_table WHERE'
  select 'cde LIKE '''+sp+''' or ' from #t
  print '1=0'
  
  drop table #t
  set nocount off

-- 
Gert-Jan


AA2e72E wrote:
> 
> Given:
> 
> select cde from mytable;
> 
> A1%
> BV7%
> CV5%
> 
> What SQL will give me the following as a result (SQL snippet):
> 
> where cde like 'A1%' or cde like 'BV7%' or cde like 'CV5%'
> 
> Thanks.
0
Gert
4/23/2010 12:47:28 PM
CREATE TABLE Patterns
(pattern VARCHAR (15) NOT NULL PRIMARY KEY);
INSERT INTO Patterns
VALUES ('A1%'), ('BV7%'), ('CV5%');

SELECT * -- never use * in production code
  FROM MyTable AS T, Patterns AS P
 WHERE T.cde LIKE P.pattern;

0
CELKO
4/23/2010 9:35:21 PM
Reply:

Similar Artilces:

SQL help #2
I want to make sub description 3 say NOT SALE where it Does not contain S My SQL knowledge is basic. I can do: UPDATE item set subdescription3 = 'NOT SALE' where subdescription3 = '' Which will make it correct for blank items, but I don't know how to do any kind of Not command? Please help. Chris See if this gets you the rows you are looking for: Select itemlookupcode, description, subdescription3 from item where subdescription3 NOT LIKE '%S%' % is the wild card for SQL - kind of like * in most other contexts. Glenn Adams Tiber Creek Consulting http:/...

Access to SQL
I have a query in access 2003 containing new patient records that needs to be inserted into a SQL server 2005 table. The code below is run from the Access database. I do NOT want to use linked tables or pass thriugh queries. ImportData contains the connect string. The Access front end will reside on the local computer NOT hosting the SQL server. The qryUniqueNewPatients query is on the access database side. The FROM clause is looking for something from the SQL server side. I need it to look at the qryUniqueNewPatients query on the Access side. This has me stuck. Any help is a...

Query to extract
I am trying to find a solution to the following. I have a Table with thousands of records that look like the following: "CN=ExchangeService I want my QUERY to extract everything to the RIGHT of the "=" sign In the example above.....I want to endup with ExchangeService Can anyone help me solve this?? Thanks Warren Phillips On Sun, 17 Feb 2008 14:42:00 -0800, WDP wrote: > I am trying to find a solution to the following. I have a Table with > thousands of records that look like the following: > > "CN=ExchangeService > > I want my QUERY to extra...

activity list freezes
In the past few days our users have been reporting that their activity list does not load. Everything else seems to be fine, and when I look at the SQL Server activity monitor, I see a suspended process with open transactions that involve an update or insert on the activity table. If I manually kill this process, the activity list loads fine. I ran the SQL profiler and was able to link the problem to one specific user who was trying to assign an activity to another user. She would get an error, and then everyone's activity lists would hang. She said she's not doing anything dif...

Gettting an SQL snippet from a query
Given: select cde from mytable; A1% BV7% CV5% What SQL will give me the following as a result (SQL snippet): where cde like 'A1%' or cde like 'BV7%' or cde like 'CV5%' Thanks. AA2e72E (AA2e72E@discussions.microsoft.com) writes: > Given: > > select cde from mytable; > > A1% > BV7% > CV5% > > What SQL will give me the following as a result (SQL snippet): > > where cde like 'A1%' or cde like 'BV7%' or cde like 'CV5%' It is far from clear what you are asking for, but if I make a b...

Web Queries #4
This is a multi-part message in MIME format. ------=_NextPart_000_0013_01C5B92B.FC805BD0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable I use Excel web query in spread sheet to go to yahoo site for stock = prices. Recently this has stopped working with an error message saying = data is not available. Does anyone know if something has changed? How = can the problem be corrected? ------=_NextPart_000_0013_01C5B92B.FC805BD0 Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable <!DOCTYPE...

Outlook Attachment Query
Does anyone know if it's possible to strip attachments from items in the 'Inbox' [or 'Sent Items', for that matter] - so that I can keep the messages, without the bulk of attachments? Any help much appreciated! Yes, this is possible but requires a third party tool. Check out the link below from Slipstick for some options. http://www.slipstick.com/addins/housekeeping.htm Regards, -- Francine Otterson President, San Diego Outlook User Group -- Francine Otterson President, San Diego Outlook User Group <someone@somedomain.com> wrote in message news:cto5fm$9i2...

Excel 2003 This Workbook contains queries to external data
Excel 2003 I get the following message box when opening an excel spreadsheet and displaying an extnernal microsoft query. This Workbook contains queries to external data that refresh automatically. Allows you to click either Enable automatic refresh or Disable automatic refresh. I know this is a new security enhancement and is turned on by default. How do you change the default to off so this does not appear every time ...

Query for next two dates
I need help with a select query that will select the next two (2) [duedate] in table and return those records where [duedate] >= today The SQL statement would look something like SELECT TOP 2 DueDate FROM YourTable WHERE DueDate >=Date() ORDER BY DueDate If you can only use the query grid Field: DueDate Sort: Ascending Criteria: DueDate >= Date() Then set the query's Top Values property to 2 If you table contains multiples of the same date then you will need to build a distinct query first and then use that in place of the table. You might be able to do that in a singl...

Access frontend with SQL Server backend: my solution for ODBC Call error
Hi This thread follows threads I started in forums microsoft.public.access.forms, and microsoft.sqlserver.programming: 1. 'Overriding constraint violation message' at 21.04.2010 12:00; 2. 'Overriding constraint restriction message, when deleting record(s) from form' at 29.04.2010 10:36. The problem (Occurs in Access 2000 mdb as stated by MS and in Access 2007 mdb as I myself discovered): When record operation is aborted from SQL Server, Access gets the full information about error, but this info gets unaccessible at moment when OnError event is triggered. The...

GP 9.0 on SQL 2008
MBS has said GP 10 is supported on SQL Server 2008. However there is no information on support for GP 9.0 on SQL 2008. Does anyone have any real-world experience running GP 9.0 on SQL 2008 in a production environment? Thanks, John Dynamics GP 9.0 is not supported on Microsoft SQL Server 2008. This is an excerpt from the System Requirements page. Database Requirements: MSDE 2000 SP 4 Microsoft SQL Server 2000 Enterprise Edition SP 4 Microsoft SQL Server 2000 Standard Edition SP 4 Microsoft SQL Server 2000 Workgroup Edition SP 4 Microsoft SQL Server 2000 (64-bit) SP 4 Microsoft SQL Serve...

Drop query
Hi, I wanted to know if its possible to drop quries how tables are dropped, something like Drop query "queryname" or any other way of doing it through VBA? i am creating a query with help of querydef and it needs to be deleted once i am done with my job. thanks for the help, navin Hi Navin You could try something like this - Of course change the names SomeEventHere - SomeEventOrButton - SomeName Private Sub SomeEventOrButton_Click() Dim SomeQueryNameHere As QueryDef For Each SomeName In CurrentDb.TableDefs If SomeName.Name = "SomeQueryNameHere" Then CurrentDb.Q...

access query trouble-HELP!
Hi all, I have been trying to figure this out for the last 14 hours and am making almost no progress. Any help is GREATLY appreciated. I have two tables: one is a large table of parliamentarians, dates of their speeches in parliament, their party and other such info (many thousands of rows). The other table is a list of 11 coalition governments, dates in power and the parties that supported them (as shown below). What I would like to do is run a query that adds a column to table 1, which stores information about whether a speaker's party was a member of the governing coalition on the da...

SQL
Hi, i am new in Sql express i am trying to create a database in sql but i dont now how can i install the database in other computer or a server programmatically where the other computer have the sql and if is a server what i nead to do . or only what i need is a valid conection ; i use the localhots to identify any clinte machine but i dont now what i need to writh for the servers my conection is look like this as a defualt from MFC hr = _db.OpenFromInitializationString(L"Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Data.mdf;Data Sour...

SQL Study Group
sql-study-group@googlegroups.com Please join for support our SQL Study Group ------=_NextPart_0001_3CECC08E Content-Type: text/plain Content-Transfer-Encoding: 7bit For those who want to join, the URL is http://groups.google.com/group/sql-study-group David Musgrave [MSFT] Escalation Engineer - Microsoft Dynamics GP Microsoft Dynamics Support - Asia Pacific Microsoft Dynamics (formerly Microsoft Business Solutions) http://www.microsoft.com/Dynamics mailto:David.Musgrave@online.microsoft.com http://blogs.msdn.com/DevelopingForDynamicsGP Any views contained within are my personal views a...

SQL 2008 and SRS
I am using SQL 2005 with CRM 4.0 on one box and have to move CRM and SQL to seperate boxes. The SQL box will have SQL 2008 Standard. The consultant who will be doing the SQL work reminded me to install SRS. Is SRS included with SQL 2008 Standard? Thanks, John SQL Server Reporting Services (SSRS) is part of 2008 Standard Edition. RLF "John" <bsmith@aol.com> wrote in message news:%23DpI1bfcKHA.808@TK2MSFTNGP02.phx.gbl... >I am using SQL 2005 with CRM 4.0 on one box and have to move CRM and SQL to >seperate boxes. The SQL box will have SQL 2...

IM with SQL 2000 to SQL 2005
Can IM be used to take data from an SQL 2000 database for use with GP 8.0 running on SQL 2005. Basilcy I need to extract order information from a website running on SQL 2000 and insert in in GP 8.0 Possible? Thanks Terry Yes, you can use SQL Server 2000 databases as a data source for Integration Manager with GP 8 running on SQL 2000/2005. -- Charles Allen, MVP "TerryB" wrote: > Can IM be used to take data from an SQL 2000 database for use with GP 8.0 > running on SQL 2005. > > Basilcy I need to extract order information from a website running on SQL > 2...

T-SQL to PL-SQL
Hello, can sombody help me to translate that script in oracle? DECLARE @ID INT DECLARE CURSEURS_JOBS CURSOR FOR SELECT REF_RE_JOB_POSTING_ID FROM DM_RE_JOB_POSTING OPEN CURSEURS_JOBS FETCH CURSEURS_JOBS INTO @ID WHILE @@FETCH_STATUS = 0 BEGIN update DM_RE_JOB_POSTING set publishedI = (select CASE WHEN rjpd.internal = 1 and (rjp.internal_external in ('B','I')) THEN ( CASE WHEN rjpd.start_date is null and rjpd.end_date is not null and rjpd.end_date >= getdate() THEN ( SELECT 'I' ) WHEN rjpd.start_date is...

Gettting divorced
After 15 yrs of marriage I'm getting divorced. My spouse is no longer willing to share her transaction data with me. I could start a new file, but I would prefer to keep my current file, which has 5 years of data in it. Since my spouse is not willing to give me the data to properly handle the divorce in MS Money, I have the following question What category should I use for the adjustments required to bring my spouse's 401K and stock options accounts to $0? I checked the FAQ, but there's no section on divorce and how to handle things in Money. Dick, any suggestions? As I go...

Query Outlook contacts folder from excel
Hi guys, I've been doing some research, and i'm not finding or more so understanding what i need to do. I'm not great with VBA, but am able to cut and paste quite well :) I've search the groups and found a couple threads but some links inside the threads don't work anymore, so i'm sort of stuck. I'm trying to query a folder within my outlook contacts, and populate a row for each contact with certain fields, not all. I'd prefer it to be a query rather than an Export.Import, for ease of use. Thanks for all for help! Shaun Anyone? On...

SQL 2005 Express.
Does anyone have detailed instructions on upgrading RMS to the 2005 Express version of the SQL Server? The 2.0 disk will upgrade you very easily. Even if you don't plan on upgrading to 2.0 you could still use it to upgrade to Express. It's a lot harder to download Express from MS and do it that way. I've done both and using the 2.0 disc is the way to go. Craig "Morris Paint" <morrispaint@ntelos.net> wrote in message news:%23rflVzbVHHA.4188@TK2MSFTNGP06.phx.gbl... > Does anyone have detailed instructions on upgrading RMS to the 2005 > Express version...

Calling Dex Procedures from SQL
Is there any way to call dex procs from SQL or VB code? Jed There is an undocumented and unsupported method of calling Dex code from VBA. However, I know of no method of calling Dexterity from SQL. Please email me if you want an example, just remove the online. from the address below. David Musgrave [MSFT] Senior Development Consultant MBS Services - Asia Pacific Microsoft Business Solutions http://www.microsoft.com/BusinessSolutions mailto:dmusgrav@online.microsoft.com Any views contained within are my personal views and not necessarily Microsoft Business Solutions policy. This p...

like query using field name
Hi All I have a column 'staffnumber' (int) and another column 'Resourceid' (nvarchar) in a table in sql server 2005 express I want to do a query to find rows where the staffnumber value is not in the Resourceid field e.g select mydate, customerid from appointments where Resourceid not like '%[staffnumber value]%' in other words the 'staffnumber' column value for each row is NOT in the 'Resourceid' column value Can this be done? Regards Steve Steve declare @par nvarchar(20) set @par='staffnumber' select my...

is SQL 2000 architecture different SQL Server 2005 / 2008 architecture?
Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I have been told is a good book. I am going backwards... reading about the sql server architecture in chapter 3 of the book. As I'm reading... I was wondering is it different SQL Server 2000 vs SQL Server 2005/2008? Also where can I find a book/or link that can give me more details about the internals on architecture? Thank you norm (normanchan@gmail.com) writes: > Hi I have a book "inside SQL Server 2000" by Kalen Delaney, which I > have been told is a good book. I am g...

Moving customizations from SQL 2k to SQL 2k5 CRM
This may be a non-issue but I wanted to check to see if anyone has done this and ran into issues. I have the XML customization files from our previous install of CRM 3.0 with SQL 2k database. I now want to import and publish these same customizations to CRM 3.0 on SQL 2k5. Has anyone done this or can point me to documentation that tells about the success or issues for doing this? Thanks. On 7 Feb., 06:35, Chris Treanor <ChrisTrea...@discussions.microsoft.com> wrote: > This may be a non-issue but I wanted to check to see if anyone has done this > and ran into issues. > &...