select
rlike('0737-M02828','^\\w*$' ),
rlike('0737_M02828','^\\w*$' ),
rlike('Madsg','^\\w*$' ),
rlike('073702828','^\\w*$' );
I am looking for:
true, true, true, false
but get:
('0737-M02828','^\\w*$' )= False
rlike('073702828','^\\w*$' )=TRUE
Please suggest - My requirement to check string.
same sql different shape:
select column1,
regexp_count(column1, '^\\w*$' ),
rlike(column1 ,'^\\w*$' )
from values
('0737-M02828'),
('0737_M02828'),
('Madsg'),
('073702828')
gives:
COLUMN1
REGEXP_COUNT(COLUMN1, '^\W*$' )
RLIKE(COLUMN1 ,'^\W*$' )
0737-M02828
0
FALSE
0737_M02828
1
TRUE
Madsg
1
TRUE
073702828
1
TRUE
The REGEX pattern doc's says:
\w: “word” character (a-z, A-Z, underscore (“_”), or decimal digit).
\W: not a word character.
Thus if you use something like:
rlike(column1 ,'^[\\w-]*$' ) AND rlike(column1 ,'.*[a-zA-Z].*' )
select column1,
rlike(column1 ,'^\\w*$' ) as _current,
rlike(column1 ,'^[\\w-]*$' ) AND rlike(column1 ,'.*[a-zA-Z].*' ) as _new
from values
('0737-M02828'),
('0737_M02828'),
('Madsg'),
('073702828')
COLUMN1
_CURRENT
_NEW
0737-M02828
FALSE
TRUE
0737_M02828
TRUE
TRUE
Madsg
TRUE
TRUE
073702828
TRUE
FALSE
Related
I cant seem to find a answer to this. I am using GUI to create a table and a column in there is called DuesPaid. I need this column boolan value to be false and it has the datatype of bit. How might one do this am I just missing where its at?
BIT is the type to store boolean values. 1 will mean true, and 0 will mean false. You can also compare it to strings 'true' and 'false'.
If you run this query:
SELECT IIF(CAST(1 AS BIT) = 'true', 'Yes', 'No') as [1 = true?]
, IIF(CAST(0 AS BIT) = 'false', 'Yes', 'No') as [0 = false?]
, IIF(CAST(0 AS BIT) = 'true', 'Yes', 'No') as [0 = true?]
You'll get the following:
1 = true? 0 = false? 0 = true?
--------- ---------- ---------
Yes Yes No
I realize that comparing NULL to any other value (including NULL) will always result in false.
DECLARE #IsSet bit = NULL;
SELECT IIF(#IsSet = 1, 'true', 'false')
SELECT IIF(#IsSet != 1, 'true', 'false')
This outputs:
false
false
But this is part that confuses me:
SELECT IIF(NOT(#IsSet = 1), 'true', 'false')
SELECT IIF(NOT(#IsSet != 1), 'true', 'false')
This also outputs:
false
false
I would expect that the NOT would have flipped the value to TRUE. (Which it does if #IsSet is set to 0 for the first expression)
It seems that the compare to the null value has some power over the boolean logic outside the parenthesis.
But the null compare is not all powerful over boolean logic:
SELECT IIF((#IsSet = 1) OR (1=1), 'true', 'false')
SELECT IIF((#IsSet != 1) OR (1=1), 'true', 'false')
This returns:
true
true
I don't understand what is happening here, but I assume that this is done on purpose. But I don't know why.
Can someone explain why NOT(NULL!=1) does not equal true.
A comparison with NULL results in UNKNOWN rather than TRUE or FALSE. NOT UNKNOWN also results in UNKNOWN, which is neither TRUE nor FALSE. One cannot "flip" UNKNOWN to a Boolean value using NOT.
This 3-way logic requires one to use IS NULL or IS NOT NULL to test for NULL values rather than traditional Boolean logic.
The way you are using NOT is not correct. You'll get error, If you only execute the NOT condition as follows:
SELECT NOT(#IsSet = 1)
When you enclose your incorrect usage of NOT condition inside IIF condition, SQL server won't show you the error, However the statement will be evaluated to false output.
If you want to explicitly check for NULL value, then following practice can be adopted.
SELECT IIF(#IsSet IS NULL, 'true', 'false')
Lastly, the following condition is returning 'true' in output, because one of the OR condition (1==1) is always evaluating to 'true', hence the overall output of the IIF statement is true.
SELECT IIF((#IsSet = 1) OR (1=1), 'true', 'false')
SELECT IIF((#IsSet != 1) OR (1=1), 'true', 'false')
I have a query where many columns could be blank or null. They actually have longer names than the example below which I am using as an example:
select *
from table1
where field1 is not null and field1 != '' and
field2 is not null and field2 != ''
...etc
It gets tiresome having to type
x is not null and x != ''.
Is there some way to specify "x is not null and x != ''"?
Like for Java with
StringUtils.isNotEmpty(x)
I use
where isnull(x, '') <> ''
a lot. I find it a bit easier to "understand" than nullif.
-- EDIT ---------------------------------------
I missed that they were all ANDed together. So, if all N fields must be non-null and not empty, assuming that all fields are strings (varchars), this should do it:
where isnull(field1 + field2 + field3 + ... + fieldN, '') <> ''
First, the strings are concatenated together:
If any are null, the result will be null
If none are null and all are empty, the result will be an empty string
Else, the result will be a non-empty string
Next, the results are isnulled:
If the concatenated value is null, it is set to an empty string
Else, you get the concatenated contents (empty or not-empty string)
Last, compare that with the empty string:
If True, then either all are empty or one or more is null
If False, none are null and at least one is not empty
Try
WHERE NULLIF(field1, '') IS NULL
For SQL Server, I would use COALESCE for this:
WHERE COALESCE(field1, '') > ''
ISNULL also works
If you want to exclude rows where every field is null or blank you can do it like this:
WHERE COAlESCE(Field1,Field2,Field3,Field4,Field5,'') <> ''
I work with a SSAS Cube and Datazen (dashboard creator). I've a data view (for the valueCode 'AZE') with 3 parameters:
AgeClassCode: 'AGE01', 'AGE02', 'AGE03', ...
StatutCode: 'A', 'B', 'C', 'D', ...
NiveauCode: 'X', 'Y', 'W', ...
With this query, when I use multiple values or just one value for each, it works.
But I would like that the query returns all values for a parameter when the parameter's value is null. I've tested ISEMPTY(#param), ISEMPTY(STRTOSET(#param)), ... but that returns this error:
An mdx expression was expected. An empty expression was specified.
This query works for one or more values:
SELECT
NON EMPTY
{
[Measures].[Value], [Measures].[PreviousValueYear], [Measures].[PreviousValueReportMonth]
} ON COLUMNS,
NON EMPTY
{
NONEMPTY
(
[EntiteFederal].[ServiceCode].[ServiceCode].ALLMEMBERS *
[EntiteFederal].[EntiteCode].[EntiteCode].ALLMEMBERS *
[ReportMonth].[ReportMonth].[ReportMonth].ALLMEMBERS *
[T].[Year].[Year].ALLMEMBERS
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM
(
SELECT ( { [ValueType].[ValueCode].&[AZE] } ) ON COLUMNS
FROM (
SELECT ( STRTOSET('{{ #AgeClassCode }}') ) ON COLUMNS
FROM (
SELECT ( STRTOSET('{{ #NiveauCode }}') ) ON COLUMNS
FROM
(
SELECT ( (STRTOSET('{{ #StatutCode }}') ) ON COLUMNS
FROM [MyCube]
)
)
)
)
WHERE
(
IIF( STRTOSET('{{ #StatutCode }}').Count = 1, STRTOSET('{{ #StatutCode }}'), [Statut].[StatutCode].currentmember ),
IIF( STRTOSET('{{ #NiveauCode }}').Count = 1, STRTOSET('{{ #NiveauCode }}'), [Niveau].[NiveauCode].currentmember ),
IIF( STRTOSET('{{ #AgeClassCode }}').Count = 1, STRTOSET('{{ #AgeClassCode }}'), [AgeClass].[AgeClassCode].currentmember ),
[ValueType].[ValueCode].&[AZE]
)
What do I have to change?
EDIT:
To test the strtoset()
, the good solution is the
isError()
When using strToSet or strToMember you need to supply a string that represents valid mdx so for example these are ok:
strToSet("[AgeClassCode].[AgeClassCode].members")
strToMember("[AgeClassCode].[AgeClassCode].[AGE01]")
This isn't valid as NULL isn't a string, or something that represents mdx:
strToSet(NULL)
So if in your client you'd like NULL to represent all members then somehow you need to transform the NULL to a string "[AgeClassCode].[AgeClassCode].members" before it hits strToSet.
I have a problem want to check if column is null or false then it return 0 otherwise it returns 1, here my column is bit type. Below is my statement where I want to check:
FROM
[dbo].[M_AttributeSet] AttributeSet
WHERE
(#AD_Org_ID IS NULL OR AttributeSet.AD_Org_ID IN (Select ID From fnSplitter(#AD_Org_ID)))
AND (#Atleastonevalue IS NULL OR AttributeSet.Atleastonevalue =#Atleastonevalue )
How can I do this? Thanks for your comments in advance
this should return 0 when false or 0 and 1 when true:
ISNULL(yourcolumn,0)