Convert INT column values to an empty string using ISNULL - sql

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.

Related

Coalesce in where clause with n

I am trying to get records based on the given input values. Below is the sample script
DECLARE input1 = '001'
DECLARE input2 = '002'
SELECT * FROM table WHERE COLUMN1 = COALESCE(input, NULL) OR
COLUMN2 = COALESCE(input2, NULL)// return non-null records, Great
DECLARE input1 = NULL
DECLARE input2 = NULL
SELECT * FROM table WHERE COLUMN = COALESCE(input, NULL) // return no records, Problem here
I know, COLUMN = NULL do not yield any values. Is there a better way, so that NULL input values return null records. Thanks in advance.
A "better" option might be to skip COALESCE (or NVL) and switch to
where column = input
or (column is null and input is null)
Your query:
SELECT *
FROM table
WHERE COLUMN1 = COALESCE(input, NULL)
OR COLUMN2 = COALESCE(input2, NULL)
Will only return results when COLUMN1 = input OR column2 = input. It will not return results when both columns are NULL.
You will get exactly the same results if you remove the COALESCE expressions:
SELECT *
FROM table
WHERE COLUMN1 = input
OR COLUMN2 = input2
If you want to check when the input is NULL and the column value is also NULL then you need to use IS to compare the values to NULL and not = like this:
SELECT *
FROM table
WHERE COLUMN1 = input -- Compare non-NULL values
OR ( input IS NULL AND COLUMN1 IS NULL ) -- Compare NULL values
OR COLUMN2 = input2 -- Compare non-NULL values
OR ( input2 IS NULL AND COLUMN2 IS NULL ) -- Compare NULL values
If you need to return rows with COLUMN1 is NULL in case of INPUT is null:
SELECT *
FROM table
WHERE
decode(COLUMN1,input, 1,0) = 1
Decode: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/DECODE.html#GUID-39341D91-3442-4730-BD34-D3CF5D4701CE
In a DECODE function, Oracle considers two nulls to be equivalent
You do not need coalesce in this query from your question:
SELECT *
FROM table
WHERE COLUMN1 = COALESCE(input, NULL) OR
COLUMN2 = COALESCE(input2, NULL)
because coalesce(input,null) is equal to simple input: coalesce returns first non-null parameter, but your second parameter is null, so it returns
input is not NULL | input
-------------------------
input is NULL | null

use cast in SQL case statement

SELECT
CASE
WHEN column1 = 43 THEN CAST('abcde' as varchar(30))
WHEN column1 = 44 THEN CAST('fghij' as varchar(30))
ELSE 'N/A'
END AS value
Is the above case and cast statement correct? I need to convert the int to a varchar. column1 is a value that comes from another table.
Question 2: I have two values coming from two different tables (table1 and table2) and they need to go to the final table data type varchar
value 1 = CONVERT(VARCHAR(10),CAST(c.table1 * 100 AS DECIMAL(5,2))) + ' %' AS value1
value 2 = CONVERT(VARCHAR(10),c.table2,101) AS value2
c.table1 is decimal coming from table1
c.table2 is int coming from table2 and both need to be varchar at the final table
how do I combine both statements so that it can show up in the final table, its not an OR its an AND.
result set:

How to select all items where LEN > 0

I'm new to Google BigQuery, and I'm finding that the SQL really quite different from normal SQL. I'm trying to select all items from a table that has data in a field named 'ticket_fields'. I son't want nulls. I tried the following:
WHERE COLUMN <> ''
WHERE LEN(COLUMN) > 0
WHERE NULLIF(LTRIM(RTRIM(COLUMN)), '') IS NOT NULL
None of that worked. How can I select all records where there are non-nulls from one specific field?
In some systems, the empty string and NULL are treated equivalently (Oracle, for example). In BigQuery, these are distinct values, so:
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns NULL
SELECT LENGTH(CAST(NULL AS STRING)) > 0
-- Returns TRUE
SELECT CAST(NULL AS STRING) IS NULL
-- Returns FALSE
SELECT '' IS NULL
-- Returns FALSE
SELECT LENGTH('') > 0
-- Returns TRUE
SELECT '' IS NOT NULL
If you want to filter rows where the column is NULL, then use IS NOT NULL:
SELECT * FROM dataset.table WHERE column IS NOT NULL
If you want to filter rows where the column is empty or NULL, you can just check that the length is positive:
SELECT * FROM dataset.table WHERE LENGTH(column) > 0
This is because LENGTH(column) returns NULL if column is null, so the WHERE clause excludes the row.

Dynamic CASE expression [duplicate]

This question already has answers here:
Check if a varchar is a number (T-SQL)
(12 answers)
Closed 5 years ago.
Is there anyway to have a case expression that produces different results based upon a value being an integer or a character.
Tables
ID CODE
1 ABC
2 123
3 YHU
4 456
5 ikl
I was looking for an expression that separated the int and char.
Result e.g.
ID CODE Category
1 ABC Char
2 123 Int
3 YHU Char
4 456 Int
5 ikl Char
my general logic
CASE WHEN CODE = INT THEN 'Int' Else 'Char' end as Category
But i didnt know if this was possible in SQL?
I am looking mainly for a way to recognise whether its int or char
UPDATE:
What is the best way to separate the numbers from char, * and - into 2 different categories using case expression
ID CODE Category
1 * No_NUM
2 123 NUM
3 YHU No_NUM
4 456 NUM
5 ikl No_NUM
6 - No_NUM
You can use SQL ISNUMERIC function.
SELECT ID, CODE, CASE ISNUMERIC(CODE) WHEN 1 THEN 'NUM' ELSE 'No_NUM' END AS Category FROM my_table;
Another Variation with REGEX
SELECT ID, CODE, CASE WHEN CODE LIKE '%[0-9]%' THEN 'NUM' ELSE 'No_NUM' END AS Category FROM my_table;
You could use TRY_CAST (SQL Server 2012+)
SELECT *,
CASE WHEN TRY_CAST(CODE AS INT) IS NOT NULL THEN 'Int' ELSE 'Char' END
FROM tab;
I've assumed that column is NOT NULL.
Rextester Demo
EDIT:
It is just text inside CASE:
SELECT *,
CASE WHEN TRY_CAST(CODE AS INT) IS NOT NULL THEN 'NUM' ELSE 'No_NUM' END
FROM tab;
Rextester Demo 2
Use PATINDEX:
create table #temp ([ID] int, [CODE] nvarchar(5))
insert into #temp values (1, '*')
insert into #temp values (2, '123')
insert into #temp values (3, 'YHU')
insert into #temp values (4, '456')
insert into #temp values (5, 'ikl')
Select ID
, CASE when PATINDEX('%[0-9]%', [code]) = 1 then 'num'
-- when PATINDEX('%[^0-9]%', [code]) = 1 then 'no_num'
-- when PATINDEX('%[A-Z]%', [code]) = 1 then 'char'
-- when PATINDEX('%[^A-Z]%', [code]) = 1 then 'no_char' /*etc..*/
ELSE 'no_num' END AS 'Category'
from #temp

How do I count the occurrences of NOT NULL fields in a record

I am trying to count the number of times a record field has a value so that I can report that number later in the application.
I am finding several answers with various approaches using COUNT and GROUP BY but the results are all sums of the total occurrences for the entire table.
I am trying to limit the count to each record.
Table Example:
COL-1 COL-2 COL-3 COL-4
VALUE VALUE
VALUE VALUE
VALUE VALUE VALUE
VALUE
I need to count the fields of each record for the number of times a value appears.
Something similar to:
Result Concept:
COL-1 COL-2 COL-3 COL-4 Occurrences
VALUE VALUE 2
VALUE VALUE 2
VALUE VALUE VALUE 3
VALUE 1
Clarification:
I do not actually need to list the columns and values in the result. I only need the accurate count for each record.
I just wanted to illustrate the relationship between the "occurrences-value" and the record values in my question.
Thank you for all suggestions and input.
Just use case:
select t.*,
( (case when col1 is not null then 1 else 0 end) +
(case when col2 is not null then 1 else 0 end) +
(case when col3 is not null then 1 else 0 end) +
(case when col4 is not null then 1 else 0 end)
) as occurrences
from t;
Or decode ;)
select t.*, DECODE(col1,null,0,1)+DECODE(col2,null,0,1)+
DECODE(col3,null,0,1)+DECODE(col4,null,0,1) cnt
from my_table t
You may use the dynamic SQL without listing all column names
DECLARE #sql VARCHAR(MAX)
DECLARE #tbl VARCHAR(100)
SET #tbl = 'sampletable' -- put your table name here
SET #sql = 'SELECT *, '
SELECT #sql = #sql + '(CASE WHEN ' + cols.name + ' IS NOT NULL THEN 1 ELSE 0 END) ' + '+'
FROM sys.columns cols
WHERE cols.object_id = object_id(#tbl);
SET #sql = LEFT(#sql, LEN(#sql) - 1)
SET #sql = #sql + ' AS occurrences FROM ' + #tbl
EXEC(#sql)