Extract name from a string [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
Example:
SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L
I want to extract only RadioNode
SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W
I want to extract only GALRNC1.

You can do it using SUBSTRING and LEFT and charindex
select LEFT(sub1, CHARINDEX(',MeContext', sub1) - 1)
from (
select
SUBSTRING(column1, charindex('SubNetwork=', column1, 2) + LEN('SubNetwork='), 20) sub1
from mytable
) as s
charindex to get the position of your word in this case it is SubNetwork=.
SUBSTRING to Extract characters from a string.
LEFT to Extract characters from a string (starting from left).
Demo here

Please try the following solution.
It is using JSON and will work starting from SQL Server 2016 onwards.
SQL
-- DDL and sample data population, start
DECLARE #tbl TABLE (id INT IDENTITY PRIMARY KEY, tokens NVARCHAR(1024));
INSERT #tbl (tokens) VALUES
(N'SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L'),
(N'SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W');
-- DDL and sample data population, end
SELECT t.*
, SubNetwork = JSON_VALUE(j,'$.SubNetwork')
FROM #tbl AS t
CROSS APPLY (SELECT '{"' + REPLACE(REPLACE(STRING_ESCAPE('~'+tokens,'json')
,',','","')
,'=','":"') + '"}'
) AS t1(j);;
Output
id
tokens
SubNetwork
1
SubNetwork=ONRM_ROOT_MO,SubNetwork=RadioNode,MeContext=BAS917L
RadioNode
2
SubNetwork=ONRM_ROOT_MO,SubNetwork=GALRNC1,MeContext=BAT045W
GALRNC1

Related

How to use RAND() in SQL [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 days ago.
Improve this question
I want to randomly choose a row in a column and change the values. I’m not too sure where Rand function goes.
To be more specific, I have a table with names in it but I want to randomly select one of the names from the first name column and change it to a different name of my choosing using the UPDATE and RAND() function and it can't be NEWID(). What parameters do I need inside the parenthesis as well?
I am using SQL Server Express
I tried starting the code out with
UPDATE table name
SET column name1 = ‘new name’, column name2 = 'new name 2'
WHERE column name = (SELECT RAND()(column name-column name)+column name
from table name);
We can use NEWID() to fetch a random row, see the documentation, especially part D.
Here an example how to use it:
Create a table with some sample data:
CREATE TABLE yourtable (yourcolumn varchar(100));
INSERT INTO yourtable VALUES ('A'),('B'),('C'),('D'),('E');
Update one randow row of this table:
UPDATE yourtable SET yourcolumn = 'HelloWorld'
WHERE yourcolumn = (SELECT TOP 1 yourcolumn FROM yourtable ORDER BY NEWID());
If we want to set the value of that column in one random row to a random existing value of another row rather than to a hardcoded string, we could use NEWID() two times, something like this:
UPDATE yourtable SET yourcolumn =
(SELECT TOP 1 yourcolumn FROM yourtable ORDER BY NEWID())
WHERE yourcolumn = (SELECT TOP 1 yourcolumn FROM yourtable ORDER BY NEWID())
Try out here

SQL Server - check more than 1 value in where condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
SELECT *
FROM table1
WHERE (#Id IS NULL
OR (a.ID = #Id)
OR (SELECT * FROM Table2 WHERE TestID = #Id) // How to check multiple values here
The above query works fine when TestID has single record. But when more than 1 record present in TestID column, I am trying to implement and not got exact solution.
How to confirm #Id value present in TestId column?
Could you please assist me on this? Thank you
It looks like you need exists
or exists (
SELECT * FROM Table2 WHERE TestID = #Id
)
Use top 1 before * from this way you will always get a single row in case there are multiple rows for the same #ID.
SELECT top 1 * FROM Table2 WHERE TestID = #Id
Though I am not sure how this line works in where clause as it does not return any boolean

Append “tail” to multiple selects [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have multiple result rows that has following structure
select data0, data1, ..., data30 from t0
union all select data0, ..., data30 from t1
...
There are multiple result rows, many of which need
to be aligned to the length of 30. So me do following
union all select data0, data1, null, null, ..., null
Is there a convenient way to automate this kind of
task. What I want is to append tail nulls as needed.
What I do now
with nulls as (
select null as nul0, ..., null as nul30 from dual
)
And I am stuck at this point. How to append this nulls to
result rows? Number of empty columns are known.
Pls edit as appropriate, I type from mobile
How to append this nulls to result rows?
There is no built-in solution for generating a projection of an arbitrary number of columns.
This solution will take a fair amount of typing but you can semi-automate it by using a text editor which supports regex search'n'replace patterns.
with nulls as (
select cast(null as varchar2(10)) as nul0
, ...
, cast(null as varchar2(10)) as nul30
from dual
)
select t1.dat01
, t1.dat02
, nulls.nul03 as dat03
, nulls.nul04 as dat04
...
, nulls.nul30 as dat30
from t1
cross join nulls
union all
select t2.dat01
, t2.dat02
, t2.dat03
, nulls.nul04 as dat04
...
, nulls.nul30 as dat30
from t2
cross join nulls

Empty value in a IN clause with select query [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to use empty value in an IN clause. But following queries don't work identically.
SELECT * from tblTest WHERE colName IN ( '')
SELECT * from tblTest WHERE colName IN (SELECT '''''')
The first works fine but the second has no error but gives empty result.
It's because SELECT '''''' will return you '' and try to compare your values to it, which is not an empty string.
Anyway, it's not quite clear what you're trying to achieve.
That's pretty clear: you want the rows where the column contains an empty string. But if you compare with (select '''''') you dont select an empty string! You select a string that looks like this: ''
This works:
SELECT * from tblTest WHERE colName IN (SELECT '')

sql to filter decimal and non decimal integer in sql server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have following table
marks
45
45.5
56
56
56.6
I need to write a query to list the marks which are not in decimal value i.e
marks
45
56
56
Is there any sql query to filter decimal and non decimal integer in sql server
Perhaps:
SELECT * FROM dbo.Table1
WHERE CEILING(marks) = FLOOR(marks)
Demo
Comparing CEILING and FLOOR
DECLARE #t TABLE ( marks NUMERIC(10,2))
INSERT INTO #t VALUES
(45),(45.5),(56),(56),(56.6)
SELECT marks
FROM #t
WHERE marks = ROUND(marks , 0)
If you are using SQL Server 2012 you can use [TRY_PARSE][1]:
In the beneath example TRY_PARSE returns an INT when the value is an INT, else it returns NULL, so those rows are filtered out by the IS NOT NULL.
SELECT *
FROM table
WHERE TRY_PARSE(CAST(Marks AS varchar(20)) AS INT) IS NOT NULL
SELECT *
FROM t
WHERE value NOT LIKE '%.%'