Report Builder .rdl Check Array Key Exists - ssrs-2008

I am making a report and I need to split a coma separated string into three columns of a table.
string = 'some text, some text, some text'
But the sting doesn't always have two coma's i.e.
string = 'some text, some text'
so when i try to get the value for the third column
=Split(Fields!GLDescription.Value, ", ").GetValue(2)
This code can result in a "#Error" message in the column. I tried to solve this by checking the length like so
=IIF(Split(Fields!GLDescription.Value, ", ").Length >= 3, Split(Fields!GLDescription.Value, ", ").GetValue(2), "")
But it still resulted in the same error. Is there anyway to check if an array key exists?

The issue, as you've seen, is that SSRS IIf expressions aren't good at short circuiting. I can think of a workaround that will work for 2 and 3 column fields.
Try an expression like:
=IIf(
Split(Fields!GLDescription.Value, ", ").Length = 3
, Mid(
Fields!GLDescription.Value
, InStrRev(Fields!GLDescription.Value, ", ") + 2
, Len(Fields!GLDescription.Value) - InStrRev(Fields!GLDescription.Value, ", ") + 2
)
, "No val 3"
)
With dataset:
Gives result:
It's not bulletproof for all possible situations, but might be enough for your data.

Related

Trying to extract text using CHARINDEX ()- 1 but getting an error

I have a column with Names, and I am trying to split the column into First and Last Name using Text functions such as LEFT/SUBSTRING/CHARINDEX.
Data in the column:
Name
Yang, Jon
Huang, Eugene
Torres, Ruben
Zhu, Christy
Johnson, Elizabeth
Everything works fine as long as I use this code:
SELECT
[Name]
--,LEFT([Name], CHARINDEX(' ', [Name])) AS FirstName
,SUBSTRING([Name], 1, CHARINDEX(' ', [Name] )) AS FirstName
FROM
DataModeling.Customer
But the problem arises when I try to subtract 1 from CHARINDEX to exclude the Comma from the result and it throws this error:
I have done this operation many times in Excel so trying to replicate it with TSQL. Any suggestion on what I am doing wrong is helpful.
You get that error when CHARINDEX(' ', [Name] ) return 0. So minus 1 will make it negative and it is invalid value for substring()
You can use CASE expression to check the return value from CHARINDEX() and return the correct value to substring()
Or, you can "cheat" by using
CHARINDEX( ' ', [Name] + ' ' )
So CHARINDEX() will always return a value that is more than 0

TSQL query to extract a value between to char where a specific set of characters is there

I have a problem I can't seem to figure out. I am trying to extract capacity from a product description. It is always between two values, "," and "oz." however there could be other commas included in the description that are not part of what I'm trying to extract. Example value is , 15 oz., or , 2 oz.,
I'm trying to find values that have the oz in them and are between two commas and I have been completely unsuccessfully. I've tried many things, but here is the latest that I have tried today and I'm just getting an error.
SELECT SUBSTRING(
FullDescription,
CHARINDEX(',', FullDescription),
CHARINDEX('oz.',FullDescription)
- CHARINDEX(',', FullDescription)
+ Len('oz.')
)
from CatalogManagement.Product
Since the backwards pattern ,.zo is more recognisable, I'd go with the REVERSE function
Sample values:
"something, something more, 18oz., complete"
"shorter, 12oz., remainder"
"there is no capacity, in this, value"
"a bit more, 14oz, and some followups, maybe"
SELECT REVERSE(
SUBSTRING (
REVERSE(FullDescription),
CHARINDEX(',.zo', REVERSE(FullDescription)) + 1,
CHARINDEX(',', REVERSE(FullDescription), CHARINDEX(',.zo', REVERSE(FullDescription)) + 1) - CHARINDEX(',.zo', REVERSE(FullDescription)) - 1
)
)
FROM CatalogManagement.Product
WHERE FullDescription LIKE '%oz.,%'
You might use XML-splitting together with a XQuery predicate:
DECLARE #tbl TABLE(ID INT IDENTITY, YourString VARCHAR(MAX));
INSERT INTO #tbl VALUES('Here is one with an amount, 1 oz., some more text')
,('Here is one with no amount, some more text')
,('a, 10 oz.')
,('b, 20oz., no blank between oz and the number')
,('30oz., starts with the pattern, no leading comma');
SELECT t.*
,A.oz.value('.','nvarchar(max)') oz
FROM #tbl t
CROSS APPLY(SELECT CAST('<x>' + REPLACE((SELECT t.YourString AS [*] FOR XML PATH('')),',','</x><x>') + '</x>' AS XML)
.query('/x[contains(text()[1],"oz.")]')) A(oz);
The idea in short:
We use some string methods to replace commas with XML tags and to cast your string to XML. each fragment is placed within a decent <x> element.
We use a predicate to return just the fragments containing "oz.".
You can filter easily with
WHERE LEN(A.oz.value('.','nvarchar(max)'))>0

SPLIT() then JOIN() and populate column with ARRAYFORMULA()

I am trying to Split email addresses in column E2:E on the "dot" operator and then Join the names back into a single cell. I have written the below formula enclosed with ARRAYFORMULA() to populate the cells in Column A. But i keep getting the following error:
JOIN range must be a single row or a single column.
=ARRAYFORMULA( IF( E2:E = "", "", JOIN( " ", SPLIT( SUBSTITUTE( FILTER( E2:E, LEN( E2:E ) > 0 ), "#abc.com", "", 1 ), ".") ) ) )
Is there a workaround solution?
I think i found the answer:
=ARRAYFORMULA(IF(E2:E="","",TRANSPOSE(QUERY(TRANSPOSE(SPLIT(SUBSTITUTE(FILTER(E2:E,LEN(E2:E)>0),"#ABC.COM","",1),".")),,50000))))

How can I query against a concatenated field?

I would like to search for a first name and surname in two sql fields using entity framework. Below is some example code where I am searching for searchString as a concatenation of firstname and lastname database strings.
using(var ent = new testEntities)
{
ent.Contacts.Where(u => (u.firstname + ' ' + u.lastname).contains(searchString))
}
Searching a single field is easy but how could I query for a string which is a concatenation of two database fields.
From the comments: [I get an error] about unable to create a constant value of type char.
The problem with your expression is the way you add a space in between the first and second string. EF knows how to translate string concatenation to SQL, but it does not know how to translate + between a field of string type and space ' ', a constant of type char.
Replacing concatenation expression with u.firstname + " " + u.lastname will fix the problem, because the space " " is now a string literal, not a char constant.
Replace code with below code that will help you i think.
using(var ent = new testEntities)
{
ent.Contacts.Where(u => (u.firstname + " " + u.lastname).contains(searchString))
}

How to represent spaces in Perl's DBI properly

I have a record in an Informix table. The table columns look like this:
acct_no integer,
suffix char(1),
meter_num char(20),
date_read datetime year to second not null ,
counter smallint,
reading integer not null ,
typeofread char(1),
estimated char(1),
time_billed datetime year to second
Using Informix's dbaccess tool:
select *
from ws_mtrread
where acct_no = 113091000
and suffix = " "
order by date_read desc;
this result (newest shown) is returned and works whether or not I use one or two spaces for suffix.
acct_no 113091000
suffix
meter_num 34153205
date_read 2013-09-09 23:31:15
counter 0
reading 1240
typeofread g
estimated
time_billed 2013-10-22 11:48:21
However, this Perl DBI query
my $sql_statement =
"select * ".
"from ws_mtrread ".
"where acct_no = ? ".
"and suffix = ? ".
"order by date_read desc ; ";
does not work. I can fetch the row without specifying $suffix, so I know the row exists.
I believe this is an error on my part in representing the suffix. In this example suffix is equal to a string of two spaces.
How do I represent spaces correctly, so the query works? Here is the rest of the code I used to fetch the row.
my $test_acct_no = 113091000;
my $suffix = " ";
my $pt_sel_hdl = $DBHdl->prepare($sql_statement);
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
my $ws_mtr_read_rec_ref = $pt_sel_hdl->fetchrow_hashref;
After the call, $ws_mtr_read_rec_ref is undefined.
Don't use DBI's quote method here:
$pt_sel_hdl->execute($test_acct_no, $DBHdl->quote($suffix));
When you use ? placeholders in your SQL, the database driver will correctly parameterize the query arguments that you pass to execute. You are probably creating a query that is searching for the literal string " " (including the quotes) when you want to search for (just two spaces.)
So this should be all you need:
$pt_sel_hdl->execute($test_acct_no, $suffix);