Sveltekit return me null value on HTML - sveltekit

I get "null" value in my html. I know my business name is null. I cannot implement {{array.business?.name || ""}}. Is there any methods to remove "null" value to empty string?
{array.business?.name}
this is the result after it render the null data

Did you use ts? This is notation from typescript.
I make tests, and for me it works.
<script lang="ts">
const array = {
business: {}
}
</script>
{array.name?.foo} --
{array.business?.foo} --
{array.name?.foo ?? '??'} --
{array.name?.foo || '||'} --
{array.business?.foo ?? 'b??'} --
{array.business?.foo || 'b||'} --
{array.business.foo ?? 'c??'} --
{array.business.foo || 'c||'} --
result:
undefined --
undefined --
?? --
|| --
b?? --
b|| --
c?? --
c|| --

Lol I just found the answers but is this the beast practice to do?
what if I have more than 100 json parameters? do I have to make each of them?
[1] https://svelte.dev/repl/095b7f2439dc42d380741350ea11b20f?version=3.22.3
<div class="section-title">
{#if !array.business?.name}
<h2></h2>
{:else}
<h2>{array.business?.name}</h2>
{/if}
</div>

Related

Snowflake Numeric value '' is not recognized

Please help me with error 'Numeric value '' is not recognized'.
In Snowflake i am having a variable day_minus which holds a number, using this i want to return date value of current_date minus the value given in the variable.
If there is Null or empty passed in the variable i want to get current date minus 1.
For this i have written a code like below. But it throws me an error Numeric value '' is not recognized
But it throws an
set day_minus=7;
SELECT DATEADD(DAY, concat('-',nvl(nullif(try_to_number($day_minus) ,''),1)),current_date() );
Can you please correct me where i am doing mistake
you can use case statement
SELECT DATEADD(DAY, concat('-',nvl(case when $Units ='' then 1 else $Units end ,1)),current_date() );
If you use concat then the result is always going to be a string. If you want to set the "default" value to -1 then just put -1 in your code.
The try_to_* functions take a string as their input so you'd need to make your variable a string to make this work e.g.
set day_minus='7';
You don't need nullif at all. So the final version would be something like:
set day_minus='7';
DATEADD(DAY, nvl(try_to_number($day_minus),-1),current_date() );

How to replace regular expression?

I do replacing
SET #data = REPLACE(#data, 'Riched20 10.0.19041}', '');
It is ok, but recently I have detected that it can be
Riched20 10.0.18362 etc
How can I replace in common case like 'Riched20 ...}'?
Can I use a regular-expression?
It should be implementation in T-SQL
If the problem is as simple as this question makes it out to be then you could, instead, find the position of the string 'Riched20' in your value, and then the position of the first } that appears after it and use STUFF to remove the text in that range.
This assumes that the value will always have a } after 'Riched20', and that if there is a } then 'Riched20' also appears. If this isn't the case you will get the value NULL.
DECLARE #data varchar(100) = 'sdjkafhbgtajl asdgasdf, Riched20 10.0.19041} dlkghbsdfl';
SET #data = STUFF(#data, CHARINDEX('Riched20',#data),CHARINDEX('}',#data,CHARINDEX('Riched20',#data)) - CHARINDEX('Riched20',#data) +1,'');
SELECT #data;
If you need "true" pattern replacement, then you are out of luck; T-SQL does not support this as the comments mention.

how to use array in any() function?

I can run a statement like
select 'a' like any('a',b'), but is it still possible to run this statement if ('a','b') were in an array?
select 'a' like any(array_construct('a','b')) doesn't work.
You can use a JavaScript UDF to approximate this behavior. Rather than trying to get JavaScript to simulate the like syntax, it's probably better to use richer, more expressive Regular Expressions. This UDF will return true if any array member matches that regexp pattern, false if none do, and null if the regex pattern is invalid or it encounters an unexpected error:
create or replace function LIKE_ANY_ARRAY("arr" array, "pattern" string)
returns boolean
language javascript
strict immutable
as
$$
"option strict"
try {
var regex = RegExp(pattern,"g")
for(let i=0; i<arr.length; i++) {
if(regex.test(arr[i])) return true;
}
return false;
} catch {
return null;
}
$$;
select like_any_array(array_construct('a','b'), 'a'); -- True
select like_any_array(array_construct('abcde','b'), '[a-z]{5}'); -- True
select like_any_array(array_construct('abcde','b'), '[a-z]{8}'); -- False, none have enough letters
select like_any_array(array_construct(2,'b'), '[a-z]{8}'); -- Auto-casting
select like_any_array(array_construct('a', 'b'), '['); -- Invalid regex pattern returns NULL
This is returning a Boolean to approximate the behavior of the LIKE ANY function, but could very easily be converted to returning an array of all the matching members or an integer representing the first match, etc.
You could turn you array into a string that resembles a regex and match against that
where 'a' rlike array_to_string( ['a','b'],'|')

SQL: how to do an IF check for data range parameters

I am writing a stored procedure that takes in 4 parameters: confirmation_number, payment_amount, start_range, end_range.
The parameters are optional, so I am doing a check in this fashion for the confirmation_number, and the payment_amount parameters:
IF (#s_Confirmation_Number IS NOT NULL)
SET #SQL = #SQL + ' AND pd.TransactionNumber = #s_Confirmation_Number'
IF (#d_Payment_Amount IS NOT NULL)
SET #SQL = #SQL + ' AND pd.PaymentAmount = #d_Payment_Amount'
I would like to ask for help because I am not sure what is the best method to check for the date range parameters.
If someone could give me en example, or several on how this is best achieved it would be great.
Thank you in advance.
UPDATE - after receiving some great help -.
This is what I have so far, I am following scsimon recommendation, but I am not sure about the dates, I got the idea from another post I found and some playing around with it. Would you care looking at it and tell me what you all think?
Many thanks.
#s_Confirmation_Number NVARCHAR(50) = NULL
, #d_Payment_Amount DECIMAL(18, 2) = NULL
, #d_Start_Range DATE = NULL
, #d_End_Range DATE = NULL
...
....
WHERE
ph.SourceType = #s_Source_Type
AND ((pd.TransConfirmID = #s_Confirmation_Number) OR #s_Confirmation_Number IS NULL)
AND ((pd.PaymentAmount = #d_Payment_Amount) OR #d_Payment_Amount IS NULL)
AND (((NULLIF(#d_Start_Range, '') IS NULL) OR CAST(pd.CreatedDate AS DATE) >= #d_Start_Range)
AND ((NULLIF(#d_End_Range, '') IS NULL) OR CAST(pd.CreatedDate AS DATE) <= #d_End_Range))
(The parameter sourceType is a hard-coded value)
This is called a catch all or kitchen sink query. It is usually written as such:
create procedure myProc
(#Payment_Amount int = null
,#Confirmation_Number = null
,#start_range datetime
,#end_range datetime)
as
select ...
from ...
where
(pd.TransactionNumber = #Confirmation_Number or #Confirmation_Number is null)
and (pd.PaymentAmount = #Payment_Amount or #Payment_Amount is null)
The NULL on the two parameters gives them a default of NULL and makes them "optional". The WHERE clause evaluates this to only return rows where your user input matches the column value, or all rows when no user input was supplied (i.e. parameter IS NULL). You can use this with the date parameters as well. Just pay close attention to your parentheses. They matter a lot here because we are mixing and and or logic.
Aaron Bertrand has blogged extensively on this.
I do it like this
WHERE
COALESCE(#s_Confirmation_Number,pd.TransactionNumber) = pd.TransactionNumber AND
COALESCE(#d_Payment_Amount,pd.PaymentAmount) = pd.PaymentAmount
If we have a value for each of these parameters then it will check against the filter value otherwise it will always match the filter value if the parameter is null.
I've found that using COALESCE is faster and clearer than IF control statements or using OR in the WHERE clause.
There is another way.
But I tested and realized that a scsimon query is faster than mine.
AND (CASE
WHEN #Confirmation_Number is not null
THEN (CASE
WHEN pd.TransactionNumber = #Confirmation_Number
THEN 1
ELSE 0
END)
ELSE 1
END = 1)

How to CAST empty array value of an ANYARRAY-function to ANYARRAY?

I am using pgv10. The function that I need seems this wrong function:
CREATE FUNCTION array_coalesce(ANYARRAY) RETURNS ANYARRAY AS $f$
SELECT CASE WHEN $1 IS NULL THEN array[]::ANYARRAY ELSE $1 END;
$f$ language SQL IMMUTABLE;
Curiosity
... I started to simplify a complex problem, and arrives in the test select coalesce(null::text[], array[]::text[]) that not worked... So it was a good question, how to implement it? But sorry, I do something workng, COALESCE(array,array) is working fine (phew!).
So, "coalesce problem" is merely illustrative/didatic. What I really want to understand here is: How to use ANYARRAY?
PS: other curiosity, the string concat(), || and other concatenation operators in PostgreSQL do some "coalescing",
select concat(NULL::text, 'Hello', NULL::text); -- 'Hello'
select null::text[] || array[]::text[]; -- []
select array[]::text[] || null::text[]; -- []
How to use anyarray?
It's an interesting issue, in the context of the usage described in the question. The only way I know is to use an argument as a variable. It's possible in plpgsql (not in plain sql) function:
create or replace function array_coalesce(anyarray)
returns anyarray as $f$
begin
if $1 is null then
select '{}' into $1;
end if;
return $1;
end
$f$ language plpgsql immutable;
select array_coalesce(null::int[]);
array_coalesce
----------------
{}
(1 row)
By the way, you can simply use coalesce() for arrays:
select coalesce(null::text[], '{}'::text[]);
coalesce
----------
{}
(1 row)

Resources