Hi! I am new to SQl. My programming background is in SAS. I am currently using SQL Server Management Studio 18.5. I am trying to determine how to solve a couple of problems:
(1) I want to assign new variables to existing columns, depending on the value of those columns. I have read about declaring variables using set and select, but I am having trouble translating that information to what I need to do. I have a view which contains several thousand records. I want the variable logic applied to each record in the view. The desired result is that I create a new view with just the new variable name and other associated data, excluding the original column names.
In SAS, my code would look like:
If newctry<>' ' then countryname=newctry; else countryname=mailingcountry;
Both newctry and mailingcountry are columns in the view I am pulling data from.
I have different instances of this type of situation. Do I need to create subqueries for each of these instances? If someone could provide an example of how this might work in SQL, I would really appreciate it.
An example of the SQL code I have tried unsuccessfully is:
Declare @countryname VARCHAR(50)
Select recordid, recorddate, newctry, mailingcountry
Select @countryname=newctry from viewname where newctry<>' '
Select @countryname=mailingcountry from viewname where newctry=' '
(2) The other question I have is that I have received errors when I have included multiple CASE statements with different END names in in Select statement. Do I need subqueries for each CASE statement?