I mean I can't query null values when column type sql_variant
For example docsdate table look like this:
ValID DocID Value <--sql variant column)
1. 488 146 30.10.2007
2. 740 190 31.03.2008
3. 570 161 31.10.2008
4. 242 103 NULL
5. 248 104 NULL
When query like select * from docsdate where value is null
no rows returned
Any idea?
This works fine for me. Are you sure your NULL values are actually proper NULLs and not strings containing the text NULL? Do they show up with a yellow background in SSMS?
create table #docsdate
(
ValID int,
DocID int,
value sql_variant
)
INSERT INTO #docsdate
SELECT 488,146,'30.10.2007' UNION ALL
SELECT 740,190,'31.03.2008' UNION ALL
SELECT 570,161,'31.10.2008' UNION ALL
SELECT 242,103,NULL UNION ALL
SELECT 248,104,NULL
SELECT * FROM #docsdate
WHERE value IS NULL
DROP TABLE #docsdate
Related
I have 2 columns of type int
and I want to make a concat to two columns in presto syntax. it is possible?
for example:
id:
345
997
age:
23
55
new_col:
34523
99755
I was trying to use the array function but it is not working :/
thanks to everyone!
As presto can not convert automatically
CONCAT(cast(id as varchar),cast(age as varchar))
You concatenate strings.
You calculate with integers.
So, multiply one column by 100, and add the other. And the result is another integer, not a string.
Data Types matter. And integers are way faster than strings.
WITH
-- your input, one table, don't use in final query ...
id(id,idcol) AS (
SELECT 1, 345
UNION ALL SELECT 2, 997
)
,
-- your input, other table, don't use in final query ...
age(id,agecol) AS (
SELECT 1, 23
UNION ALL SELECT 2, 55
)
-- real query starts here ...
SELECT
id.id
, idcol * 100 + agecol AS new_col
FROM id JOIN age USING(id)
ORDER BY 1
-- out id | new_col
-- out ----+---------
-- out 1 | 34523
-- out 2 | 99755
I'm having a hard time solving my sql problem. Would like to select a row if a certain field(LOOKUP) is not null. Else, get the row with the null field instead. Please see table below:
(PAYCODE must be unique)
PAYCODE
LOOKUP
ACCOUNT
201
null
720001
201
659057
999999
202
null
720002
The output must be:
PAYCODE
LOOKUP
ACCOUNT
201
659057
999999
202
null
720002
This looks so easy but I am new to sql and solving this for 2 days while searching for solutions but no luck.
You could try the following logic:
SELECT *
FROM yourTable t1
WHERE LOOKUP IS NOT NULL OR
NOT EXISTS (SELECT 1 FROM yourTable t2
WHERE t2.PAYCODE = t1.PAYCODE AND
t2.LOOKUP IS NOT NULL);
Demo
This logic retains any record whose LOOKUP is not null or any record for which there is no non null record having the same PAYCODE.
I have a simple column with some numbers, which looks like;
133
8
55
11
NULL
NULL
235
NULL
I want to put a default in when selecting this column, with the aim of replacing the nulls. I've done;
CASE
WHEN round(avg(s.seconds)) IS NULL THEN 0
ELSE round(avg(s.seconds))
END as 'seconds'
However, my nulls remain. My expectation is that they would be set to 0. Why would this not work?
NULL values are ignored in average calculations.
If you want to replace NULL values for 0 in your average, you need to use COALESCE on the values, inside the average function. Example follows:
DROP TEMPORARY TABLE IF EXISTS TEMP1;
CREATE TEMPORARY TABLE TEMP1
SELECT 1 AS price
UNION ALL SELECT NULL AS price
UNION ALL SELECT NULL AS price
UNION ALL SELECT 3 AS price;
SELECT * FROM TEMP1; # List values (1, NULL, NULL, 3)
SELECT AVG(price) FROM TEMP1; # Returns 2 (NULL values IGNORED)
SELECT AVG(COALESCE(price,0)) FROM TEMP1; # Returns 1 (NULL values REPLACED with 0)
You can do it by 3 ways
1
round(avg(CASE
WHEN s.seconds IS NULL THEN 0
ELSE s.seconds
END)) as 'seconds'
2
The MySQL IFNULL() function lets you return an alternative value if an
expression is NULL:
round(avg(IFNULL(s.seconds,0)))
3
or we can use the COALESCE() function, like this:
round(avg(COALESCE(s.seconds,0)))
I have a some entries in database table rows as follows.
101 - 1
101 - 2
101 - 3
102 - 1
102 - 2
102 - 3
103
I need to get the result of SELECT Query for count as '3' since there are 101 and 102 are the only number before the -.
So is there any way to find the unique value in db table columns before a character?
EDIT : I have entries even without the - .
In case your entries have always the format you have provided us, you just have to find the position of the '-' character, split the values, get the first n characters and count the distinct values
This works for SQL Server, otherwise informs us about what DBMS you are using or replace the functions with the ones of your DBMS on your own
SELECT COUNT(DISTINCT SUBSTRING(val,0,CHARINDEX('-', val))) from YourTable
create table T1
(
id int primary key identity,
col1 varchar(20)
)
insert into T1 values('101 - 1'),('101 - 2'),('101 - 3'),('102 - 1'),('102 - 2'),('102 - 3')
select SUBSTRING(col1,0,CHARINDEX(' ',col1)) as 'Value',count(*) as 'Count' from T1 group by SUBSTRING(col1,0,CHARINDEX(' ',col1))
Below is my database schema.
id from to value
1 1 10 5
2 11 NULL 10 -- I have stored 'NULL' for any value GREATER THAN 11
Now i have to select value where input like (4) is between from and to.
I know that it can be achieved with this query.
SELECT *
FROM TABLE
WHERE 4 BETWEEN `from` AND `to`
But how to select value where input is like 15?
SELECT *
FROM TABLE
WHERE 15 BETWEEN `from` AND `to`
In this case above query will not work because to column has null.
UPDATE
columen to will contain null if it can have any value.
Now If input value is 15, then query should return 2nd row, because 15 is not between 1 and 10 in 1st row and 15 is greater than 11 and to value can be anything greater than 11.
SELECT *
FROM TABLE
WHERE 15 BETWEEN coalesce(`from`,15) AND coalesce(`to`,15);
Use boolean combinations
select *
from table
where (to is not null and 15 between from and to)
or (to is null and 15 >= from)
SELECT *
FROM TABLE
WHERE 15 BETWEEN `from` AND IF_NULL(`to`,15)