SQL splitting a string - sql-server-2017

From the dept column, I want to create a new column that just returns what is on the right side of the hyphen. It should look like the NewDept column. Thank you

Related

How do you query a table filtering on a substring of one of the columns?

I have a table I wish to query. It has a string variable called comment which contains an ID along with other things. (i.e. "123456;varA;varB")
rowNo
comment
1
"123456;varA;varB"
2
"987654;varA;varB"
I want to filter based on the first substring in the comment variable.
That is, I want to filter the table on rows where the first substring of comment is "123456" (which in the example would return the first row)
How do I do this?
I was thinking something along the lines of the code below, using the "string_split" function, but it doesn't work.
SELECT *,
FROM table
WHERE (SELECT value FROM STRING_SPLIT(comment,';',1)="123456")
Does anyone have any ideas?
Note, I am querying in SQL in SAS, and this is on a large dataset, so I don't want to create a new table with a new column to then query on instead. Ideally I'd want to query on the existing table directly.
You can use the SCAN() function to parse a string.
WHERE '123456'=scan(comment,1,';')

Teradata Character Column with non alphabet values

I have a name column in Teradata that has customer full name all in one column. There are some names with -,_,.,/,#,! in between the name characters. I want to be able to pull records where there are names with these conditions. Is there a better option to pull records with the scenario below?
Currently, I am writing query like this
SELECT NAME FROM TABLESOURCE WHERE NAME LIKE ANY('%-%','%.%','%#%','%~%','%!%')
Thanks in advance.
I haven't tested this but I think you could test for equality when those characters are removed from the name using otranslate
select name
from tablesource
where name <> otranslate(name,'-.#~!','')

SQL - just view the description for explanation

I would like to ask if it is possible to do this:
For example the search string is '009' -> (consider the digits as string)
is it possible to have a query that will return any occurrences of this on the database not considering the order.
for this example it will return
'009'
'090'
'900'
given these exists on the database. thanks!!!!
Use the Like operator.
For Example :-
SELECT Marks FROM Report WHERE Marks LIKE '%009%' OR '%090%' OR '%900%'
Split the string into individual characters, select all rows containing the first character and put them in a temporary table, then select all rows from the temporary table that contain the second character and put these in a temporary table, then select all rows from that temporary table that contain the third character.
Of course, there are probably many ways to optimize this, but I see no reason why it would not be possible to make a query like that work.
It can not be achieved in a straight forward way as there is no sort() function for a particular value like there is lower(), upper() functions.
But there is some workarounds like -
Suppose you are running query for COL A, maintain another column SORTED_A where from application level you keep the sorted value of COL A
Then when you execute query - sort the searchToken and run select query with matching sorted searchToken with the SORTED_A column

SQL Query: Modify records based on a secondary table

I have two tables in a PostgreSQL database.
The first table contains an ID and a text field with up to 200 characters and the second table contains a data definition table which has a column that contains smileys or acronyms and a second column which converts them to plain readable English.
The number of records in table 1 is about 1200 and the number in table two is about 300.
I wish to write a SQL statement which will convert any text speak in column 1 in table one into normal readable language based on the definitions in Table 2.
So for example if the value in table 1 reads as: Finally Finished :)
The transformed SQL would be something like: Finally Finished Smiles or smiling,
where the definition is pulled from the second table.
Note the smiley could be anywhere in the text in column one and could one of three hundred characters.
Does anyone know if this is possible?
Yes. Do you want to do it entirely in SQL, or are you writing a brief bit of code to do this? I'm not entirely sure of how to do it all in SQL but I would consider something like what is below:
SELECT row.textToTranslate FROM Table_1
oldText = row.textToTranslate
Split row.textToTranslate by some delimeter
For each word in row.textToTranslate:
queryResult = SELECT FROM Table_2 WHERE pretranslate=word
if(queryResult!=Null)
modifiedText = textToTranslate.replace(word, queryResult)
UPDATE Table_1 SET translatedText=modifiedText WHERE textToTranslate=oldText

SQL query to extract text from a column and store it to a different column in the same record

I need some help with a SQL query...
I have a SQL table that holds in a column details of a form that has been submitted. I need to get a part of the text that is stored in that column and put it into a different column on the same row. The bit of text that I need to copy is always in the same position in the column.
Any help would be appreciated guys... my mind has gone blank :">
UPDATE mytable
SET other_column = SUBSTRING(column, begin_position, length)
You may just want to use a computed column. This way if the source string changes, your computed column is still correct. If you need to seek to this substring then you might want a persisted computed column if your db supports it.
UPDATE table
SET Column2 = SUBSTRING(Column1, startPos, length)
What if the value you wanted to copy was in a different position in each record, but always followed the same text?