Variable declaration to store multiple values like full column value - sql

I have an requirement where I have to store the column value into an declared variable is it possible anyways. I understand that an variable can store only single value and not multiple values. But in my case I need it badly.
What I have here is to set the column if date value which is in INT has to be converted into Date column and save it in that variable to compare it.
I have some column with values like below
"A(B_CD_EE_FF_DFE)_ERT"
So i have to remove those two bracktes and process.
Set #Variable= (SELECT replace (replace ( replace (SUBSTRING(TXMLFileName, CHARINDEX('(', TXMLFileName)
, CHARINDEX(')',TXMLFileName) - CHARINDEX(')', reverse (TXMLFileName)) ) , ')', '') ,'(' ,'') ,'.xml' , '')
from
tblXML )
Gives me error and below gives answer as I select only top 1.
Set #Variable= (SELECT top 1 replace (replace ( replace (SUBSTRING(TXMLFileName, CHARINDEX('(', TXMLFileName)
, CHARINDEX(')',TXMLFileName) - CHARINDEX(')', reverse (TXMLFileName)) ) , ')', '') ,'(' ,'') ,'.xml' , '')
from
tblXML )
SO is there any known solution... ????? Thanks in advance

A variable needn't just store a single value. You can create e.g. custom table types
CREATE TYPE MyCustomType AS TABLE (
MyCustomTypeId INT,
MyCustomValueColumn VARCHAR(100)
)
Or just declare a comparable table variable inline where needed.

declare #tblXML table (TXMLFileName varchar(50))
insert into #tblXML values
('A(B_CD_EE_FF_DFE)_ERT'),
('(EE_FF_DFE)_ERT'),
('A(B_CD_EE)')
declare #tmp varchar(max)
SET #tmp = ''
SELECT #tmp=#tmp+ replace (replace ( replace (SUBSTRING(TXMLFileName, CHARINDEX('(', TXMLFileName)
, CHARINDEX(')',TXMLFileName) -1 ) , ')', '') ,'(' ,'') ,'.xml' , '')+''' '''
from
#tblXML
select ''''+#tmp
or you don't want to single row use this
SELECT ''''+ replace (replace ( replace (SUBSTRING(TXMLFileName, CHARINDEX('(', TXMLFileName)
, CHARINDEX(')',TXMLFileName) -1 ) , ')', '') ,'(' ,'') ,'.xml' , '')+''''
from
#tblXML

Related

SQL: select the last values before a space in a string

I have a set of strings like this:
CAP BCP0018 36
MFP ACZZ1BD 265
LZP FEI-12 3
I need to extract only the last values from the right and before the space, like:
36
265
3
how will the select statement look like? I tried using the below statement, but it did not work.
select CHARINDEX(myField, ' ', -1)
FROM myTable;
Perhaps the simplest method in SQL Server is:
select t.*, v.value
from t cross apply
(select top (1) value
from string_split(t.col, ' ')
where t.col like concat('% ', val)
) v;
This is perhaps not the most performant method. You probably would use:
select right(t.col, charindex(' ', reverse(t.col)) - 1)
Note: If there are no spaces, then to prevent an error:
select right(t.col, charindex(' ', reverse(t.col) + ' ') - 1)
Since you have mentioned CHARINDEX() in question, I am assuming you are using SQL Server.
Try below
declare #table table(col varchar(100))
insert into #table values('CAP BCP0018 36')
insert into #table values('MFP ACZZ1BD 265')
insert into #table values('LZP FE-12 3')
SELECT REVERSE(LEFT(REVERSE(col),CHARINDEX(' ',REVERSE(col)) - 1)) FROM #table
Functions used
CHARINDEX ( expressionToFind , expressionToSearch ) : returns position of FIRST occurence of an expression inside another expression.
LEFT ( character_expression , integer_expression ) : Returns the left part of a character string with the specified number of characters.
REVERSE ( string_expression ) : Returns the reverse order of a string value

Combine source column values into one destination column

I have a source table which has the columns AreaCodeMobile + MobilePhoneNumber, AreaCodeHome + HomeNumber, AreaCodeOffice + OfficeNumber and I am wanting to combine these into one telephone column in my destination table. How would I accomplish this?
What are the data types of those columns? Also, is it any format that you are looking to save in destination column? If they are all int datatype, you need to use CAST to convert to varchar. For e.g.,
DECLARE #AreaCodeMobile int = 833, #MobilePhoneNumber INT = 7571256
DECLARE #areacodehome INT = 877, #homenumber INT = 2968997
DECLARE #areacodeoffice INT = 247 , #officenumber INT = 8658734
select CONCAT (
'Mobile Phone Number: ' , cast(#areacodemobile as varchar(50)) , cast(#MobilePhoneNumber as varchar(50))
,' Home Number: ', CAST(#areacodehome as varchar(50)) , cast(#homenumber as varchar(50))
,' Office Number: ', CAST(#areacodeoffice as varchar(50)) , cast(#officenumber as varchar(50))
)
Simply use CONCAT.
And use + to prefix a comma, or whatever separator you prefere.
SELECT t.*,
CONCAT(AreaCodeMobile,
', '+MobilePhoneNumber,
', '+AreaCodeHome,
', '+HomeNumber,
', '+AreaCodeOffice,
', '+OfficeNumber) AS [Telephones]
FROM YourTable t;
The trick here is that CONCAT will ignore NULL's.
But a + will result to NULL if a NULL is added.
So then no useless comma's are added for the null's.
Or you could also use CONCAT_WS, if your version of Sql Server supports it.
SELECT t.*,
CONCAT_WS(', ', AreaCodeMobile, MobilePhoneNumber, AreaCodeHome, HomeNumber, AreaCodeOffice, OfficeNumber) AS [Telephones]
FROM YourTable t;
So to update that column in your table, maybe something like this:
UPDATE YourTable
SET Telephones = CONCAT_WS(', ', AreaCodeMobile, MobilePhoneNumber, AreaCodeHome, HomeNumber, AreaCodeOffice, OfficeNumber)
WHERE Telephones IS NULL;

Find values in between two characters '(' and ')' in column that have nulls

I have a table name authors. I need to extract the values at the end of their names excluding '(' and ')'.
I have tried to use a substring with Charindex function.
select
isnull (SUBSTRING(name,CHARINDEX('(',name) +1 ,CHARINDEX(')',name) - CHARINDEX('(',name) - 1), '') as [Name]
from Authors
But I got an error message.
Msg 537, Level 16, State 3, Line 6
Invalid length parameter passed to the LEFT or SUBSTRING function.
Here is what I am expecting my results to be.
For your data, I would do something like this:
select coalesce(replace(stuff(name, 1, charindex('(', name + '(') + 1, ''), ')', ''),
'') as [Name]
This assumes that the parentheses are at the end of the string, as in the examples.
declare #string nvarchar(25)
set #string = '(asdfgh)'
select REPLACE( REPLACE(#string,'(',''),')','') where #string like '%[(]%%[)]%'
in above code i used regex to find string between ( and ) then replace them by empty
here you go, apply to your situation
declare #string varchar(25)
set #string = '(asdfgh)'
SELECT SUBSTRING(#string,
charindex('(', #string)+1,
charindex(')', #string)-
charindex('(', #string)-1)
You can use APPLY :
select substring(name, tt.startp + 1, (endp - startp) - 1) as Name
from Authors a cross apply
( values (charindex('(', name + ')'), charindex(')', name + ')'))
) tt(startp, endp);
Your Name column doesn't have a (...) in some places so, you can add either where clause or add explicitly (...) at the end of string.

SQL Query to List

I have a table variable in a stored procedure. What I want is to find all of the unique values in one column and join them in a comma-separated list. I am already in a stored procedure, so I can do it some way that way; however, I am curious if I can do this with a query. I am on SQL Server 2008. This query gets me the values I want:
SELECT DISTINCT faultType FROM #simFaults;
Is there a way (using CONCAT or something like that) where I can get the list as a single comma-separated value?
This worked for me on a test dataset.
DECLARE #MyCSV Varchar(200) = ''
SELECT #MyCSV = #MyCSV +
CAST(faulttype AS Varchar) + ','
FROM #Simfaults
GROUP BY faultType
SET #MyCSV = LEFT(#MyCSV, LEN(#MyCSV) - 1)
SELECT #MyCSV
The last part is needed to trim the trailing comma.
+1 to JNK - the other common way you will see, which doesn't require a variable is:
SELECT DISTINCT faulttype + ','
FROM #simfaults
FOR XML PATH ('')
Note that if faulttype contains characters like "<" for example, those will be xml encoded. But for simple values this will be OK.
this is how we do this
create table #test (item int)
insert into #test
values(1),(2),(3)
select STUFF((SELECT ', ' + cast(Item as nvarchar)
FROM #test
FOR XML PATH('')), 1, 2, '')
Without the space after the comma it would be;
select STUFF((SELECT ',' + cast(Item as nvarchar)
FROM #test
FOR XML PATH('')), 1,1, '')

Replace with wildcard, in SQL

I know MS T-SQL does not support regular expression, but I need similar functionality. Here's what I'm trying to do:
I have a varchar table field which stores a breadcrumb, like this:
/ID1:Category1/ID2:Category2/ID3:Category3/
Each Category name is preceded by its Category ID, separated by a colon. I'd like to select and display these breadcrumbs but I want to remove the Category IDs and colons, like this:
/Category1/Category2/Category3/
Everything between the leading slash (/) up to and including the colon (:) should be stripped out.
I don't have the option of extracting the data, manipulating it externally, and re-inserting back into the table; so I'm trying to accomplish this in a SELECT statement.
I also can't resort to using a cursor to loop through each row and clean each field with a nested loop, due to the number of rows returned in the SELECT.
Can this be done?
Thanks all - Jay
I think your best bet is going to be to use a recursive user-defined function (UDF). I've included some code here that you can use to pass in a string to achieve the results you're looking for.
CREATE FUNCTION ufn_StripIDsFromBreadcrumb (#cIndex int, #breadcrumb varchar(max), #theString varchar(max))
RETURNS varchar(max)
AS
BEGIN
DECLARE #nextColon int
DECLARE #nextSlash int
SET #nextColon = CHARINDEX(':', #theString, #cIndex)
SET #nextSlash = CHARINDEX('/', #theString, #nextColon)
SET #breadcrumb = #breadcrumb + SUBSTRING(#theString, #nextColon + 1, #nextSlash - #nextColon)
IF #nextSlash != LEN(#theString)
BEGIN
exec #breadcrumb = ufn_StripIDsFromBreadcrumb #cIndex = #nextSlash, #breadcrumb = #breadcrumb, #theString = #theString
END
RETURN #breadcrumb
END
You could then execute it with:
DECLARE #myString varchar(max)
EXEC #myString = ufn_StripIDsFromBreadcrumb 1, '/', '/ID1:Category1/ID2:Category2/ID3:Category3/'
PRINT #myString
This works for SQL Server 2005 and up.
create table strings (
string varchar(1000)
)
insert into strings values( '/ID1:Category1/ID2:Category2/ID3:Category3/' )
insert into strings values( '/ID4:Category4/ID5:Category5/ID8:Category6/' )
insert into strings values( '/ID7:Category7/ID8:Category8/ID9:Category9/' )
go
with
replace_with_wildcard ( restrung ) as
(
select replace( string, '', '' )
from strings
union all
select
replace( restrung, substring( restrung, patindex( '%ID%', restrung ), 4 ), '' )
from replace_with_wildcard
where patindex( '%ID%', restrung ) > 0
)
select restrung
from replace_with_wildcard
where charindex( ':', restrung ) = 0
order by restrung
drop table strings
You might be able to do this using a Split function. The following split function relies on the existence of a Numbers table which literally contains a sequential list of numbers like so:
Create Table dbo.Numbers( Value int not null primary key clustered )
GO
With Nums As
(
Select ROW_NUMBER() OVER( Order By o.object_id ) As Num
From sys.objects as o
cross join sys.objects as o2
)
Insert dbo.Numbers( Value )
Select Num
From Nums
Where Num Between 1 And 10000
GO
Create Function [dbo].[udf_Split] (#DelimitedList nvarchar(max), #Delimiter nvarchar(2) = ',')
Returns #SplitResults TABLE (Position int NOT NULL PRIMARY KEY, Value nvarchar(max))
AS
/*
PURPOSE: to split the #DelimitedList based on the #Delimter
DESIGN NOTES:
1. In general the contents of the next item is: NextDelimiterPosition - CurrentStartPosition
2. CurrentStartPosition =
CharIndex(#Delimiter, A.list, N.Value) = Current Delimiter position
+ Len(#Delimiter) + The number of delimiter characters
+ 1 + 1 since the text of the item starts after the delimiter
3. We need to calculate the delimiter length because the LEN function excludes trailing spaces. Thus
if a delimiter of ", " (a comma followed by a space) is used, the LEN function will return 1.
4. The DataLength function returns the number of bytes in the string. However, since we're using
an nvarchar for the delimiter, the number of bytes will double the number of characters.
*/
Begin
Declare #DelimiterLength int
Set #DelimiterLength = DataLength(#Delimiter) / 2
If Left(#DelimitedList, #DelimiterLength) <> #Delimiter
Set #DelimitedList = #Delimiter + #DelimitedList
If Right(#DelimitedList, #DelimiterLength) <> #Delimiter
Set #DelimitedList = #DelimitedList + #Delimiter
Insert #SplitResults(Position, Value)
Select CharIndex(#Delimiter, A.list, N.Value) + #DelimiterLength
, Substring (
A.List
, CharIndex(#Delimiter, A.list, N.Value) + #DelimiterLength
, CharIndex(#Delimiter, A.list, N.Value + 1)
- ( CharIndex(#Delimiter, A.list, N.Value) + #DelimiterLength )
)
From dbo.Numbers As N
Cross Join (Select #DelimitedList As list) As A
Where N.Value > 0
And N.Value < LEN(A.list)
And Substring(A.list, N.Value, #DelimiterLength) = #Delimiter
Order By N.Value
Return
End
You then might be able to run a query like so where you strip out the prefixes:
Select Table, Substring(S.Value, CharIndex(':', S.Value) + 1, Len(S.Value))
From Table
Cross Apply dbo.udf_Split(Table.ListColumn, '/') As S
This would give you values like:
Category1
Category2
Category3
You could then use FOR XML PATH to combine them again:
Select Table.PK
, Stuff( (
Select '/' + Substring(S.Value, CharIndex(':', S.Value) + 1, Len(S.Value))
From Table As Table1
Cross Apply dbo.udf_Split(Table.ListColumn, '/') As S1
Where Table1.PK = Table.PK
Order By S1.Position
For Xml Path('')
), 1, 1, '') As BreadCrumb
From Table
For SQL Server 2005+, you can get regex support by:
Enabling CLR (doesn't require instance restart)
Uploading your CLR functionality (in this case, regex replace)
Using native TSQL, you'll need to define REPLACE statements for everything you want to remove:
SELECT REPLACE(
REPLACE(
REPLACE(''/ID1:Category1/ID2:Category2/ID3:Category3/'', 'ID1:', ''),
'ID2:', ''),
'ID3:', '')
Regex or otherwise, you need to be sure these patterns don't appear in the actual data.
You can use SQL CLR. Here's an MSDN article:
declare #test1 nvarchar(max)
set #test1='/ID1:Category1/ID2:Category2/ID3:Category3/'
while(CHARINDEX('ID',#test1)<>0)
Begin
select #test1=REPLACE(#test1,SUBSTRING(#test1,CHARINDEX('ID',#test1),CHARINDEX(':',#test1)-
CHARINDEX('ID',#test1)+1),'')
End
select #test1