How do i use try catch or conditional columns to solve csv issue in power bi - try-catch

I have a dataset coming into power bi from CSV where certain rows have been pushed to the right because there's a comma in the description. Once i apply a datatype change, this creates errors with the presence of text in a numeric column.
What i want to do is something like = try [col1] otherwise [col2], except that i want to do this for several columns where the result isn't necessarily the column where the error is.
So if there's an error in column 1, give me the value in column 3, but if there's no error in column 1, then column 2 has the value i want. So something like = if([Col1] = Error, [Col3], [Col2])

Try this:
if (try [col1])[HasError] then [col3] else [col2]
Here is the reference I used to create this expression:
https://learn.microsoft.com/en-us/power-query/handlingerrors

Related

MSSQL - Select NULL values on varbinary column

I am trying to do a select on a table with around 35.000 rows, one of the columns is a varbinary with may have some NULL values and I need to exclude those NULL values from my results.
The problem is: when you do a select on a varbinary column, it takes a lot of time to complete, I've found a way that I'm not sure I can use or is the best way to do something like that and I would like some opinions.
Here is it:
SELECT REQUEST,REQLOCATION,DESCRIPT,BLOBNAME,BLOBSIZE,substring(BLOBVALUE,0,1) AS BLOBVALUE,BLOBMODE,BLOBPATH,BLOBID,
REDIRID,ANALYST,CLIENT,SEVENT,PACKAGE,INSERTDATE
FROM REQBLOB WHERE substring(BLOBVALUE,0,1) IS NOT NULL
The varbinary column is the "BLOBVALUE" one where I do a "substring" select and this query gave me a result of 20.000 rows instantly and I think it's returned only valid data, not NULLs, what you think about that?
Get rid of the SUBSTRING in the WHERE, it's making your query non-SARGable; that's why it's slow.
SELECT REQUEST,
REQLOCATION,
DESCRIPT,
BLOBNAME,
BLOBSIZE,
SUBSTRING(BLOBVALUE, 0, 1) AS BLOBVALUE,
BLOBMODE,
BLOBPATH,
BLOBID,
REDIRID,
ANALYST,
CLIENT,
SEVENT,
PACKAGE,
INSERTDATE
FROM REQBLOB
WHERE BLOBVALUE IS NOT NULL;
Why are you using substring on a varbinary anyway though..?

Fetching results from inner query having comma separated values - SQL Server

I have a temp table having two columns - key and value:
temp_tbl:
key value
---|-----
k1 | a','b
Below is the insert script with which I am storing the value in temp_tbl:
insert into temp_tbl values ('k1', 'a'+char(39)+char(44)+char(39)+'b');
Now, I want trying to fetch records from another table (actual_tbl) like this:
select * from actual_tbl where field_value in
(select value from tamp_tbl where key = 'k1');--query 1
But this is not returning anything.
I want the above query to behave like the following one:
select * from actual_tbl where field_value in
('a','b');--query 2
Where am I doing wrong in query 1?
I am using sql server.
Where am I doing wrong in query 1?
Where you are going wrong is in failing to understand the way the IN keyword works with a subquery vs a hard-coded list.
When an IN clause is followed by a list, each item in the list is a discrete value:
IN ('I am a value', 'I am another value', 'I am yet another value')
When it's followed by a sub-query, each row generates a single value. Your temp table only has one row, so the IN clause is only considering a single value. No matter how you try to "trick" the parser with commas and single-quotes, it won't work. The SQL Server parser is too smart to be tricked. It will know that a single value of 'a','b' is still just a single value, and it will look for that single value. It won't treat them as two separate values like you are trying to do.

Query to compare differences of two columns from two different tables

I am attempting to create a UNION ALL query, on two differently named columns in two different tables.
I would like to take the "MTRL" column in the table USER_EXCEL and compare it against the "short_material_number" column from the IM_EXCEL table. I would then like the query to only return the differences between the two columns. Both columns house material numbers but are named differently (column wise) in the tables.
What I have so far is:
(SELECT [MTRL] FROM dbo.USER_EXCEL
EXCEPT
SELECT [short_material_number] FROM dbo.IM_Excel)
UNION ALL
(SELECT [short_material_number] FROM dbo.IM_Excel
EXCEPT
SELECT [MTRL] FROM dbo.USER_EXCEL)
However, when trying to run that query I receive an error message that states:
Msg 8114, Level 16, State 5, Line 22
Error converting data type varchar to float.
You're almost certainly getting this error because one of your two columns is a FLOAT data type, while the other is VARCHAR. This article would be a good place to start reading about implicit conversions, which happen when you try to compare two columns that are of different data types.
To get this working, you need to convert the float to a varchar, like in the example below.
(
SELECT [MTRL] FROM dbo.USER_EXCEL
EXCEPT
SELECT CAST([short_material_number] AS VARCHAR(18)) FROM dbo.IM_Excel
)
UNION ALL
(
SELECT CAST([short_material_number] AS VARCHAR(18)) FROM dbo.IM_Excel
EXCEPT
SELECT [MTRL] FROM dbo.USER_EXCEL
)
From you question I understand you are trying to compare two columns but returning only one column I would recommend you to use following query to compare the differences side by side
SELECT ue.[MTRL], ie.[short_material_number]
FROM dbo.IM_Excel ie
FULL OUTER JOIN
dbo.USER_EXCEL ue
ON CAST(ie.[short_material_number] AS VARCHAR(20)) = ue.[MTRL]
WHERE ie.[short_material_number] IS NULL
OR ue.[MTRL] IS NULL

Inserting data into one column fails saying values in another column can't be null

I can't understand this misunderstanding by SQL Server.
As you can see, I'm trying to insert into column Ordamount, but SQL Server shows me in its error message that it can't insert null into column UserID?
Declare #variable1 int =( select sum(Orr.quantity *OI.Iteprice)
from Orderrouter Orr
inner join OrdItem OI on Orr.OrdItems =OI.ItemId
where OrdId = 1)
insert into Ord (Ordamount)
values (#variable1);
Error:
Msg 515, Level 16, State 2, Line 6 Cannot insert the value
NULL into column 'UserID', table 'Example.dbo.Ord'; column does
not allow nulls. INSERT fails.
The statement has been terminated.
For columns in the Ord table that does not allow null by default, you have to provide value for, you cannot skip them. You have to provide value for UserID if it's not Nullable unless it's an identity column
I think the problem is that you only specify the amount when inserting which isn't the primary key in the table, might be an idea to show how the table looks as well. An auto increment on the id column migh solve the problem
Please use a more descriptive title.
Beside this, it seems that the Ord.UserID field is not initialized. Maybe it is not an autoincrement.
Try with this, specifying the UserId value:
INSERT into Ord values(<UserId value here>, #Varaible1)

"Error converting data type varchar to numeric." - What column?

I have a huge INSERT-statement with 200 columns and suddendly I get the dreaded Error converting data type varchar to numeric. Is there somewhere I can see the actual column that contains the "varchar" value? I know I can remove one of the columns at a time until the error disappears, but it's very tedious.
Unfortunately, this error is a serious pain and there's no easy way to troubleshoot it. When I've encountered it in the past, I've always just had to comment out groups of columns until I find the culprit.
Another approach might be to use the ISNUMERIC() function in in T-SQL to try and find the culprit. Assuming every column in your destination table is numeric (adjust accordingly if it's not), you could try this:
SELECT *
FROM SourceTable
WHERE ISNUMERIC(Column1) = 0
OR ISNUMERIC(Column2) = 0
OR ISNUMERIC(Column3) = 0
OR ISNUMERIC(Column4) = 0
...
This will expose the row that contains your non-numeric value, and should make it pretty clear which column it's in. I know it's tedious, but at least it helps you hunt down the actual value, in addition to the column that's causing trouble.
You don't specify SQL Server Version or number of rows.
For SQL2005+ adding the OUTPUT clause to the INSERT might help identify the rogue row in that it will output the inserted rows until it encounters an error so the next row is the one with the problem
DECLARE #Source TABLE
(
Col1 VARCHAR(10),
Col2 VARCHAR(10)
)
INSERT INTO #Source
SELECT '1','1' UNION ALL
SELECT '2','2' UNION ALL
SELECT '3','3' UNION ALL
SELECT '4A','4' UNION ALL
SELECT '5','5'
DECLARE #Destination TABLE
(
Col1 INT,
Col2 VARCHAR(10)
)
INSERT INTO #Destination
OUTPUT inserted.*
SELECT *
FROM #Source
Returns
(5 row(s) affected)
Col1 Col2
----------- ----------
1 1
2 2
3 3
Msg 245, Level 16, State 1, Line 23
Conversion failed when converting the varchar value '4A' to data type int.
Well, this is just a hunch but what about inserting the data to a temporary table and the using the GUI to migrate the data to the other table? If it still generates an error, you should at least be able to get more feedback on that non-numerical column...
If it doesn't work, consider trying this.
Cheers!

Resources