I am looking to pull through everything after an : and before a ;.
For example my current code is:
select Left('{Script.Testdata}', CHARINDEX('; ',('{Script.Testdata}'))-1)
This currently pulls " Leon: My Job" through data from: Leon: My Job; New job
I am looking for this to pull through only My Job instead of Leon: My Job. So everything from the : and between the ; but everything between ; and ; not to show.
Here is one method:
select left(stuff(col, 1, charindex(':', col), ''),
charindex(';', stuff(col, 1, charindex(':', col), '')) - 1
)
Of course, this gets more complicated if the string may not have : or ;.
declare #var varchar(30)
set #var=' Leon: My Job; New jobs;;;'
select Substring(substring (#var,charindex(':',#var)+1,Len(#var)),0,
CHARINDEX('; ',substring (#var,charindex(':',#var)+1,Len(#var))))
Related
In Azure SQL, I'm attempting to delete any text that is present between the < and > characters to my column in my table
Sample text:
The best part is that. < br >Note:< br >< u> reading
:< /u> < span style="font-family: calibri,sans-serif; font-size: 11pt;"> moral stories from an early age
< b>not only helps your child.< /b>< br>< u>in
learning important: < /u>< /span>< span style="font-family: calibri;
">life lessons but it also helps, in language development.< /span>< ./span>
Output:
The best part is that. reading: moral stories from an early age not only helps your child in learning important: life lessons but it also helps in language development.
I tried below query its working only for small comments text:
SELECT [Comments],REPLACE([Comments], SUBSTRING([Comments], CHARINDEX('<', [Comments]), CHARINDEX('>', [Comments]) - CHARINDEX('<', [Comments]) + 1),'') AS result
FROM table
I have taken input table named check_1 and sample data is inserted into that table.
This query removes only the first occurring pattern.
SELECT [Comments],REPLACE([Comments], SUBSTRING([Comments], CHARINDEX('<', [Comments]), CHARINDEX('>', [Comments]) - CHARINDEX('<', [Comments]) + 1),'') AS result
FROM check_1
In order to remove all string patterns beginning with '<' and ending with '>' in the text, a user defined function with a while loop is created.
CREATE FUNCTION [dbo].[udf_removetags] (#input_text VARCHAR(MAX)) RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE #pos_1 INT
DECLARE #pos_n INT
DECLARE #Length INT
SET #pos_1 = CHARINDEX('<',#input_text)
SET #pos_n = CHARINDEX('>',#input_text,CHARINDEX('<',#input_text))
SET #Length = (#pos_n - #pos_1) + 1
WHILE #pos_1 > 0 AND #pos_n > 0 AND #Length > 0
BEGIN
SET #input_text = replace(#input_text,substring(#input_text,#pos_1,#Length),'')
SET #pos_1 = CHARINDEX('<',#input_text)
SET #pos_n = CHARINDEX('>',#input_text,CHARINDEX('<',#input_text))
SET #Length = (#pos_n - #pos_1) + 1
END
RETURN #input_text
END
select [dbo].[udf_removetags](comments) as result from check_1
Output String:
The best part is that. Note: reading : moral stories from an early age not only helps your child.in learning important: life lessons but it also helps, in language development.
You can also use Stuff [Refer Microsoft document on STUFF] in place of replace+substring function.
Replace this SET #input_text = replace(#input_text,substring(#input_text,#pos_1,#Length),'')
line with the line
SET #input_text = STUFF(#input_text,#pos_1,#Length,'')
in the user defined function.
Result will be same.
According to https://learn.microsoft.com/../azure/../regexp_replace Azure supports REGEXP_REPLACE.
This means it should be possible to replace all '<...>' by '' via
select regexp_replace(comments, '<[^>]*>', '') from mytable;
I need to insert multiple rows in a database table from a single string.
Here is my string it will be comma-seperated values
Current string:
batch 1 45665 987655,1228857 76554738,12390 8885858,301297 38998798
What I want is that batch 1 should be ignored or removed and remaining part should be added into the SQL Server database as a separate row for after each comma like this
Table name dbo.MSISDNData
Data
------------------
45665 987655
1228857 76554738
12390 8885858
301297 38998798
and when I query the table it should return the results like this
Query :
Select data
from dbo.MSISDNData
Results
Data
---------------------
45665 987655
1228857 76554738
12390 8885858
301297 38998798
Try this:
DECLARE #Data NVARCHAR(MAX) = N'batch 1 45665 987655,1228857 76554738,12390 8885858,301297 38998798'
DECLARE #DataXML XML;
SET #Data = '<a>' + REPLACE(REPLACE(#Data, 'batch 1 ', ''), ',', '</a><a>') + '</a>';
SET #DataXML = #Data;
SELECT LTRIM(RTRIM(T.c.value('.', 'VARCHAR(MAX)'))) AS [Data]
FROM #DataXML.nodes('./a') T(c);
It demonstrates how to split the data. You may need to sanitize it, too - remove the batch 1, perform trimming, etc.
have a single column Varchar(2000).
Data looks like in a single column,
12:10:08: Dialing12:10:08: Connecting12:10:08: ABC: abc:9433769781$100.88.77.0:878712:10:08: ABCD: 000012:10:09: Agent Initializing12:10:25: On Call12:10:25: Assigned to operator12:10:25: Waiting for Supervisor12:10:30: Waiting for Manager12:11:30: Call Ended12:11:30: Call Not connected..
I want to parse it like,
12:10:08: Dialing
12:10:08: Connecting
12:10:08: ABC: abc:9433769782$100.88.77.0:8787
12:10:08: ABCD: 0000
12:10:25: Agent Initializing
12:10:18: On Call
12:10:25: Assigned to operator
12:10:30: Waiting for Supervisor
12:10:30: Waiting for Manager
12:11:30: Call Ended
12:11:30: Call Not connected
Any help. Searched the complete forum, but I am really unsure about this, particularly an absence of a specific identifier. Appreciate your help.
P/S- This is just an example of a single time,time is not constant.
Yuck. But, you can do this with a recursive CTE. Here is how:
with t as (
select '12:10:08: Dialing12:10:08: Connecting12:10:08: ABC: abc:9433769781$100.88.77.0:878712:10:08: ABCD: 000012:10:09: Agent Initializing12:10:25: On Call12:10:25: Assigned to operator12:10:25: Waiting for Supervisor12:10:30: Waiting for Manager12:11:30: Call Ended12:11:30: Call Not connected.. ' as col
),
cte as (
select left(t.col, 9 + patindex('%[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %', substring(t.col, 11, 1000))) as val,
substring(t.col, 10 + patindex('%[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %', substring(t.col, 11, 1000)), 1000) as rest
from t
where t.col like '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %'
union all
select (case when rest like '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %'
then left(rest, 9 + patindex('%[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %', substring(rest, 11, 1000)))
else rest
end) as val,
substring(rest, 10 + patindex('%[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %', substring(rest, 11, 1000)), 1000) as rest
from cte
where rest like '[0-9][0-9]:[0-9][0-9]:[0-9][0-9]: %'
)
select val
from cte;
The SQL Fiddle is here.
Alternative;
DECLARE #string VARCHAR(1024) = '12:10:08: Dialing12:10:08: Connecting12:10:08: ABC: abc:9433769781$100.88.77.0:878712:10:08: ABCD: 000012:10:09: Agent Initializing12:10:25: On Call12:10:25: Assigned to operator12:10:25: Waiting for Supervisor12:10:30: Waiting for Manager12:11:30: Call Ended12:11:30: Call Not connected'
WITH T(last, pos) AS(
SELECT 0, 1
UNION ALL
SELECT pos, pos + PATINDEX('%[0-9][0-9]:[0-9][0-9]:[0-9][0-9]%', SUBSTRING(#string, pos + 1, LEN(#string)))
FROM T
WHERE pos != last
)
SELECT SUBSTRING(#string, last, CASE WHEN pos = last THEN len(#string) ELSE pos - last END)
FROM T
WHERE LAST > 0
For
(No column name)
12:10:08: Dialing
12:10:08: Connecting
12:10:08: ABC: abc:9433769781$100.88.77.0:8787
12:10:08: ABCD: 0000
12:10:09: Agent Initializing
12:10:25: On Call
12:10:25: Assigned to operator
12:10:25: Waiting for Supervisor
12:10:30: Waiting for Manager
12:11:30: Call Ended
12:11:30: Call Not connected
I know you are asking for an SQL solution but I am not sure that is possible without a while loop and extensive string manipulation which is quite inefficient.
If you are happy to bring the original varchar down to the BLL level, you can do it here using a regular expression. As I assume you want to do this in order to output to screen or log file then this should be possible.
For example:
Replace
/([0-9]{2}:[0-9]{2}:[0-9]{2}: ).*/gU
with
\n/1
Example:
http://regex101.com/r/eM4vD5/1
I want to convert the order of the values with column name PTNT_VST_CSNO from the following :
VMIP1
VMIP10
VMIP11
VMIP2
VMIP20
VMIP21
VMIP3
VMIP31
VMIP32
VMIP5
VMIP6
VMIP7
VMIP8
VMIP9
VMOP10
VMOP11
VMOP12
VMOP3
VMOP30
VMOP31
VMOP32
VMOP4
VMOP40
VMOP41
VMOP42
VMOP43
VMOP7
VMOP70
VMOP71
VMOP8
VMOP9
to:
VMIP1
VMIP2
VMIP3
VMIP5
VMIP6
VMIP7
VMIP8
VMIP9
VMIP10
VMIP11
VMIP20
VMIP21
VMIP31
VMIP32
VMOP3
VMOP4
VMOP7
VMOP8
VMOP9
VMOP10
VMOP11
VMOP12
VMOP30
VMOP31
VMOP32
VMOP40
VMOP41
VMOP42
VMOP43
VMOP70
VMOP71
I want to sort the numeric part of 'vmip' first then that of 'vmop'.. I tried a lot but failed every time. kindly help me guys to sort out the sorting problem... thank you in advance
Not the fastest thing in the world, but it should get the job done:
ORDER BY CASE WHEN PTNT_VST_CSNO LIKE 'vmi%' THEN 0 ELSE 1 END
,CAST(replace(replace(PTNT_VST_CSNO, 'vmip', ''), 'vmop', '') as int)
After great trial, I succeeded to solve it with the following way..
SELECT ptnt_vst_csno
FROM table_name
ORDER BY Substring(ptnt_vst_csno, 1, Charindex('P', ptnt_vst_csno)),
CONVERT(INT, Substring(Substring(ptnt_vst_csno,
Charindex('P', ptnt_vst_csno),
Len(
ptnt_vst_csno)), 2, Len(
ptnt_vst_csno)))
the easiest way to accomplish this would be to change your numering to 3 digits.
i.e. 1 would become 001, 2 would become 002, 10 would become 010, and so on...
this would then allow you to order the data correctly.
you might want to do something like this:
SELECT
PTNT_VST_CSNO,
'VMIP' + CASE LEN(REPLACE(PTNT_VST_CSNO, 'VMIP', ''))
WHEN 1 THEN '00' + REPLACE(PTNT_VST_CSNO, 'VMIP', '')
WHEN 2 THEN '0' + REPLACE(PTNT_VST_CSNO, 'VMIP', '')
ELSE REPLACE(PTNT_VST_CSNO, 'VMIP', '')
END
FROM
TableName
WHERE
LEFT(PTNT_VST_CSNO, 4) = 'VMIP'
ORDER BY
2
I have a question regarding how to extract data from string (variable length) and include it in the select queries.
For example, I have value in Portfolio_full_name as TS.PDO.CTS
(Portfolio_full_name = TS.PDO.CTS)
I would like to retrieve each word before the . and put it into another fields.
Portfolio_name = TS
Portfolio_category = PDO
Portfolio_subcategory = CTS
I am looking for to include this in the select statement before where condition (create CASE statement maybe?) Could you please let me know how could I do this?
In SQL Server (assuming that the format is fixed to NAME.CATEGORY.SUBCATEGORY and you've got the Portfolio_full_name column in some table, called atable here, and are updating the columns Portfolio_name, Portfolio_category and Portfolio_subcategory in the same table):
UPDATE atable
SET
Portfolio_name = SUBSTRING(s.Portfolio_full_name, 1, DotPos1 - 1),
Portfolio_category = SUBSTRING(s.Portfolio_full_name, DotPos1 + 1, DotPos2 - DotPos1 - 1),
Portfolio_subcategory = SUBSTRING(s.Portfolio_full_name, DotPos2 + 1, FullLen - DotPos2)
FROM (
SELECT
Portfolio_full_name,
DotPos1 = CHARINDEX('.', Portfolio_full_name),
DotPos2 = CHARINDEX('.', Portfolio_full_name, CHARINDEX('.', Portfolio_full_name) + 1),
FullLen = LEN(Portfolio_full_name)
FROM (
SELECT Portfolio_full_name FROM atable
) s
) s
WHERE atable.Portfolio_full_name = s.Portfolio_full_name