How to get consolidated errors in column in SQL Server - sql

I have a requirement which needs to find out consolidated error report of all columns containing error report. Is there any function or any ways to do this in SQL Server?
I tried this using a CASE statement but it only gives error report for one column.
I need the result like this after applying filter or procedure

You can use CASE:
select
row,
case when length(name) > 10
then 'Name max=10 Present=' + length(name) + '; '
else '' end +
case when length(place) > 10
then 'Place max=10 Present=' + length(place) + '; '
else '' end +
case when length(address) > 10
then 'Address max=10 Present=' + length(address) + '; '
else '' end as error
from t

Related

SQL subtracting a string column from another string column

I am looking to subtract two string columns from another string column.
I have acheived this in the past in Oracle using the below
SELECT
C.MANUFACTURER,
C.MODEL_GROUP,
REGEXP_REPLACE(C.VARIANT, '^'||C.MANUFACTURER || ' +' || C.MODEL_GROUP) "VAR DESC",
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C
I now need to acheive the same in MS SQL any suggestions would be appreciated.
"Subtract" is surely a misnomer here. The OP appears to want to remove a string at the beginning of another string.
From this point on, I am assuming that MANUFACTURER and MODEL_GROUP do not contain any regular expression wildcard characters.
There is a challenge with multiple spaces. If there are not too many, then you can do. For instance, this handles one or two spaces:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%'
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%' AND
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
EDIT:
Here is a more complete solution:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' %' + C.MODEL_GROUP + '%' AND
C.VARIANT NOT LIKE C.MANUFACTURER + ' %[^ ]%' + C.MODEL_GROUP + '%'
THEN STUFF(LTRIM(STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER), '')), 1, LEN(C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
This tests for just spaces between the two strings (the not like is checking for something other than a space). The replacement is then in three steps -- remove the manufacturer, then the spaces, then the model group.
I have spent some time on this one as much for my own satisfaction and in addition to the answer above, I have come up with the following which also generates the correct answer.
SUBSTRING(FV.VARIANT, LEN(FV.MANUFACTURER + ' ' + FV.MODEL_RANGE + ' ') + 1 , LEN(FV.VARIANT))
My problem was kinda easier to solve, I just needed to remove "xxxx" (the first 4 characters) at the start of a column from some rows:
UPDATE coluna SET columToEdit = SUBSTRING(columToEdit, 5, 200);

Using a CASE function

I am using Oracle and am trying to build out some sql for the following scenario:
On EMPLOYEES table, if employee has ADDRESS3 not equal to ' ', populate this field with ADDRESS2 else, populate this field with ADDRESS1.
...
, ADDRESS_LINE2 = NVL((
SELECT (CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END)
FROM EMPLOYEES t2
WHERE t2.EMPLID = PS_Z_EXS_EMP_TBL_T.EMPLID
), t2.ADDRESS1)
...
but it keeps giving me an error message about missing the right parenthesis. When I comment this bit out though it runs fine. DOes anyone know what I'm doing wrong?
CASE has two variants - both needs WHEN clauses.
One variant can have complete and complex boolean expression in each WHEN clause:
CASE
WHEN t2.ADDRESS3 != ' ' THEN t2.ADDRESS2
ELSE t2.ADDRESS1
END
In the other variant each WHEN clause contain values to be tested for the CASE expression:
CASE t2.ADDRESS3
WHEN ' ' THEN t2.ADDRESS1
ELSE t2.ADDRESS2
END
The last one cannot do != so therefore "reversed" logic ;-)
You need the END keyword at the end of a case expression:
CASE t2.ADDRESS3 != ' ' THEN t2.ADDRESS2 ELSE t2.ADDRESS1 END

Using mathematical operators in multi variable parameters

I'm wondering if it's possible to include mathematical operators and situations in a multivariable parameter.
In my example, I'm looking to have the freedom to enter 'Overdue' 'Less than one day' or 'More than one day' as #SLA_Days.
Currently, I can enter any of these one variables and it will return a result.
if #sla_days = 'Overdue'
BEGIN
SELECT #sql = #sql + 'and sla_days < 0'
END
ELSE
if #sla_days = 'Less than one day'
BEGIN
SELECT #sql = #sql + 'and sla_days <1 and sla_days >0'
END
ELSE
if #sla_days = 'More than one day'
BEGIN
SELECT #sql = #sql + 'and sla_days > 1'
END
EXEC sp_exectuesql #SQL
I do have a function that I've created that will look a comma delimited string and pull out variables. I do not need help in creating this
What I do need help is, is it possible to enter #SLA_days = 'Overdue,Less than one day,More than one day' and still have it recognize these rules?
Therefore, if I selected Overdue and less than one day, it would return both values where sla_days < 0 and sla days < 1
You don't need a function to pull out variables. This will perform much better.
The trick for your specific problem is to use OR instead of AND, because the value in a column could never be, for example, > 0 AND < 0. Also I add commas to each side of the variable so that conditions can be identified in any order. Note, though, that it will not find values if there are spaces between the commas as well - you'll have to adjust for that.
SET #SLA_days = ',' + #SLA_days + ',';
SET #sql = #sql + ' (1=2 '
+ CASE WHEN #SLA_days LIKE '%,Overdue,%'
THEN ' OR (sla_days < 0)' ELSE '' END
+ CASE WHEN #SLA_days LIKE '%,Less than one day,%'
THEN ' OR (sla_days < 1 AND sla_days > 0)' ELSE '' END
+ CASE WHEN #SLA_days LIKE '%,More than one day,%'
THEN ' OR (sla_days > 1)' ELSE '' END + ')';
Finally, do you really need to pass these big ugly strings to the database? Presumably the users are selecting from a multi-select or checking boxes, in which case you could use much tidier coefficients to represent each case.

SQL Select Error When using Case

CASE WHEN PFW_Access__c = 1
THEN 'New Employee (' + New_Employee_Name__c + ') needs PFW Access'
ELSE ''
END AS PFWAccessDesc,`
The line above is just one of many lines in my select statement. It outputs correctly with New Employee (name) needs PFW Access. What I'm trying to do is to add another field to the output Description_Short__c but when I tried to add it to the statement I receive an error. I'm not sure how to add it in to the statement to show in the output.
is this waht you are after?
CASE WHEN PFW_Access__c = 1
THEN 'New Employee (' + ISNULL(New_Employee_Name__c,'') + ') needs PFW Access '
+ISNULL(Description_Short__c,'')
ELSE ''
END AS PFWAccessDesc,
If I remember correctly from a previous question, Description_Short__c is a text field in your database. If you're trying to do just about anything w/ this column, you're going to have to use cast(Description_Short__c as varchar(max)) instead of Description_Short__c.

SQL Query to concatenate strings or add default value

I have to create a View that shows a field created by concatenating some other fields. The simple query that I use is this one:
SELECT
CODPROY, DESCPROY, USER, CODPROY + ' - ' + USER + ' - ' + DESCPROY AS Expr
FROM
dbo.PROY
The problem is that USER may be NULL and in this case I have to insert a default text, something like 'NOT AVAILABLE'. Can this be done in a view?
Use coalesce if you are using SQL Server
SELECT
CODPROY,
DESCPROY,
USER,
CODPROY + ' - ' + COALESCE(USER,'NOT AVAILABLE') + ' - ' + DESCPROY AS Expr
FROM dbo.PROY
Yes, it can be done. You need to use IFNULL (mySQL) or something like it ..
CREATE VIEW foobar as
SELECT CODPROY
, DESCPROY
, USER
, CODPROY + ' - ' + IFNULL(USER,'DEFAULT') + ' - ' + DESCPROY AS Expr
FROM dbo.PROY;