I'm trying to add to current query where a certain field name must contain either 'Y' or 'N'. I'm currently using substrings and isnumeric functions to manipulate the data within.
Here is an example:
(
LEN(STERLING_RETURN_SIGNAL) > 1
or ISNUMERIC(substring(STERLING_RETURN_SIGNAL,1,1)) = 1
)
So the STERLING_RETURN_SIGNAL, must either be 'Y' or 'N' otherwise +('Error message').
Many thanks. Using Sql Server Management Studio.
Please note, I am a beginner...
select
case
when
STERLING_RETURN_SIGNAL in ('Y','y','N','n')
then STERLING_RETURN_SIGNAL
else
'Invalid value for signal'
end as signal
from ...
Related
I've been looking over some code in an old Classic ASP system of ours that builds its own SQL within the stored procedure and then executes it {shudders}.
Several of the SELECTion lines contain an assignment, similar to:
SELECT
my_field = CASE WHEN value = whatever THEN 1 ELSE 0 END
...
Is there any difference (or anything I need to be aware of) between this and using a standard AS alias?...
SELECT
CASE WHEN value = whatever THEN 1 ELSE 0 END AS my_field
...
No, the following code is all synonymous:
SELECT one = 1;
SELECT 1 one;
SELECT 1 AS one;
SELECT 'one' = 1; --this is deprecated, don't use it.
Which you use (apart from the last), is normally down the preference. Personally, I use AS. One reason is I can then easily tell queries that return datasets, and those that assign values to variables a part.
The 2 examples that you have given are identical. However, when you go through the old code you might also find a variant with an # sign before my_field, like this:
SELECT
#my_field = CASE WHEN value = whatever THEN 1 ELSE 0 END
In this case a varable called #my_field is assigned a value, but nothing is SELECTed. This you can not rewrite to the other syntax using AS #myfield.
UPDATE:
It appears there may be some certificate or rule running on open queries with enrypted column data. I have discovered that the following produces an unencrypted value concat 'more'. I will have to verify with our DBA what may cause this behavior.
case when s.EncryptedColumn is not null then concat(s.[EncryptedColumn], ' more') else s.[RawColumn] end
I am trying to do a simple comparison to null against a varbinary(16) column however I cannot get the result to return true.
This is what I have tried:
Attempt 1:
select
s.[EncryptedColumn],
(case when s.[EncryptedColumn] is not null
then s.[EncryptedColumn]
else s.[RawColumn]
end) as 'result'
Result 1: encrypted data, raw data
Attempt 2:
select
datalength(s.[EncryptedColumn]),
(case when datalength(s.[EncryptedColumn]) > 0
then s.[EncryptedColumn]
else s.[RawColumn]
end) as 'result'
Result 2: encrypted data length (16), raw data
Any ideas?
If I got it right, you should take a look here:
use ISNULL(DATALENGTH(Content), -1) instead, so that you can
distinguish between length 0 and NULL. Or just use DATALENGTH(Content)
UPDATE:
It appears there may be some certificate or rule running on open queries with enrypted column data. I have discovered that the following produces an unencrypted value concat 'more'. I will have to verify with our DBA what may cause this behavior.
case when s.EncryptedColumn is not null then concat(s.[EncryptedColumn], ' more') else s.[RawColumn] end
I have a column that is of type 'bit'. I want the column to display either the word yes (1) or (0) if no. Is there somewhere I can set this?
If not is there a replacement for boolean I can use? True/false will do fine as a replacement
I am using SQL Server-management 2008 R2
I'm trying to amend the table.
Try using case:
select case
when MyBitField = 1 then
'yes'
else
'no'
end
from MyTable
I'm selecting a value out of a table that can either be an integer or a nvarchar. It's stored as nvarchar. I want to conditionally call a function that will convert this value if it is an integer (that is, if it can be converted into an integer), otherwise I want to select the nvarchar with no conversion.
This is hitting a SQL Server 2005 database.
select case
when T.Value (is integer) then SomeConversionFunction(T.Value)
else T.Value
end as SomeAlias
from SomeTable T
Note that it is the "(is integer)" part that I'm having trouble with. Thanks in advance.
UPDATE
Check the comment on Ian's answer. It explains the why and the what a little better. Thanks to everyone for their thoughts.
select case
when ISNUMERIC(T.Value) then T.Value
else SomeConversionFunction(T.Value)
end as SomeAlias
Also, have you considered using the sql_variant data type?
The result set can only have one type associated with it for each column, you will get an error if the first row converts to an integer and there are strings that follow:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the nvarchar value 'word' to data type int.
try this to see:
create table testing
(
strangevalue nvarchar(10)
)
insert into testing values (1)
insert into testing values ('word')
select * from testing
select
case
when ISNUMERIC(strangevalue)=1 THEN CONVERT(int,strangevalue)
ELSE strangevalue
END
FROM testing
best bet is to return two columns:
select
case
when ISNUMERIC(strangevalue)=1 THEN CONVERT(int,strangevalue)
ELSE NULL
END AS StrangvalueINT
,case
when ISNUMERIC(strangevalue)=1 THEN NULL
ELSE strangevalue
END AS StrangvalueString
FROM testing
or your application can test for numeric and do your special processing.
You can't have a column that is sometimes an integer and sometimes a string. Return the string and check it using int.TryParse() in the client code.
ISNUMERIC. However, this accepts +, - and decimals so more work is needed.
However, you can't have the columns as both datatypes in one go: you'll need 2 columns.
I'd suggest that you deal with this in your client or use an ISNUMERIC replacement
IsNumeric will get you part of the way there. You can then add some further code to check whether it is an integer
for example:
select top 10
case
when isnumeric(mycolumn) = 1 then
case
when convert(int, mycolumn) = mycolumn then
'integer'
else
'number but not an integer'
end
else
'not a number'
end
from mytable
To clarify some other answers, your SQL statement can't return different data types in one column (it looks like the other answers are saying you can't store different data types in one column - yours are all strign represenations).
Therefore, if you use ISNUMERIC or another function, the value will be cast as a string in the table that is returned anyway if there are other strigns being selected.
If you are selecting only one value then it could return a string or a number, however your front end code will need to be able to return the different data types.
Just to add to some of the other comments about not being able to return different data types in the same column... Database columns should know what datatype they are holding. If they don't then that should be a BIG red flag that you have a design problem somewhere, which almost guarantees future headaches (like this one).
I have an SQL server database that I am querying and I only want to get the information when a specific row is null. I used a where statement such as:
WHERE database.foobar = NULL
and it does not return anything. However, I know that there is at least one result because I created an instance in the database where 'foobar' is equal to null. If I take out the where statement it shows data so I know it is not the rest of the query.
Can anyone help me out?
Correct syntax is WHERE database.foobar IS NULL. See http://msdn.microsoft.com/en-us/library/ms188795.aspx for more info
Comparison to NULL will be false every time. You want to use IS NULL instead.
x = NULL -- always false
x <> NULL -- always false
x IS NULL -- these do what you want
x IS NOT NULL
Read Testing for Null Values, you need IS NULL not = NULL
Is it an SQL Server database?
If so, use IS NULL instead of making the comparison (MSDN).