Convert value ' ' to null in SQL or BigQuery - sql

I have the following extraction query
select A.documentid.Docid, A.documentId.Appid, A.timestamp, A.EventStatus, D.Sequence, D.EventAppName, E.Value as Federation
from `dbo.events` A left join `dam.eventsroot` B on A.documentid.docid = B.docid left join `dbo.documentroot` C on B.rootdocid=C.rootdocid
inner join `dbo.reference_status` D
on A.DocumentID.AppID=D.EventAppID and A.EventStatus = D.EventStatus left join unnest(C.metadata) E on E.Key='Federation'
where A.timestamp > TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(),DAY)
The result is that in column FEDERATION i get different values and values of '' (nothing) and i want to change them to Null.
How can i do this?
Thank you very much

I could use this in simple way like
select A.documentid.Docid, A.documentId.Appid, A.timestamp, A.EventStatus,
D.Sequence, D.EventAppName,
Case
When LTrim(RTrim(E.Value)) = '' Then Null
Else E.Value
End AS Federation
from dbo.events A
left join dam.eventsroot B on A.documentid.docid = B.docid
left join dbo.documentroot C on B.rootdocid=C.rootdocid
inner join dbo.reference_status D on A.DocumentID.AppID=D.EventAppID and
A.EventStatus = D.EventStatus
left join unnest(C.metadata) E on E.Key='Federation'
where A.timestamp > TIMESTAMP_TRUNC(CURRENT_TIMESTAMP(),DAY)

there are functions like NULLIF(column , '') and REPLACE(column,'',NULL) which you can them in such this cases. u can use LTRIM(RTRIM(column)) so any extra spaces will be removed.
so like:
NULLIF(LTRIM(RTRIM(federation)), '')
REPLACE(LTRIM(RTRIM(federation)),'',NULL)
and so many other ways...

You can use nullif() in any database:
select nullif(federation, '')
You might want to remove white space in general, and do:
select nullif(trim(federation), '')
Or, perhaps:
select (case when trim(federation) <> '' then federation end)

Related

Why am I getting invalid column name error in group by?

I am trying to make this request. I added the group by because I had duplicates. I am getting the error:
Invalid column name for the columns in the group by.
I have read a lot of posts regarding this type of error but I am still stuck. Also I would like to know if I should use sum(). Help please.
INSERT INTO tabA
([load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd])
select 1 [load_number]
,getdate() [load_date]
,'' [no_command]
,'' [no_document]
,coalesce(c.id_numb,-1) [id_transc]
,coalesce(b.elem_cd,-1) [division_cd]
,coalesce(d.budget_cd,-1) [activity_cd]
,'' [project_cd]
from tabB b
left join tabC c on c.credit = b.account
left join tabD d on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010'
group by [load_number]
,[load_date]
,[no_command]
,[no_document]
,[id_transc]
,[division_cd]
,[activity_cd]
,[project_cd]
If you are concerned about duplicates, you can just use select distinct:
select distinct 1 [load_number],
getdate() [load_date],
'' [no_command],
'' [no_document],
coalesce(c.id_numb,-1) [id_transc],
coalesce(b.elem_cd,-1) [division_cd],
coalesce(d.budget_cd,-1) [activity_cd],
'' [project_cd]
from tabB b left join
tabC c
on c.credit = b.account left join
tabD d
on d.activity = SUBSTRING([b.name],CHARINDEX('-',[b.name])+1,LEN([b.name])) and d.transc = '1010';
You can't use column aliases defined in the select in the group by in SQL Server.

Case statement in multiple conditions?

I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no. How? Can use if else or case?
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select
mr.name as mr_no,
coalesce(mr.user_id,0) as mr_user
from stock_production_lot lot
left join kg_grn grn on (grn.name = lot.grn_no)
left join kg_department_indent mr on (mr.name = grn.mr_no)
order by mr.user_id) main
where mr_user=65
When i use like this
case when mr.name is null then '' else mr.ticket_no = grn.mr_no as mr_no
it will throw an error
if mr.name = null means i have to replace mr.name = mr.ticket_no. I want to check the column mr.name, if mr.name is null then i have to replace mr.name as mr.ticket_no
You can use coalesce() to replace null with anything:
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select
coalesce(mr.name,mr.ticket_no) as mr_no,
coalesce(mr.user_id,0) as mr_user
from stock_production_lot lot
left join kg_grn grn on (grn.name = lot.grn_no)
left join kg_department_indent mr on (mr.name = grn.mr_no)
order by mr.user_id) main
where mr_user=65
But if you are comfortable with case when then use as below:
select ROW_NUMBER() OVER(ORDER BY mr_user) sl_no,* from (select
(case when mr.name is null then mr.ticket_no else mr.name end) as mr_no,
coalesce(mr.user_id,0) as mr_user
from stock_production_lot lot
left join kg_grn grn on (grn.name = lot.grn_no)
left join kg_department_indent mr on (mr.name = grn.mr_no)
order by mr.user_id) main
where mr_user=65

Compare two columns with one coalesce function

I tried this query but it doesn't work, ORA-00904 error.
SELECT
C.BUSINESS_UNIT
,COALESCE(L.CHILD_CONTR_ID
,L.PARENT_CONTR_ID) AS CONTRAT
, C.BILL_TO_CUST_ID
FROM C_LIE_VW L
,CONTR_HDR C
WHERE 1=1
AND CONTRAT=C.CONTRACT_NUM
AND (L.PARENT_CONTR_ID <> ' '
OR L.CHILD_CONTR_ID IS NOT NULL)
It seems I can't do this: AND CONTRAT=C.CONTRACT_NUM
How can I compare the coalesce column with another column?
Please use below query, you cannot use alias to join a value, instead you have to use complete expression. And you no need to use 1=1 condition as well.
SELECT
C.BUSINESS_UNIT
,COALESCE(L.CHILD_CONTR_ID
,L.PARENT_CONTR_ID) AS CONTRAT
, C.BILL_TO_CUST_ID
FROM C_LIE_VW L
,CONTR_HDR C
WHERE COALESCE(L.CHILD_CONTR_ID,L.PARENT_CONTR_ID)=C.CONTRACT_NUM
AND (L.PARENT_CONTR_ID <> '' OR L.CHILD_CONTR_ID IS NOT NULL);
Below is the join query if required,
SELECT
C.BUSINESS_UNIT
,COALESCE(L.CHILD_CONTR_ID
,L.PARENT_CONTR_ID) AS CONTRAT
, C.BILL_TO_CUST_ID
FROM C_LIE_VW L inner join
CONTR_HDR C
on (COALESCE(L.CHILD_CONTR_ID,L.PARENT_CONTR_ID)=C.CONTRACT_NUM )
WHERE (L.PARENT_CONTR_ID <> ' '
OR L.CHILD_CONTR_ID IS NOT NULL);

Oracle SQL - Case statement with iteration

The ASCII table values should be compared to the s.manure_type. For each record in the following table below the QuantityText case statement should do a comparison. The value it needs to select is e.g. oats,velvet beans, other none.
select
c.id customer_num,
c.type type,
s.id_text sample_num,
c.sasa_grower_code s_grower,
c.address s_address1,
c.postalcode s_post_code,
c.email q1_email,
nvl(c.client_name, c.farm_name )s_company,
c.farm_name s_estate,
c.contact_name s_contact,
s.id_numeric id_numeric,
s.id_text fas_lab_id,
s.date_received received_date,
s.date_printed printed_date,
s.sampled_date sampled_date,
e.name S_AREA_DESCRIP,
a.name s_advisor_name,
a.email s_advisor_email,
s.order_no s_order_num,
s.field_name s_field,
p.phrase_text || ' cm' sample_depth,
cr.crop_name s_crop,
s.attyield s_yield,
s.variety s_varty,
case when s.flg_trashed is null then
'None'
else (case when s.flg_trashed = constant_pkg.get_true then
'Yes'
else (case when s.flg_trashed = constant_pkg.get_false then
'No'
else ' '
end)
end) end trashed,
case when s.flg_irrigated is null then
'None'
else (case when s.flg_irrigated = constant_pkg.get_true then
'Yes'
else (case when s.flg_irrigated = constant_pkg.get_false then
'No'
else ' '
end)
end) end s_irrig,
CASE
WHEN trim(s.manure_type) in (select p.phrase_id from phrase p where p.phrase_type = 'AL_G_MANUR') then (select p.phrase_text from phrase p)
END AS QuantityText,
'' S_GM_YIELD,
s.project_code project_code,
s.trial_ref trial_ref,
s.cost_centre cost_centre
from client c
left outer join sample s on (s.client_id = c.id)
left outer join extension e on (e.id = c.extension_id)
left outer join advisor a on (a.id = c.advisor_id)
left outer join phrase p on (p.phrase_id = s.depth)
left outer join crop cr on (cr.id = s.crop_id)
where p.phrase_type = phrase_pkg.get_soil_depth
and c.id = '211493A'
and s.fas_sample_type = sample_pkg.get_soil_sample
and s.flg_recommendation = sample_pkg.get_flg_recommendation
and s.id_numeric between 14932 and 14933
+----------------------------+
| Phrase |
+----------------------------+
|AL_G_MANUR OA Oats |
|AL_G_MANUR V Velvet Beans
|AL_G_MANUR O Other
|AL_G_MANUR N None |
+----------------------------+
But I get the error ORA-00900: Single row query returns more than one row
Missing where clause in one of the case statements is most likely the cause.
CASE
WHEN trim(s.manure_type) in
(select p.phrase_id from phrase p where p.phrase_type = 'AL_G_MANUR')
then (select p.phrase_text from phrase p) <<< NO WHERE CLAUSE ?
END AS QuantityText,
This is relatively easy to debug yourself.
Remove a single selected column
Check if error still occurs
If it does, go back to 1.
If it does not then verify why last added column errors
Problem is in the following statement
then (select p.phrase_text from phrase p)
I guess, It should be replaced with this
(select p.phrase_text from phrase p where p.phrase_type = 'AL_G_MANUR')

Case statement on a join in where clause

I am trying to use a CASE statement inside a WHERE clause for joining 2 tables. I want to evaluate POSITION_NBR2, if it is blank (null) then I want to join A.POSITION_NBR2 = B.POSITION_NBR Else I want to join A.POSITION_NBR = B.POSITION_NBR , however I am getting syntax errors while trying to run the following:
UPDATE #WORKTABLE
SET SLT_MEMBER = B.NAME
FROM PS_GHS_FTE_SLT A,
PS_EMPLOYEES B,
#WORKTABLE C
WHERE
CASE WHEN A.POSITION_NBR2 <> '' THEN A.POSITION_NBR2 = B.POSITION_NBR
ELSE A.POSITION_NBR = B.POSITION_NBR
END
AND A.DEPTID COLLATE Latin1_General_BIN = C.DEPTID COLLATE Latin1_General_BIN
AND B.EMPL_STATUS = 'A'
Use proper JOIN syntax and reasonable table aliases:
UPDATE WT
SET SLT_MEMBER = E.NAME
FROM PS_GHS_FTE_SLT GFS JOIN
PS_EMPLOYEES E
ON E.POSITION_NBR = (CASE WHEN GFS.POSITION_NBR2 <> '' THEN GFS.POSITION_NBR2 ELSE GFS.POSITION_NBR
END) JOIN
#WORKTABLE WT
ON GFS.DEPTID COLLATE Latin1_General_BIN = WT.DEPTID COLLATE Latin1_General_BIN
WHERE E.EMPL_STATUS = 'A';
I'm generally not a fan of using CASE for ON and WHERE clauses. In this case, though, it is only selectively choosing one column, so it doesn't bother me so much.