if else
-
does sql has if else statement like c++, vb..? I have following statement
SELECT [k1] & ';' & [k2] & ';' & [k3] AS AllK FROM TEST;
That Query Statement combine strings of k1,k2,and k3 as k1;k2;k3 and insert it into AllK. However when K1 field has nothing in it, then the result is ;k2;k3 again, when k2 field is emtpy then the result is k1;;k3 so how can i detect whether the field is empty or not to get the result as k1;k3 but not k1;;k3??? Thank you -
does sql has if else statement like c++, vb..? I have following statement
SELECT [k1] & ';' & [k2] & ';' & [k3] AS AllK FROM TEST;
That Query Statement combine strings of k1,k2,and k3 as k1;k2;k3 and insert it into AllK. However when K1 field has nothing in it, then the result is ;k2;k3 again, when k2 field is emtpy then the result is k1;;k3 so how can i detect whether the field is empty or not to get the result as k1;k3 but not k1;;k3??? Thank you -
does sql has if else statement like c++, vb..? I have following statement
SELECT [k1] & ';' & [k2] & ';' & [k3] AS AllK FROM TEST;
That Query Statement combine strings of k1,k2,and k3 as k1;k2;k3 and insert it into AllK. However when K1 field has nothing in it, then the result is ;k2;k3 again, when k2 field is emtpy then the result is k1;;k3 so how can i detect whether the field is empty or not to get the result as k1;k3 but not k1;;k3??? Thank youTry using
CASE
. Best regards, Alexandru Savescu P.S. Interested in art? Visit this! -
does sql has if else statement like c++, vb..? I have following statement
SELECT [k1] & ';' & [k2] & ';' & [k3] AS AllK FROM TEST;
That Query Statement combine strings of k1,k2,and k3 as k1;k2;k3 and insert it into AllK. However when K1 field has nothing in it, then the result is ;k2;k3 again, when k2 field is emtpy then the result is k1;;k3 so how can i detect whether the field is empty or not to get the result as k1;k3 but not k1;;k3??? Thank youWrite Query like this-
Select ISNULL([K1] + ';', '') + ISNULL([K2] + ';', '') + ISNULL([K3] + ';', '') AS AllK FROM TEST
Ofcourse I am using the property of SQL that sum of a NULL string with another string is NULL. So if any of the K1, K2 or K3 field is NULL, [K1] + ';' will give NULL. HTH. Gaurav