How to check the date format of the cell (oracle) - sql

Please, help)
I need to verify that cells are populated with correct values.
I have a few types of column with a few format of value, i.e. '2015-05-20Z' and 'XXX/MOSCOW/XXXMSX/2015-05-20'.
I do not know how to check the date, if it is only part of the value.
This query work:
case
when A like 'XXX/MOSCOW/XXXMSX/%'
then 'true'
else 'false'
end
But it doesn't enough...
This query doesn't work:
case
when A like 'XXX/MOSCOW/XXXMSX/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'
then 'true'
else 'false'
end

You appear to be trying to use like to match regular expressions. If that is your intent, look into regexp_like instead.

use this:
case
when regexp_like(A, 'XXX/MOSCOW/XXXMSX/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]')
then 'true'
else 'false'
end

Related

SSRS: CASE WHEN NULL

I'm trying to import data from a table into my SQL Report Builder report.
In this particular column, the data will either be someone's name or "NULL".
I want to set my field to change NULL to "Other", but leave it how it is if it contains a name.
I know I must be close with what I have below, but I can't figure out how to get it to not alter the value if it's NOT NULL:
CASE WHEN ([Reviewed_By] IS NULL) THEN 'Other' ELSE '' END AS [Reviewed_By]
Obviously, with how it's written here, it will convert any name to a blank but I can't figure out the correct logic to get it to "skip" the line-item if it's a valid name.
Any help is appreciated!
Let me know if you need any other information.
Thanks in advance,
Cameron
you could just do ...
SELECT
ISNULL([Reviewed_By], 'Other') AS [Reviewed_By]
FROM myTable
To answer your question for the SQL side.
CASE WHEN [Reviewed_By] IS NULL THEN 'Other' ELSE [Reviewed_By] END AS [Reviewed_By]
Report builder has functionality to do this as well with expressions.
You can read more here 32716829/if-value-null-then-else-value-ssrs-expression-issues.

How to identify and flag a sub-string from a column of entries in Oracle SQL

I'd like to add a column to my query that has a binary Yes/No, depending on whether a substring can be found anywhere in another column.
Example: I'd like to find TTY and have them labelled in the new column:
Name | ...... | Flag_Column
TestTTY123 Yes
Test-test No
TTY111 Yes
11TTY11 Yes
Test-tube No
Test-TTY Yes
Sorry if the question is very simple, I've tried searching for a solution, but it seems that it's a bit more particular than the ones I've come across.
I've tried
case when INSTR(NAME,'TTY') = 1 THEN 'Yes'
else 'No'
end Flag_Column
I was also considering using the like function, but it didn't seem like a good fit. Any suggestions pointing to an obvious solution I'm missing?
You've almost got the right solution. INSTR won't always return 1 when it matches but rather a number corresponding to the location in the string it found the match.
Just change your query to:
case when INSTR(name,'TTY') > 0 THEN 'Yes'
else 'No'
end Flag_Column
If you also need to consider case insensitive search(to return Yes for values such as tty, TTY, TtY ..), then REGEXP_LIKE()
select case when REGEXP_LIKE(name,'TTY','i') then 'Yes'
else 'No'
end as Flag_Column
from t
Or REGEXP_INSTR() might be used :
select case when REGEXP_INSTR(name,'TTY',1,1,0,'i') > 0 then 'Yes'
else 'No'
end as Flag_Column
from t
Even instr might be overcomplicating it, if the requirement is a simple pattern match. How about just:
case
when name like '%TTY%' then 'Yes'
else 'No'
end as flag_column
Why didn't like seem like a good fit?

An expression of non-boolean type

With this:
select puser_id, puser_name, plast_login_time, plicense_level =
case
when '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time
I have error like this:
An expression of non-boolean type specified in a context where a
condition is expected, near 'then'.
Please help me to find the problem in code.
Change your WHEN to:
when plicense_level = '1'
You need to have a valid comparison in WHEN to avoid the error.
From the CASE documentation:
WHEN when_expression
Is a simple expression to which input_expression is compared when the
simple CASE format is used. when_expression is any valid expression.
Your syntax for the CASE statement expression isn't quite right, try:
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end
If what you're trying is to show the user a text describing the licennse instead of the numeric value of the column you should try something like this:
select
puser_id,
puser_name,
plast_login_time,
case plicense_level
when '1' then 'Потребитель'
else 'Автор'
end as plicense_level
from dbo.PPOM_USER
order by plast_login_time
You can find some samples of CASE in this page at MSDN
Thank all of you!
The dicision was in changing data type of field plicense_level in report
Figure 1
Figure 2
select puser_id, puser_name, plast_login_time, plicense_level =
case
when plicense_level = '1' then 'Потребитель'
else 'Автор'
end
from dbo.PPOM_USER
order by plast_login_time

how to detect in case statement if the value of the field is a string "0" in oracle?

in oracle, How to detect in case statement if the value of the field is in "0", then set it to ""?
because I have a problem detecting it when i use something like this
case
when REGEXP_LIKE (ctyid, '^[:DIGIT]') then '""'
it doesn't work, am still getting "0" in the output..i want to set it to ""
If you want to look for 0 anywhere in the string, then use LIKE operator :
WHEN ctyid LIKE '%0%' THEN NULL
That would return NULL value if 0 is found anywhere in the string.
If you want to look for the value as 0 itself, then use = operator.
WHEN ctyid = '0' THEN NULL
CASE looks easier to read, but the same expression could be written as DECODE :
DECODE(cytid, '0', NULL, cytid)
Please reference the documentation of Regular Expression Syntax in ORACLE
Right expression should be:
case
when REGEXP_LIKE (ctyid, '^[[:digit:]]') then '""' else ctyid end
OR
case
when REGEXP_LIKE (ctyid, '^\d') then '""' else ctyid end
But your sample code would match all ctyid start with a number, not only with 0 in it.
UPDATE
According to #LalitKumarB's comment, I have to append following code that I think it should be finished by the OPself.
case
when REGEXP_LIKE (ctyid, '^0') then '""' else ctyid end

How to return different strings from a Boolean type in MySQL?

If I have a column set up as Boolean in MySql, a query returns the value as either 0 or 1.
Is it possible to do something like this
SELECT `bool_value` AS "yes" OR "no"
What I mean is, return two different strings based on whether it is true or false.
SELECT CASE WHEN bool_value <> 0 THEN "yes" ELSE "no" END
You need the case statement.
SELECT (CASE WHEN column <> 0 THEN 'yes' ELSE 'no' END) As Value
http://dev.mysql.com/doc/refman/5.0/en/case.html
MySql supports the standdard SQL CASE statement, which other answers use. MySQL also has the shorter, but non-standard IF statement
SELECT IF(bool_value,'Yes','No')
See
MySQL Flow Control Functions: IF