I have a case expression in a DB2 statement like the following.
SELECT A,
CASE WHEN B LIKE ' %'
THEN C
ELSE B
END CASE,
D
FROM TAB
I'd like to name the column that the case expression results in, but I get a syntax error with both the AS immediately following END CASE and by wrapping the entire expression in parentheses and following that with an AS.
Adding the AS (without the parens) results in the following error
199: SQL0199N The use of the reserved word "AS" following "" is not valid. Expected tokens may include: ", FROM INTO". SQLSTATE=42601
How can I name this column?
The ending keyword is not END CASE, it's just END:
SELECT A,
CASE WHEN B LIKE ' %'
THEN C
ELSE B
END AS D --- the AS is optional
FROM TAB
Here's the documentation for CASE expression in DB2 - although it's not needed really, that's standard SQL.
Related
I have went through some documentations and as it states SELECT TRIM('') FROM something; can be used to trim strings.
I have SQL command to get the table:
"select NRO,SNAME,NAMEA,NAMEB,ADDRESS,POSTS,POSTN,POSTTP,COMPANY,COUNTRY,BID from COMPANY where ACTIVE = '1' AND NRO Like '7%'"
So I have tried to use:
"select TRIM(NRO),TRIM(SNAME),TRIM(NAMEA),TRIM(NAMEB),TRIM(ADDRESS),TRIM(POSTS),TRIM(POSTN),TRIM(POSTTP),TRIM(COMPANY),TRIM(COUNTRY),TRIM(BID) from COMPANY where ACTIVE = '1' AND NRO Like '7%'"
but this is throwing an error. What is the proper way of using TRIM in this case?
I need to remove spaces from left and right but leave them in between if there are any.
Error message:
Client Interface][LNA][PSQL][SQL Engine]Error in expression: TRIM (
NRO ) ERROR [HY000] [PSQL][ODBC Client Interface][LNA][PSQL][SQL
Engine][Data Record Manager]Invalid user-defined or scalar function.'
You can use LTRIM and RTRIM functions which removes starting and ending spaces for the SQL Server prior to 2017.
So, your query becomes:
select RTRIM(LTRIM(NRO)),
RTRIM(LTRIM(SNAME),
RTRIM(LTRIM(NAMEA)),
RTRIM(LTRIM(NAMEB)),
RTRIM(LTRIM(ADDRESS)),
RTRIM(LTRIM(POSTS)),
RTRIM(LTRIM(POSTN)),
RTRIM(LTRIM(POSTTP)),
RTRIM(LTRIM(COMPANY)),
RTRIM(LTRIM(COUNTRY)),
RTRIM(LTRIM(BID))
from COMPANY where ACTIVE = '1' AND NRO Like '7%'
For the SQL Server 2017 and later you can use TRIM function as Dale K suggested.
I am attempting to generate a new field based on an existing field where some of the entries contain different special characters. (ie. *, ') The special characters are at the end of the string, which is either second or third position.
I am NEW to SQL but not new to data. I am using a CASE WHEN statement. I have tried several approaches and several other commands within the CASE statement.
what I want is:
SELECT *
CASE WHEN grde_code_mid LIKE '[*]' THEN 'Remedial'
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble
I keep getting the same error: "FROM keyword not found where expected". I expect to have all 3 returns in the new field.
If you're looking for ' character you should escape it.
Change
WHEN grde_code_mid LIKE '[']' THEN 'Continuing'
by
WHEN grde_code_mid LIKE '['']' THEN 'Continuing'
Have a look at this question: How do I escape a single quote in SQL Server?
There are several issues with your query:
you are missing a comma in the SELECT clause between * and the CASE expression (this is causing the error that you are currently getting)
the bracket notations is only supported in SQL-Server; if you want to match on strings that end with * in a portable manner, you need expression ... LIKE '%*', not LIKE '[*]'
single quotes embedded in a string need to be escaped
SELECT *, other_field FROM ... is not supported on all RDBMS (and, as commented by jarhl, actually isn't standard ANSI SQL notation); you usually need to prefix the * with the table name or alias
Consider:
SELECT
g.*,
CASE
WHEN grde_code_mid LIKE '%*' THEN 'Remedial'
WHEN grde_code_mid LIKE '%''' THEN 'Continuing'
ELSE NULL
END AS class_type
FROM grde_tble g
Thank you all. I am still getting the hang of the language and proper terms. I am using Oracle DB. Also, yes, I did need to reference the table in the select statement (SELECT tablename.*). Found a work around:
CASE WHEN regexp_like(grde_code_mid, '[*]') THEN 'Remedial'
WHEN regexp_like(grde_code_mid, '['']') THEN 'Continuing'
ELSE NULL END AS special_class
The percentage sign (%) is used as the "everything" wildcard instead of an asterisk. It will match zero or more characters.
As #onedaywhen said, the two have same function.
But in impala, I find they only work in different specific situation.
show tables like ' '
Suppose in my database opd, there are there table,
opd.haha
opd.haha1
opd.abc
input:
show tables like 'haha*'
output:
opd.haha
opd.haha1
input:
show tables like 'haha%'
output:
Done. 0 results.
select ... like
select 'haha' like 'ha%' -- true
select 'haha' like 'ha*' -- false
select 'haha' like 'ha__' -- true
select 'haha' like 'haha%' -- true
My question is that
To summarise,
asterisk sign only works in show tables clause, and
percentage sign only works in select clause
Is this comment right?
The standard wildcards for like are:
_ which represents a single character
% which represents zero or more characters
like does not implement regular expressions.
If you want regular expressions, then use regexp_like().
I have 2 columns that I want to compare based on Postgres SIMILAR TO notation, in the example below it is the ~*. Lets call the columns: COLUMN1 and COLUMN2.
It works on most of the rows, then after further investigation if the value of either column has "++" the query failed.
SELECT
CASE
WHEN 'gcc' ~* 'gcc++' THEN 1
ELSE 2
END
with the following message
ERROR: invalid regular expression: quantifier operand invalid
Obviously the gcc++ string above need to be escaped.
Question:
Is the there a Postgres built-in function in postgres that escape the column values so that the comparison can be made safer? I'm thinking something like below ...
SELECT
CASE
WHEN escapeMe(COLUMN1) ~* escapeMe(COLUMN2) THEN 1
ELSE 2
END
FROM TABLE_T1
what is difference between these two sql statements
1- select * from tblperson where name not like '[^AKG]%';
2- select * from tblperson where name like '[AKG]%';
showing same results: letter starting from a,k,g
like '[^AKG]% -- This gets you rows where the first character of name is not A,K or G. ^ matches any single character not in the specified set or a specified range of characters. There is one more negation not. So when you say name not like '[^AKG]%' you get rows where the first character of name is A,K or G.
name like '[AKG]% -- you get rows where the first character of name is A,K or G.
The wildcard character [] matches any character in a specified range or a set of characters. In your case it is a set of characters.
So both the conditions are equivalent.
You are using a double 'NOT'. The carrot '^' in your first character match is shorthand for 'not', so you are evaluating 'not like [not' AKG]% IE not like '[^AKG]%'.
1)In the first query you are using 'Not' and '^' basically it is Not twice so it cancels outs
therefore your query is 'Not Like [^AKG]' ==> 'Like [AKG]'
^ a.k.a caret or up arrow.
The purpose of this symbol is to provide a match for any characters not listed within the brackets [] , meaning that normally it wouldn't provide a result for anything that starts with AKG, but since you added the word NOT to the query , you are basically cancelling the operator, just as if you were doing in math :
(- 1) * (- 1)