Get the specified input parameter from stored procedure - sql-server

I want to get all the related information from database. For example I have a summary column which contains data like "A01.1 | Foodland Inbound QIP Details and Pre Unloading" and I want to pass only "A01" in input parameter and get all the data related to "A01"

select * from TableName where summary_column like 'A01%';

Based on the code in your comment, the where clause will only find a summary that is the equal to A01.2 e.g:
TaskMaster.Summary =#summary
You want to use LIKE and a wild card:
WHERE TaskMaster.Summary LIKE '''' + #summary + '%' + ''''
The above will find everything that starts with A01.2, so if you want to find all with A01, pass #summary as A01

Related

SQL XML parsing using a attribute value supplied by another field in the same row

Context: I'm scraping some XML form descriptions from a Web Services table in hopes of using that name to identify what the user has inputted as response. Since this description changes for each step (row) of the process and each product I want something that can evaluate dynamically.
What I tried: The following was quite useful but it returns a dynamic attribute query result in it's own field ans using a coalesce to reduce the results as one field would lead to it's own complications: Get values from XML tags with dynamically specified data fields
Current Attempt:
I'm using the following code to generate the attribute name that I will use in the next step to query the attribute's value:
case when left([Return], 5) = '<?xml'
then lower(cast([Return] as xml).value('(/response/form/*/#name)[1]','varchar(30)'))
else ''
end as [FormRequest]
And as part of step 2 I have used the STUFF function to try and make the row-level query possible
case when len(FormRequest)>0
then stuff( ',' + 'cast([tmpFormResponse] as xml).value(''(/wrapper/#' + [FormRequest] + ')[1]'',''varchar(max)'')', 1, 1, '')
else ''
end as [FormResponse]
Instead of seeing 1 returned as my FormReponse feild value for the submit attribute (please see in yellow below) it's returning the query text -- cast([tmpFormResponse] as xml).value('(/wrapper/#submit)1','varchar(max)') -- instead (that which should be queried).
How should I action the value method so that I can dynamically strip out the response per row of XML data in tmpFormResponse based on the field value in the FormRequest field?
Thanx
You can check this out:
DECLARE #xml XML=
N'<root>
<SomeAttributes a="a" b="b" c="c"/>
<SomeAttributes a="aa" b="bb" c="cc"/>
</root>';
DECLARE #localName NVARCHAR(100)='b';
SELECT sa.value(N'(./#*[local-name()=sql:variable("#localName")])[1]','nvarchar(max)')
FROM #xml.nodes(N'/root/SomeAttributes') AS A(sa)
Ended up hacking up a solution to the problem by using PATINDEX and CHARINDEX to look for the value in the [FormRequest] field in the he tmpFormResponse field.

Conditionally count based on other field data in SSRS Report

I am working SSRS report, I have a field named as Details and I would like to get the count of other field SerialNumber.
So in short I want to get the total count of SerialNumber which has no Details.
I tried below but not working. It always give the total count count without considering blank Details
=CStr(COUNT(IIF(Not(IsNothing(Fields!Details.Value)),Fields!SerialNumber.Value,0)))
How can I achieve this by expression? Please help.
Issue was with my data. In dataset I was fetching rows based on Isnull() like below
SELECT CASE
WHEN 'Y'='Y' THEN ISNULL(Code ,'') + ' ' + ISNULL(Description ,'')
ELSE CASE
WHEN ISNULL(Code ,'')='' THEN ISNULL(RefCharge ,'')
+' '+ISNULL(RefDescription ,'')
ELSE ISNULL(Code ,'')+' '+ISNULL(Description ,'')
END
END AS Details
So it was showing (3 blank spaces) and my expression was.
=COUNT(IIF(Fields!Details.Value<>"",Fields!Number.Value,Nothing))
Finally I tried below, now it's working fine now.
=COUNT(IIF(Trim(Fields!Details.Value)<>"",Fields!Number.Value,Nothing))
Note: So I have noted that whenever we need to check such kind of conditions we must have to use Trim()

SQL Server - remove HTML Tag from a varchar

I know there are a few about similar topics on here but I can't find one related to my issue, this is it:
I have a table with an ID Column and a QRCode column. each time an item is added the primary key auto increments. The QRCode will scan in to be like the following:
"http://somewebsite.com/12345/987654321"
i want to be able to remove the "http://somewebsite.com/" from the string, I know how to do this in C# however I am unsure of how to do this in Sql Server. any guidance would be great, thanks
Regular formats are like the following, and used in the example below.
"http://somewebsite.com/12345/456564654"
"http://somewebsite.com/12345/989886765"
"http://somewebsite.com/12346/987654321"
the query returns the following results:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'
"12345 456564654"
"12345 989886765"
"12346 987654321"
Now i need to update the table with those new results however as there's 3 results, i get the error message "Subquery returned more than 1 value". is there a way to replace the selected values in the table with the ones that exist based on the primary key field?
**Removed previous example
A more complete answer based on your updated question. This removes the first portion of the URL as well as the trailing / so that you get your desired output.
DECLARE #Variable VARCHAR(50)
SET #Variable = 'http://somewebsite.com/12345/456564654'
SET #Variable =
REPLACE
(
REPLACE(#Variable, 'http://somewebsite.com/', '')
,'/', ' '
)
PRINT #Variable
Output = 12345 456564654
Looking at your SQL statement you want this:
SELECT
REPLACE
(
REPLACE(QRCode, 'http://somewebsite.com/', '')
,'/', ' '
) AS QRCode
FROM
QRTable
WHERE
QRCode LIKE '%http://somewebsite.com/%'

How to remove a full stop from the end of data

I need to update my table because a number of fields in a column have got a fullstop on the end, i need to remove this.
So, in TableA, Field1: the data looks like
1002243.
1007053.
1007403.
1104098.
1110010.
NOTE: Not all the fields are the same length.
I need to remove the full stops.
Im using SQL Server 2005, cheers :)
UPDATE TableA
SET Field1 = LEFT(Field1 ,LEN(Field1)-1)
WHERE Field1 LIKE '%.'
Then you have data after the period. Try this to see:
Print '[' + Field1 + ']' and see if you get something like this:
[101. ]
There is a space after the period.

SQL Server : LENGTH() inside of REPLICATE()

I have a SQL Server table with the columns Lvl and Title. I need to insert a "-" in front of the title for every character in the Lvl field.
As an example: If Lvl = 111 the title should become --- My Title.
I can only edit the following SQL string. There is no possibility to create other functions or likewise.
SELECT REPLICATE('_', { fn LENGTH(Lvl) }) + ' ' + Title AS Title
FROM Documents
My problem is, that the LENGTH() function doesn't work inside the REPLICATE() function. Does anybody know why or how to solve this problem?
Thank you.
Try this:
SELECT REPLACE(Lvl, '1', '-') + ' ' + Title as Title
FROM Documents
Simply take the Lvl column, and replace all instances of 1 with whatever character you want, then concatenate Title to the end of the result.
Try this. It works fine for me -
select REPLICATE('-',LEN(Lvl)) + ' ' + Title as title from documents

Resources