Author |
Topic |
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-26 : 20:43:40
|
Hi,I have a requirements to capture those words with "-RTR".May i ask you assistance on how to make this in t-sql. thanks.Sample:create table #sample(item nvarchar(50))insert into #sample(item) values('HOT3053-RTR/2937-RTR Motorola DRDRZRMX Housing Frame')insert into #sample(item) values('HOT3053-RTR Motorola DRDRZRMX Housing Frame') insert into #sample(item) values('DAM1615-RTR LP - LCM Assembly') insert into #sample(item) values('Scraped Plastic kit HOT2937-RTR DROIDRAZOR2 Housing Frame')ResultItem----------------------Item2-------------------------------------------------------------HOT3053-RTR/2937-RTR-- Motorola DRDRZRMX Housing FrameHOT3053-RTR----------- Motorola DRDRZRMX Housing FrameDAM1615-RTR----------- LP - LCM AssemblyHOT2937-RTR----------- DROIDRAZOR2 Housing Frame |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-26 : 21:58:33
|
select * from #sample where item like '%-RTR%'Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-26 : 22:32:19
|
Hi Tkizer,thanks for your reply. sorry for not giving a clear requrements.I wanted to create a separate column for the string with "-RTR" complete name like (HOT3053-RTR)and the other column will be the string after the -RTR.Please see the sample result in the main thanks. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-26 : 22:38:49
|
I couldn't figure out the sample results, was't sure what those dashes were for. Now I'm thinking they are an attempt to space it out for us. Just add code tags around the formatted output so that it's readable.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-26 : 22:42:24
|
select LEFT(item, charindex('-RTR', item))+'-RTR' as item, substring(item, charindex('-RTR', item)+4, datalength(item)) as item2 from #sampleTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-26 : 23:22:36
|
Thank you very much for your prompt reply. Btw, can i have a condition if in the string does not have -RTR can i place an space or blank else display the valuein this codes.Select LEFT(item, charindex('-RTR', item))+'-RTR' as itemFROm #sample |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-26 : 23:32:24
|
SOLVED. Thank you very much Tkizer..here is the codes with condition.CASE WHEN sal.NAME like '%-RTR%' THEN LEFT(sal.NAME, charindex('-RTR', sal.NAME))+'RTR' ELSE '' END as SKU |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-27 : 01:31:48
|
Hi Tkizer,I just encounter this kind of string, how can i get this Item from the codes you have given, the result would be HHC1035-RTR and item2 is INCREDIBLE00193. thanks.CASE WHEN sal.NAME like '%-RTR%' THEN LEFT(sal.NAME, charindex('-RTR', sal.NAME))+'RTR' ELSE '' END as SKUItem---------------------------INCREDIBLE00193 HHC1035-RTR |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-27 : 01:38:50
|
You'll need to explain that logic.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2014-06-27 : 01:38:57
|
You'll need to explain that logic.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
Villanuev
Constraint Violating Yak Guru
478 Posts |
Posted - 2014-06-27 : 01:58:56
|
Running the codes that you have given, it gives me incorrect data specially for the records #5, it shows INCREDIBLE00193 HHC1035-RTRInstead of HHC1025-RTR. the result should be like this. thanks.Item1--------------Item2--------------------------------------------------------HOT3053--RTR-----Motorola DRDRZRMX Housing FrameDAM1615--RTR-----LP - LCM AssemblyHHC1035--RTR-----INCREDIBLE00193 --DDLcreate table #sample(item nvarchar(50))insert into #sample(item) values('HOT3053-RTR/2937-RTR Motorola DRDRZRMX Housing Frame')insert into #sample(item) values('HOT3053-RTR Motorola DRDRZRMX Housing Frame') insert into #sample(item) values('DAM1615-RTR LP - LCM Assembly') insert into #sample(item) values('Scraped Plastic kit HOT2937-RTR DROIDRAZOR2 Housing Frame')insert into #sample(item) values('INCREDIBLE00193 HHC1035-RTR') |
|
|
|