Replacing a word with a number in SQL Server - sql

I have a table which has 12 columns containing a word, I want to change this to a number. For instance: each of these 12 columns can have one of 5 pre-specified possible values of:
highly agree
agree
no opinion
disagree
highly disagree
I want to change these words with a number, the problem is the data type, it does not allow me to change a nvarchar data type to number, I even tried text data type to contain { highly agree, agree , no opinion , disagree, highly disagree } and then changed them to numbers but this error appeared:
Msg 402, Level 16, State 1, Line 1
The data types text and varchar are incompatible in the equal to operator.
The query I used was this:
[A1]= (case when [A1]='highly agree' then 1
when [A1]='agree' then 4
when [a1]='نظری ندارم' then 9
when [a1]='موافقم' then 16
when [a1]='کاملا موافقم' then 25
else [A1] end )

You dont need to convert your column to text data type, but you have to use your numbers as varchar in your case statement. You cannot mix up data types in the case statement
[A1]= (case when [A1]='highly agree' then '1' when [A1]='agree' then '4' when [a1]='نظری ندارم' then '9' when [a1]='موافقم' then '16' when [a1]='کاملا موافقم' then '25' else [A1] end )
Further to your question, if you want to keep the calculation on this field then you have to cast the whole column to Number. As an example you can use this query
[CalculatedColumn]= CAST(case when [A1]='highly agree' then '1'
when [A1]='agree' then '4'
when [a1]='نظری ندارم' then '9'
when [a1]='موافقم' then '16'
when [a1]='کاملا موافقم' then '25'
else '999' end ) AS INT) -- Any other number which can cast to integer

Try this:
SELECT (CASE A1 WHEN 'highly agree' THEN '1'
WHEN 'agree' THEN '4'
WHEN 'نظری ندارم' THEN '9'
WHEN 'موافقم' THEN '16'
WHEN 'کاملا موافقم' THEN '25'
ELSE A1
END) AS A1

Related

SQL case statement error handling when there are non-integer values

I have a field of 5 digit codes, and I am trying to create a new flag field if the 5 digit code is between 2 numbers. That part is easy, but there are also a lot of values that have letters and aren't strictly 5 digits. So I'm trying to put a statement at the beginning of the case statement that says if there's an error then set the flag to zero. Or a statement that says if the value is not a number then set to zero.
Here's a sample of listed values:
36569
38206
J8502
JAA8C
Here is some code I've tried (simplified to get the point across):
case
when not isnumeric([code]) then 'N'
when [code] between 50000 and 50005 then 'Y'
ELSE 'N'
end as NewFlag
Thanks!
How about using try_cast() or try_convert():
(case when try_convert(int, code) between 50000 and 50005
then 'Y' else 'N'
end) as newFlag
Actually for your particular values, you can do the comparison as strings:
(case when code between '50000' and '50005'
then 'Y' else 'N'
end) as newFlag
This is really a special case, because you have 5 digit codes and you are only concerned about the last character.

Using BETWEEN function

I have this query, using which I am trying to categorize my data. If the first character is between 0 and 9, I want to categorize it with the first character. If it is anything else including special characters or alphabets, then I want to use 10.
select CUSTOMER_ID, CASE
WHEN LEFT(CUSTOMER_ID, 1) BETWEEN 0 AND 9 THEN LEFT(CUSTOMER_ID, 1)
ELSE '10'
END
AS CUST_DIGIT
from CUSTOMER
I get this error when I run the above query:
Conversion failed when converting the nvarchar value 'A' to data type
int.
This is what my data looks like. Could you please help point what I could change.
Update your between values to string as '0' AND '9' then it will work.
Reason you are getting error is when you perform LEFT it will return string and you are comparing it with int as 0 AND 9 are int, So it will try to convert your result value from LEFT to int.
Your some of the record have digit at beginning of value those will work fine but when record comes like A46564 it won't be able to cast A to int and throw error.
SELECT CUSTOMER_ID, CASE
WHEN LEFT(CUSTOMER_ID, 1) BETWEEN '0' AND '9' THEN LEFT(CUSTOMER_ID, 1)
ELSE '10'
END AS CUST_DIGIT
FROM CUSTOMER
I would initially answer the same as #Karan.
Just for completeness... In your case, a possible alternative would be to use ISNUMERIC:
select
CUSTOMER_ID,
CASE
WHEN ISNUMERIC(LEFT(CUSTOMER_ID, 1)) = 1 THEN LEFT(CUSTOMER_ID, 1)
ELSE '10'
END AS CUST_DIGIT
from
CUSTOMER
And yet another approach would be to use the LIKE operator:
select
CUSTOMER_ID,
CASE
WHEN CUSTOMER_ID LIKE '[0-9]%' THEN LEFT(CUSTOMER_ID, 1)
ELSE '10'
END AS CUST_DIGIT
from
CUSTOMER

Error in SQL case statement when trying to create binary flag?

Here's my query where I'm testing my case structure:
SELECT TOP 1 CASE 130
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian
I'm just trying to make a new temporary table, but I'd like my case to work first. I keep getting the error:
Conversion failed when converting the varchar value 'XXX' to data type int.
In the database, the column 130 is a char, and I don't know why it thinks I want to make it a number. SQL server management studio, if it matters.
The column name is 130, I left the '1' off because I rewrote it here but I get the error regardless in my actual query.
130 is an integer literal. If that's really the column name, you'll have to escape it using double quotes. As a side note, you should probably return the same type (char) in the else branch too:
CASE "130"
WHEN '000000000000000' THEN '0'
WHEN '' THEN '0'
WHEN 'XXX' THEN '0'
WHEN 'RETIRED' THEN '0'
WHEN 'STUDENT' THEN '0'
ELSE '1'
END AS employed_flag
130 is a really bad column name. But, I would simplify the logic to:
SELECT TOP 1 (CASE WHEN [130] IN ('000000000000000', '', 'XXX', 'RETIRED', 'STUDENT')
THEN 0 ELSE 1
END) AS employed_flag
INTO #employedbeta
FROM CreditBureau.Experian;
Note that I also changed the employed_flag to a numeric value rather than a string. That makes more sense to me.

REPLACE to just have a number causing conversion failure

I'm trying to do a count to see how many fields in column value are > 10:
SELECT
COUNT(CASE WHEN t.value > 10)
THEN 1
ELSE NULL
END
FROM table t
WHERE t.DATE = '2017-01-01'
However, the column has a few custom entries like +15 or >14.0, so I added the following:
SELECT
COUNT(CASE WHEN value LIKE '>%'
and Replace(value, '>', '') > 10)
FROM table t
WHERE t.DATE = '2017-01-01'
However, after doing that, I get the following error:
Conversion failed when converting the varchar value '>14.0' to data
type int. Warning: Null value is eliminated by an aggregate or other
SET operation.
Seeing I have no access to rewrite the database with an UPDATE, does anyone have a workaround solution?
You could fix this, either by simply changing 10 to 10.0:
SELECT CASE WHEN '14.0' > 10.0 THEN 1 ELSE 0 END
This will cause the implicit conversion of '14.0' to decimal rather than int, which works, or you explicitly convert it:
SELECT CASE WHEN CONVERT(DECIMAL(14, 2), '14.0') > 10 THEN 1 ELSE 0 END
If it were me however, and I was not in a position to update the data, and do something a bit left field, like use a numeric data type to store numbers, I would ignore these values completely, and simply use TRY_CONVERT to avoid the conversion errors:
SELECT
COUNT(CASE WHEN TRY_CONVERT(DECIMAL(14, 2), value) > 10 THEN 1 END)
It is a varchar column, so the possibilities of what nonsense could be in there are endless, you might get a query that works now by replacing > and +, but then what about when someone puts in <, or ends up with a space in between like + 14, or something completely random like 'aaaa', where does it end?
It would be helpful to see the table and sample data, but it sounds like you have strings that are numbers and a sign.
You can cast it to convert the data since you are mixing and matching data types.
SELECT
COUNT(CASE WHEN CAST(value AS VARCHAR(10)) LIKE '>%'
and CAST(Replace(value, '>', '') AS your_num_datatype_here) > 10)

How to conditionally select a column in an Oracle query

I want to do something like:
select (if lookup = 8 then 08 else lookup) lookup
, //more columns
from lookup_table
order by lookup
Unfortunately, Oracle doesn't seem to like this syntax, and I am failing to discover why or find what other function I should be using.
Basically, if the lookup value is 8, I want to get 08, otherwise I want the value of lookup. I'm sure I'm just being stupid.
You want a case statement:
select (case when lookup = 8 then 8 else lookup end) as lookup
If lookup is a character string, you probably want:
select (case when lookup = '08' then '08' else lookup end) as lookup
If lookup is an integer and you want to convert it to a string, then:
select (case when lookup = 8 then to_char(lookup, '00') else to_char(lookup, '00') end) as lookup
However, that would seem redundant to me.