Can I set condition on language such as:
IF(statement is in Hebrew)
.....
ELSE IF(statement in English)
......
ELSE
......
Or is there other way to achieve the same result?
SQL Server doesn't have any built-in functions for determining what language a string is in.
If you choose to write your own function, then employing it in the IF structure you want is simple:
IF dbo.GetLanguage(statement) = 'Hebrew'
...
ELSE IF dbo.GetLanguage(statement) = 'English'
...
ELSE
Related
I am getting the titled error from a number of SQL Server views I am trying to create. They are modified from a MS Access database I'm upgrading to use SQL as the back end. See SQL below:
SELECT dbo.Site_Info_All.SiteID,
IIf(dbo.Site_Info_All.Fixed_Charge, N'Yes', N'No') AS [Fixed Charge],
dbo.Site_Info_All.Fixed_Charge_Date, dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition INNER JOIN
dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID =
dbo.Site_Info_All.MG_DefinitionID
It looks as though I'm being told that the Fixed_Charge field is not boolean, except that it is. I'm encountering this issue with multiple views. What am I doing wrong?
It's not boolean. It's a bit. :)
A bit column doesn't directly evaluate to true or false, the way a bool does in other languages. You actually have to compare it to another bit value to return the boolean value.
declare #bit bit = 1;
if (#bit) print '#bit was true'; -- this does not work
if (#bit = 1) print '#bit = 1 was true'; -- this works
What you want is:
... IIf(dbo.Site_Info_All.Fixed_Charge = 1, N'Yes', N'No') ...
SELECT dbo.Site_Info_All.SiteID,
IIf(dbo.Site_Info_All.Fixed_Charge = 1, N'Yes', N'No') AS [Fixed Charge],
dbo.Site_Info_All.Fixed_Charge_Date,
dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition
INNER JOIN dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID = dbo.Site_Info_All.MG_DefinitionID
In the end, I decided that IIF simply wasn't working for whatever reason. An answer to that would be very welcome, btw.
I used
SELECT dbo.Site_Info_All.SiteID,
CASE WHEN dbo.Site_Info_All.Fixed_Charge = 1
THEN N'Yes'
ELSE N'No'
END AS [Fixed Charge], dbo.Site_Info_All.Fixed_Charge_Date,
dbo.MG_Definition.MG_Definition
FROM dbo.MG_Definition INNER JOIN
dbo.Site_Info_All ON dbo.MG_Definition.MG_DefinitionID =
dbo.Site_Info_All.MG_DefinitionID
That gave me the results I was after, but only after pulling out what little hair I have left.
+ case when
(
(
PYMT.element like '____.T.T-0_______.____.________' or
PYMT.element like '____.T.T-K_______.____.________'
)
and len(PYMT.element) = 31
)
then ''
else '12|'
end
I'm trying to find a more elegant way of doing this like statement. Is there another way of doing it?
Only thing I can see you could do instead is to replace both LIKEs with a single one:
PYMT.element LIKE '____.T.T-[0K]_______.____.________'
And, as WEI_DBA mentions, you can remove the len(PYMT.element) = 31, as the LIKE handles that already.
Is there 'concat' function in GreenPlum? I can use concat function in postgresql and it works well, but when i use it in Greenplum, I got an error.
select concat('a', 'b');
ERROR: function concat(unknown, unknown) does not exist at character 8
HINT: No function matches the given name and argument types. You may need to add explicit type casts.
LINE 1: select concat('a', 'b');
^
Is there some other functions can instead of 'concat' function in GreenPlum? And I have tried to create a function to instead of it, but got some syntax errors also.
CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ) RETURNS VARCHAR AS $$ SELECT array_to_string(arr, ''); $$ LANGUAGE SQL;
ERROR: syntax error at or near "VARCHAR" at character 51
LINE 1: CREATE OR REPLACE FUNCTION my_concat(VARIADIC arr VARCHAR[] ...
^
Anyone can help? Thanks very much!
Like most databases, Greenplum uses "||" to concatenate two strings together.
SELECT 'Green' || 'plum';
Result:
Greenplum
its a versional issue , you have use || in place where ever u using contact function.
Greenplum doesn't have the concat function yet. May be you can modify your code to use "||" instead of concat.
Well,
First I agree that you should replace your code to use the correct SQL syntax '||' for concatenation.
If you really want to create a function to emulate the concat, you could do something like:
create or replace function myschema.concat(arg1 text, arg2 text)
returns text as
$body$
declare
v_arg1 text;
v_arg2 text;
begin
v_arg1 := arg1;
v_arg2 := arg2;
return v_arg1 || v_arg2;
end
$body$
language plpgsql volatile;
Then, the query will work:
select myschema.concat('test1', 'test2');
>>test1test2
Hope you are looking for the below query.
gpadmin=# CREATE OR REPLACE FUNCTION my_concat( character varying[] ) RETURNS VARCHAR AS $$ SELECT array_to_string($1, ''); $$ LANGUAGE SQL;
gpadmin=# select my_concat(ARRAY['Green','plum']);
my_concat
Greenplum
Some time ago after not standing anymore lines like this:
if (arg)
invk(test);
else if (test)
{
alot();
stuff();
}
I decided for my self its better for readability in our 1920x1200 times, to not omit the {}.
So I wrote a tool that reformats my existing code.
later I noticed a bug in that tool resulting in
if (x)
{
...
}
else if(y)
{
...
}
else if(z)
{
...
}
had been changed (wihtout obvisiously changing the behavior) into:
if (x)
{
...
}
else
{
if(y)
{
...
}
else
{
if(z)
{
...
}
}
}
This made me realize (unintended) that this is actually what a else if does by syntax and semantical rules of C.
So is there even an statement like else if() existing or is it just an abuse of semantic that results in this usefull but (lets call it so for this purpose) obfuscation originated wording that breaks any formating rules and just serves as human readable?
As per the C11, chapter ยง6.8.4, selection statements, the available statement blocks are
if ( expression ) statement
if ( expression ) statement else statement
switch ( expression ) statement
and, regarding the else part,
An else is associated with the lexically nearest preceding if that is allowed by the
syntax.
So, there is no else if construct, exists as per standard C.
Obviously, an if (or if...else) block can exist as the statement in else block (nested if...else, we may say).
The choice of indentation , is left to the user.
Note:
Just to add, there is no reason for a separate else if construct to exist, the functionality can be achieved by nesting. See this wiki article. Relevant quote
The often-encountered else if in the C family of languages, and in COBOL and Haskell, is not a language feature but a set of nested and independent "if then else" statements combined with a particular source code layout. However, this also means that a distinct else-if construct is not really needed in these languages.
As every one is aware, there is several different styles of programming languages. The presence of a elseif is based on the style of language.
In a C/Pascal style the if statement is
if (...)
statment;
else
statement;
and you have a compound statement:
{
statment;
statment;
}
while in a Modula2 / VB / (python ?) style language you have
if (...)
statement;
...
statement;
else
statement;
...
statement;
end-if
in the Modula2 / VB you need a elseif statement because if you tried using else if you get
if (..)
else if (..)
else if (..)
end-if; end-if; end-if;
The end-if's at the end are rather ugly.
As #anatolyg noted this is why you have a #elif in C macro language.
Cobol-85 has a different take on the elseif statement. It provides an extended Select/case statement. In Cobol you have an evaluate:
evaluate option
when 1
...
when 2
...
but you can also do
evaluate true
when age > 21 and gender = 'male'
...
when age > 21 and gender = 'female'
In Cobol you can also mix the case and if structure
evaluate option also true
when 1 also age > 21 and gender = 'male'
....
when 1 also age > 21 and gender = 'female'
....
when 2 also any
....
One final point in java and C you sometimes see
if () {
} else if () {
} else if () {
}
To all intents and purposes, this is VB / Modula-2 written in Java/C.
For sql queries like..
select Quantity_Books/datepart(hour,Rent_Hour) from Rent_Book where (some conditions..)
They will return error when datepart(hour,Rent_Hour) is 0.
If sth like that Happens, I would like to show 0.
I know I should use case when But I am not really sure how..
Or any other better method?
You'd simply test the value first
select
CASE
WHEN datepart(hour,Rent_Hour) = 0 THEN 0
ELSE Quantity_Books/datepart(hour,Rent_Hour)
END
from
Rent_Book where (some conditions..)
Alternatively, use NULL rules
ISNULL((Quantity_Books / (NULLIF(datepart(hour,Rent_Hour), 0))), 0)
select case when datepart(hour,Rent_Hour)<>0 then Quantity_Books/datepart(hour,Rent_Hour) else 0 end as col ...