Forced update of first letters if present else mark it as null when empty
IF LENGTH([Column])>0 THEN UPDATE [Table] SET Position = Upper([Column]) ELSE RETURN NULL END IF;
Col A
abcdef
defghi
efgijg
''
elllef
''
ijkmnk
Expected Result (first two as capital and marked as null if row value is empty)
Col A
ABcdef
DEfghi
EFgijg
NULL
ELllef
NULL
IJkmnk
You can do this with a CASE expression in the SET part
UPDATE the_table
SET column_a = CASE
WHEN column_a = '' THEN NULL
ELSE upper(left(column_a,2))||substr(column_a,3)
END;
If you want to treat a value with only spaces as "empty" as well, you can use WHEN trim(column_a) = '' THEN NULL instead.
Related
In this project I'm currently working on (I'm building a bridge between a desktop app and a new e-shop) there is a products table that has some spare columns defined that can be used for whatever reason the end user might need some custom data to be stored into.
So, the user needed to set a true/false flag to determine whether the products would appear in three different sliders... Unfortunately, the person who implemented this, didn't even use the same type of spare columns... So,
Slider1's flag is stored in a varchar(50) column
Slider2's flag is stored in a float column
Slider3's flag is stored in a float column
Additionally I ran a SELECT DISTINCT <column> for each one of them to get an idea of the actual data stored in each column and got the following results:
The varchar column has the following data stored in it:
FLDSTRING1
NULL
''
0
1
194276400456
The float column has the following data stored:
FLDFLOAT5
NULL
0
1
And the other float column has this:
FLDFLOAT6
NULL
1
Also, I ran the following query to find the different combinations of the data stored for each column:
SELECT FLDSTRING1, FLDFLOAT5, FLDFLOAT6
FROM MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6
and got the following combinations...
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
NULL
NULL
NULL
NULL
NULL
1
NULL
0
NULL
NULL
1
NULL
NULL
1
1
''
NULL
NULL
''
NULL
1
0
NULL
NULL
0
0
NULL
1
NULL
NULL
1
NULL
1
1
0
NULL
1
1
NULL
1
1
1
194276400456
0
NULL
What I need after all this introduction...
I want a concatenated string of three comma-separated values like this
NEWPROD for when FLDSTRING1 would evaluate to true - anything not NULL, 0, or ''
CUSTOM1 for when FLDFLOAT5 would evaluate to true - basically the value 1
CUSTOM2 for when FLDFLOAT6 would evaluate to true - again value 1
After some trial and error I managed to bring this to a point that it kind of works, in the sense that it brings the correct values, not comma-separated though...
SELECT
FLDSTRING1, FLDFLOAT5, FLDFLOAT6,
CONCAT(CASE WHEN ISNULL(FLDSTRING1, '') = '' THEN '' ELSE 'NEWPROD' END,
CASE WHEN ISNULL(FLDFLOAT5, '') = '' THEN '' ELSE 'CUSTOM1' END,
CASE WHEN ISNULL(FLDFLOAT6, '') = '' THEN '' ELSE 'CUSTOM2' END) AS TAGS
FROM
MATERIAL
GROUP BY
FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
FLDSTRING1
FLDFLOAT5
FLDFLOAT6
TAGS
NULL
NULL
NULL
NULL
NULL
1
CUSTOM2
NULL
0
NULL
NULL
1
NULL
CUSTOM1
NULL
1
1
CUSTOM1CUSTOM2
''
NULL
NULL
''
NULL
1
CUSTOM2
0
NULL
NULL
NEWPROD
0
0
NULL
NEWPROD
1
NULL
NULL
NEWPROD
1
NULL
1
NEWPRODCUSTOM2
1
0
NULL
NEWPROD
1
1
NULL
NEWPRODCUSTOM1
1
1
1
NEWPRODCUSTOM1CUSTOM2
194276400456
0
NULL
NEWPROD
Problem #1 is I don't quite understand how this works... I mean, value 0 isn't '', but still for the combination of NULL 0 NULL I get an empty value, which is what I wanted... But how does it do that?
And also, can someone update my final query to comma-separate the calculated TAGS column? Problem #2 is that I don't want it to contain just two commas, like ,,, when the combination wouldn't justify any of the three values to appear... It should work like PHP's implode() works...
To help you help me with this, I'm including a fiddle with the setup of the scenario I describe here... Thanks in advance!
Since you are using SQL Server 2014, instead of CONCAT_WS you may try STUFF as shown below. By prepending the delimiter , before all strings ,the STUFF will remove the first comma found.
SELECT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
STUFF(
CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 IN ('0','') THEN '' THEN '' ELSE ',NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL THEN '' ELSE ',CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL THEN '' ELSE ',CUSTOM2' END
),
1,1,''
) AS TAGS
FROM #MATERIAL
GROUP BY FLDSTRING1, FLDFLOAT5, FLDFLOAT6;
View working demo db fiddle
Let me know if this works for you.
Use the CONCAT_WS() function to concat values into a comma (or other separator) separated list, which ignores nulls.
To use CONCAT_WS(), you want to pass it a true NULL if the value is "blank" (by your definition), otherwise your custom label:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
CONCAT_WS(',',
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN NULL ELSE 'NEWPROD' END,
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN NULL ELSE 'CUSTOM1' END,
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN NULL ELSE 'CUSTOM2' END) AS TAGS
FROM MATERIAL
Replaced GROUP BY with DISTINCT because it's simpler and (here) achieves the same thing.
If CONCAT_WS is not available:
SELECT DISTINCT
FLDSTRING1,
FLDFLOAT5,
FLDFLOAT6,
REPLACE(REPLACE(REPLACE(CONCAT(
CASE WHEN FLDSTRING1 IS NULL OR FLDSTRING1 = '' OR FLDSTRING1 = '0' THEN 'X' ELSE 'NEWPROD' END,
',',
CASE WHEN FLDFLOAT5 IS NULL OR FLDFLOAT5 = 0 THEN 'X' ELSE 'CUSTOM1' END,
',',
CASE WHEN FLDFLOAT6 IS NULL OR FLDFLOAT6 = 0 THEN 'X' ELSE 'CUSTOM2' END
), ',X', ''), 'X,', ''), 'X', '') AS TAGS
FROM MATERIAL
See dbfiddle.
I'm writing an sql constraints about values on columns based on other values in other columns for Example:
if Column Replacement Part = 'N/A', the Replacement Company must be = 'N/A' and verse vice .
if type ='obs' the LTB_Date and LTS_Date Must be Not Null and
if type !='obs' the LTB_Date and LTS_Date Must be Null
Try to use a CHECK constraint like this:
ALTER TABLE PCN_Table ADD CONSTRAINT CHECK(
CASE
WHEN Replacement_Part = 'N/A' THEN CASE WHEN Replacement_Company = 'N/A' THEN 1 ELSE 0 END
ELSE 1
END=1
AND
CASE
WHEN [type] = 'obs' AND LTB_Date IS NOT NULL AND LTS_Date IS NOT NULL THEN 1
WHEN [type] <> 'obs' AND LTB_Date IS NULL AND LTS_Date IS NULL THEN 1
ELSE 0
END=1
)
Hi i want to update a table column by concatenating 3 columns value but the problem is how can i stop the update if any of the 3 columns has null value.
my query is given below where i want to update record of ind_ref 123 but if any of the value of the 3 column which i concatenating is null then it shouldn't update the display_name column. How can i achieve it?
update individual_loc
Set individual_loc.display_name=individual_loc.address1+','+INDIVIDUAL_LOCATION.TOWN+','+INDIVIDUAL_LOCATION.COUNTY
where individual_loc.ind_ref=123
Simply add a check for NULL in your WHERE clause.
UPDATE individual_loc
SET display_name = address1 + ',' + TOWN + ',' + COUNTY
WHERE ind_ref = 123 AND address1 IS NOT NULL AND TOWN IS NOT NULL AND COUNTY IS NOT NULL
UPDATE individual_loc
SET individual_loc.display_name = CASE WHEN ISNULL(individual_loc.address1,
'') != ''
AND ISNULL(INDIVIDUAL_LOCATION.TOWN,
'') != ''
AND ISNULL(INDIVIDUAL_LOCATION.COUNTY,
'') != ''
THEN individual_loc.address1 + ','
+ INDIVIDUAL_LOCATION.TOWN
+ ','
+ INDIVIDUAL_LOCATION.COUNTY
ELSE individual_loc.display_name
END
WHERE individual_loc.ind_ref = 123
try this
update individual_loc
Set individual_loc.display_name=individual_loc.address1+','+
individual_loc.TOWN+','+individual_loc.COUNTY
where individual_loc.ind_ref=123
AND address1 is not null
AND town is not null
AND county is not null
Basically, only perform the update if the fields aren't null
One method is with COALESCE:
UPDATE individual_loc
SET individual_loc.display_name=COALESCE(individual_loc.address1+','+INDIVIDUAL_LOCATION.TOWN+','+INDIVIDUAL_LOCATION.COUNTY, individual_loc.display_name)
where individual_loc.ind_ref=123;
You could also check null values by using coalesce() function
update individual_loc
Set individual_loc.display_name=individual_loc.address1+','+INDIVIDUAL_LOCATION.TOWN+','+INDIVIDUAL_LOCATION.COUNTY
where individual_loc.ind_ref=123
and coalesce(individual_loc.address1+','+INDIVIDUAL_LOCATION.TOWN+','+INDIVIDUAL_LOCATION.COUNTY, 0) <> 0
If you are trying to avoid null issues just check for isnull(individual_loc.address1,'')
If its something else try :
IIF((isnull(address1,'')='' OR isnull(TOWN,'')='' OR isnull(COUNTY,'')=''), UPDATE STATEMENT, PRINT 'False')
I need to convert column ID of INT data type to a empty string ['']. I should not modify the source column data type, but need to convert it in my transformation in the other table. The ID column is "nullable" as it has null in it.This is my code.
CREATE TABLE #V(ID INT) ;
INSERT INTO #V
VALUES (1),(2),(3),(4),(5),(NULL),(NULL) ;
SELECT CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
FROM #V;
this returns:
ID_Column
1
2
3
4
5
NULL
NULL
when I modify my CASE statement it as follows:
CASE WHEN CAST(ISNULL(ID,'') AS VARCHAR(10)) = '' THEN '' ELSE ID END AS ID_Column
it returns:
ID_Column
1
2
3
4
5
0
0
Is this what you want?
select coalesce(cast(id as varchar(255)), '')
from #v;
You have to turn the entire result column into a single column. If you want a blank value, then the type is some sort of character string.
In your examples, the else id means that the result from the case is an integer, which is why you are getting either 0 or NULL.
I need to change (mutate) all fields with with value 'N/A' to NULL for all records in a table - is there a quick way to accomplish this ?
You can perform simple UPDATE operation like this.
UPDATE table_name SET string_column = NULL WHERE string_column = 'N/A'
To avoid errors, column must be nullable (i.e don't have 'NOT NULL' ih definition).
This is possible to update all columns by one query like this.
UPDATE table_name SET
col1 = CASE
WHEN col1 = 'N/A' THEN NULL
ELSE col1
END,
col2 = CASE
WHEN col2 = 'N/A' THEN NULL
ELSE col2
END