April 21, 2010 at 10:00 am
Hi All,
I'm looking for the correct syntax to find my workorder number within my serial number which starts at the 7th character and is 5 characters long. I even tried "(_)" but that didn't work. Any help is appreciated.
select sn, unit
from table
where sn like '%#FORM.Workorder#%'
April 21, 2010 at 10:11 am
It would help if you would post the DDL (CREATE TABLE statement) for the table, sample data (series of INSERT INTO table statements) for the table, and expected results. Hard to help diagnosis a problem when we have nothing to work with ourselves.
April 21, 2010 at 10:29 am
Buddy, as Lynn said, it really is hard to guess and tell u a solution.. so post ur sample records, ur table structure and desired output..
But for starters, i will u this code.. if u need to get a subset of characters from a position , use SUBSTRING.
so your code now will look like the following:
select sn, unit
from table
where SUBSTRING(sn,7,5) like <your search condition>
Hope this gets u started :-)! but before that, how about you reading the first link on Lynn Pettis signature line to get ur "questioning" skills honed? 😎
Cheers!!
April 21, 2010 at 10:49 am
Coldcoffee,
Thanks. Not sure if posting data would apply in this situation. But, I posted data in the past and ended up with people responding with how valuable their time was because I posted the data incorrectly. SUBSTRING is what I was looking for.
Thanks again,
djkhalif
April 21, 2010 at 11:32 am
kabaari (4/21/2010)
Coldcoffee,Thanks. Not sure if posting data would apply in this situation. But, I posted data in the past and ended up with people responding with how valuable their time was because I posted the data incorrectly. SUBSTRING is what I was looking for.
Thanks again,
djkhalif
Please note that if you write the query the way Coldcoffee showed you it will not take advantage of any indexes that may exist on your table.
Also, take the time to read the first article I reference in my signature block. It shows you how to post data in a readily consummable format that makes it easy for us to load a table with sample data.
April 21, 2010 at 4:30 pm
kabaari (4/21/2010)
Coldcoffee,Thanks. Not sure if posting data would apply in this situation. But, I posted data in the past and ended up with people responding with how valuable their time was because I posted the data incorrectly. SUBSTRING is what I was looking for.
Thanks again,
djkhalif
Heh... it's your question and you're the one that needs an answer. Read and heed the link that Lynn referred you to and life will become a pleasure again. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
April 21, 2010 at 8:13 pm
I'll second (third?) the sentiment of Lynn and Jeff, from the perspective of a non-expert, if you post the code, you're taking advantage of their experience and brain-power, and they may come up with a solution that you never would have considered because you didn't know it existed.
Worth the effort.
---------------------------------------------------------
How best to post your question[/url]
How to post performance problems[/url]
Tally Table:What it is and how it replaces a loop[/url]
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."
April 21, 2010 at 8:24 pm
Lynn Pettis (4/21/2010)
Please note that if you write the query the way Coldcoffee showed you it will not take advantage of any indexes that may exist on your table.
Guess am under the hammer for providing cant-use-index code :crying:!! Substring does an index scan i guess (am not into indexes much :() so it is not am optimal code.. Lynn/Jeff, can u point me on how to scale Substring function , using this example??
April 21, 2010 at 9:37 pm
ColdCoffee (4/21/2010)
Lynn Pettis (4/21/2010)
Please note that if you write the query the way Coldcoffee showed you it will not take advantage of any indexes that may exist on your table.Guess am under the hammer for providing cant-use-index code :crying:!! Substring does an index scan i guess (am not into indexes much :() so it is not am optimal code.. Lynn/Jeff, can u point me on how to scale Substring function , using this example??
As stated, SubString won't use an index. What you need to do is use LIKE, with the underscore wildcard at the beginning.
This code works real nice:
-- See how you start off by actually creating a table and then
-- inserting the data into it? Your doing this makes it a lot easier
-- for all of us volunteers to help you. So, help us help you.
-- See http://www.sqlservercentral.com/articles/Best+Practices/61537/
-- for more details on how to do all of this.
IF object_id('tempdb..#Temp') IS NOT NULL DROP TABLE #Temp
CREATE TABLE #Temp (SerialNumber varchar(20) PRIMARY KEY CLUSTERED)
-- Create a virtual tally table.
-- See Jeff Modem's article The "Numbers" or "Tally" Table: What it is and how it replaces a loop.
-- at http://www.sqlservercentral.com/articles/T-SQL/62867/ for how a tally table can split strings apart.
;WITH
TENS (N) AS (SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL
SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0 UNION ALL SELECT 0),
THOUSANDS (N) AS (SELECT 1 FROM TENS t1 CROSS JOIN TENS t2 CROSS JOIN TENS t3),
MILLIONS (N) AS (SELECT 1 FROM THOUSANDS t1 CROSS JOIN THOUSANDS t2),
TALLY (N) AS (SELECT ROW_NUMBER() OVER (ORDER BY (SELECT 0)) FROM MILLIONS)
INSERT INTO #Temp
SELECT TOP (99999) CHAR(ABS(CHECKSUM(NEWID()))%26+65) + -- random characters
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
right('00000' + convert(varchar(5),N),5) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65) +
CHAR(ABS(CHECKSUM(NEWID()))%26+65)
FROM TALLY
declare @NumToCheck smallint
set @NumToCheck = 9458
select SerialNumber
from #Temp
where SerialNumber like '______' + right('00000' + convert(varchar(5), @NumToCheck), 5) + '%'
It uses an index seek, as shown by this execution plan:
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply