-5

I have column which contain strings like:

1. "HDPE Unlaminated Fabric-60gsm 101 White"
2. "SUP 150 110 GSM white Cut piece"
3. "HDPE LF 140/120 GSM Mix color"
4. "self adhsv(50gsm) HDPE

Could you help me writing query to get the output as shown below:

1. "HDPE Unlaminated Fabric-60gsm 101 White" | 60gsm
2. "SUP 150 110 GSM white Cut piece"         | 110 GSM
3. "HDPE LF 140/120 GSM Mix color"           | 140/120 GSM
4. "self adhsv(50gsm) HDPE                   | 50gsm
aduguid
  • 3,099
  • 6
  • 18
  • 37
Jai
  • 1
  • 1
  • 1
    `select columnA, columnB from tableName` – TZHX Nov 28 '19 at 10:00
  • maybe with `top 8` in there... – TZHX Nov 28 '19 at 10:00
  • 1
    Please, read [how to ask a good question](https://stackoverflow.com/help/how-to-ask). – Zhorov Nov 28 '19 at 10:03
  • The input column is already there, I just want query for o/p column, how can we get only the needed string as shown in o/p column from the input. – Jai Nov 28 '19 at 10:05
  • @DaleK The sample data is the input column shown in image. – Jai Nov 28 '19 at 10:08
  • 1
    Ideally post your data as text not an image. Even better post is as DDL/DML statements that people can copy and paste to test. – Dale K Nov 28 '19 at 10:10
  • Do show us what you have tried, as we are not here to write your query for you. – Dale K Nov 28 '19 at 10:11
  • 2
    *"I'll take care of it in future"* don't do so in the future, do so now. Users with overwall negative question scores do get asking "bans" (meaning that you can only ask questions once a month, or even once ever 6 months), and this *demand* (it's not a question) already has 4 downvotes. Take the time to fix your **question**, so that you *can* ask questions in the future. – Thom A Nov 28 '19 at 10:12
  • Could anyone please help me to get the expected output, I tried many combinations of substring,left,right,charindex,patindex but could not get to it. – Jai Nov 28 '19 at 13:21
  • @Larnu As you suggested I had updated my question, Please help me to solve the issue. Thanks in anticipation. – Jai Nov 28 '19 at 14:00

1 Answers1

0

You can create a statement using T-SQL built-in functions that is returning your desire result but it:

  • will be complex
  • will become even more complex when your conditions are changed

So, it will be difficult for you to change it and support it. For me, such tasks can be handled more appropriately using regular expressions.

We have two options:

  • read the data and perform the transformation using regex in the client side or in your back end routines (JavaScript, C#, etc);
  • to create SQL CLR functions that allows to use regex function in the context of T-SQL statement

For the second option follow the example I have linked to create a RegexMatches function and you will be able to do the following (like in my environment):

DECLARE @DataSource TABLE
(
    [value] NVARCHAR(128)
);

INSERT INTO @DataSource ([value])
VALUES ('HDPE Unlaminated Fabric-60gsm 101 White')
      ,('SUP 150 110 GSM white Cut piece')
      ,('HDPE LF 140/120 GSM Mix color')
      ,('self adhsv(50gsm) HDPE');

SELECT *
FROM @DataSource 
CROSS APPLY dbo.fn_Utils_RegexMatches ([value], '(?i)\d+[\d/]+\s*gsm');

enter image description here

Few important notes:

  • you will need to put some effort to understand and implement SQL CLR functions
  • for the past years Microsoft have added a lot of built-in functions like string aggregate and string split without order, but as SQL Server 2019 there is no built-in support for regex
  • if you have no time to learn or you are not allowed to support/create SQL CLR functions it will be better to perform the operation outside the SQL Server

If you implement regex SQL CLR functions you can find solving particular issues very simple and clear.

gotqn
  • 42,737
  • 46
  • 157
  • 243
  • thanks, Could you help me how you created the RegexMatches ? – Jai Nov 29 '19 at 11:11
  • Check the link I have added - you will see how to create SQL CLR function there. And here, you can find the code of the function - https://learn.microsoft.com/en-us/previous-versions/sql/sql-server-2012/ff878119(v=sql.110)?redirectedfrom=MSDN – gotqn Nov 29 '19 at 11:21