SQL - How can I use a Select based on the parameter - sql-server

I've got a sql statement in a SQL Server stored procedure using a where clause like this:
Select (fields)
From Table
Where Testname like #SearchLetter + '%'
I need to do check the parameter #SearchLetter and if it's "#", I need to make the parameter choose records that the field Testname starts with a numeric field (0-9)
I've tried several things (ifs/selects, etc.) but I can't seem to get what I need.
anyone know how I can do this?

Try
where Testname like case #SearchLetter
when '#' then '[0-9]' + '%'
else #Searchletter + '%' end

You can use
Where Testname like REPLACE(#SearchLetter, '#', '[0-9]') + '%'
Assuming that the string is always one character (or that you always want this substitution to occur wherever the hash falls) Otherwise use a CASE statement to check for exact equality.
Where Testname like
CASE WHEN #SearchLetter = '#'
THEN '[0-9]'
ELSE #SearchLetter
END + '%'

The simplest way is
where left(#SearchLetter, 1) between '0' and '9

Related

Select Query is not working with in Clause

Sql query is not working if we pass value In clause converted from 'Canada,Portugal' to 'Canada','Portugal'
if pass hardcode value In clause 'Canada','Portugal' it works
declare #GeographicalLocation varchar(max)
set #GeographicalLocation ='Canada,Portugal'
set #GeographicalLocation = REPLACE(#GeographicalLocation, ',', ''',''')
set #GeographicalLocation = ''''+#GeographicalLocation+'''';
select ContinentName from [ContinentList] where ContinentId in
(select ContinentId from [CountryList] where [CountryName]
in(#GeographicalLocation)and BaseId is Null)
Because, at the end your where clause looks like:
where [CountryName] in ('''Canada'',''Portugal''')
This string in where caluse should be two separate strings, but it's only one! It should look like this:
where [CountryName] in ('Canada','Portugal')
So, for this situation I'd use something like
where charindex([CountryName], #GeographicalLocation) > 0
In this approach, you don't need to append extra ' to your string variable.
And I'd recommend using case sensitive collation.

Why the result is still null after placing the condition also? [duplicate]

I wrote a T-SQL Statement similar like this (the original one looks different but I want to give an easy example here):
SELECT first_name +
CASE last_name WHEN null THEN 'Max' ELSE 'Peter' END AS Name
FROM dbo.person
This Statement does not have any syntax errors but the case-clause always chooses the ELSE-part - also if the last_name is null. But Why?
What I want to do is to unite first_name and last_name, but if last_name is null the whole name becomes null:
SELECT first_name +
CASE last_name WHEN null THEN '' ELSE ' ' + last_name END AS Name
FROM dbo.person
Do you know where the problem is?
CASE WHEN last_name IS NULL THEN '' ELSE ' '+last_name END
The WHEN part is compared with ==, but you can't really compare with NULL. Try
CASE WHEN last_name is NULL THEN ... ELSE .. END
instead or COALESCE:
COALESCE(' '+last_name,'')
(' '+last_name is NULL when last_name is NULL, so it should return '' in that case)
There are plenty of solutions but none covers why the original statement doesn't work.
CASE last_name WHEN null THEN '' ELSE ' '+last_name
After the when, there is a check for equality, which should be true or false.
If one or both parts of a comparison is null, the result of the comparison will be UNKNOWN, which is treated like false in a case structure. See: https://www.xaprb.com/blog/2006/05/18/why-null-never-compares-false-to-anything-in-sql/
To avoid this, Coalesce is the best way.
Given your query you can also do this:
SELECT first_name + ' ' + ISNULL(last_name, '') AS Name FROM dbo.person
The problem is that null is not considered equal to itself, hence the clause never matches.
You need to check for null explicitly:
SELECT CASE WHEN last_name is NULL THEN first_name ELSE first_name + ' ' + last_name
try:
SELECT first_name + ISNULL(' '+last_name, '') AS Name FROM dbo.person
This adds the space to the last name, if it is null, the entire space+last name goes to NULL and you only get a first name, otherwise you get a firts+space+last name.
this will work as long as the default setting for concatenation with null strings is set:
SET CONCAT_NULL_YIELDS_NULL ON
this shouldn't be a concern since the OFF mode is going away in future versions of SQl Server
The issue is that NULL is not considered to be equal to anything even not to itself, but the strange part is that is also not not equal to itself.
Consider the following statements (which is BTW illegal in SQL Server T-SQL but is valid in My-SQL, however this is what ANSI defines for null, and can be verified even in SQL Server by using case statements etc.)
SELECT NULL = NULL -- Results in NULL
SELECT NULL <> NULL -- Results in NULL
So there is no true/false answer to the question, instead the answer is also null.
This has many implications, for example in
CASE statements, in which any null value will always use the ELSE clause unless you use explicitly the WHEN IS NULL condition (NOT the WHEN NULL condition )
String concatenation, as
SELECT a + NULL -- Results in NULL
In a WHERE IN or WHERE NOT IN clause, as if you want correct results make sure in the correlated sub-query to filter out any null values.
One can override this behavior in SQL Server by specifying SET ANSI_NULLS OFF, however this is NOT recommended and should not be done as it can cause many issues, simply because deviation of the standard.
(As a side note, in My-SQL there is an option to use a special operator <=> for null comparison.)
In comparison, in general programming languages null is treated is a regular value and is equal to itself, however the is the NAN value which is also not equal to itself, but at least it returns 'false' when comparing it to itself, (and when checking for not equals different programming languages have different implementations).
Note however that in the Basic languages (i.e. VB etc.) there is no 'null' keyword and instead one uses the 'Nothing' keyword, which cannot be used in direct comparison and instead one needs to use 'IS' as in SQL, however it is in fact equal to itself (when using indirect comparisons).
Found a solution to this. Just ISNULL the CASE statement:
ISNULL(CASE x WHEN x THEN x ELSE x END, '') AS 'BLAH'
CASE
WHEN last_name IS null THEN ''
ELSE ' ' + last_name
END
Jason caught an error, so this works...
Can anyone confirm the other platform versions?
SQL Server:
SELECT
CASE LEN(ISNULL(last_name,''))
WHEN 0 THEN ''
ELSE ' ' + last_name
END AS newlastName
MySQL:
SELECT
CASE LENGTH(IFNULL(last_name,''))
WHEN 0 THEN ''
ELSE ' ' + last_name
END AS newlastName
Oracle:
SELECT
CASE LENGTH(NVL(last_name,''))
WHEN 0 THEN ''
ELSE ' ' + last_name
END AS newlastName
When you get frustrated trying this:
CASE WHEN last_name IS NULL THEN '' ELSE ' '+last_name END
Try this one instead:
CASE LEN(ISNULL(last_Name,''))
WHEN 0 THEN ''
ELSE ' ' + last_name
END AS newlastName
LEN(ISNULL(last_Name,'')) measures the number of characters in that column, which will be zero whether it's empty, or NULL, therefore WHEN 0 THEN will evaluate to true and return the '' as expected.
I hope this is a helpful alternative.
I have included this test case for sql server 2008 and above:
DECLARE #last_Name varchar(50) = NULL
SELECT
CASE LEN(ISNULL(#last_Name,''))
WHEN 0 THEN ''
ELSE 'A ' + #last_name
END AS newlastName
SET #last_Name = 'LastName'
SELECT
CASE LEN(ISNULL(#last_Name,''))
WHEN 0 THEN ''
ELSE 'A ' + #last_name
END AS newlastName
I tried casting to a string and testing for a zero-length string and it worked.
CASE
WHEN LEN(CAST(field_value AS VARCHAR(MAX))) = 0 THEN
DO THIS
END AS field
You can use IsNull function
select
isnull(rtrim(ltrim([FirstName]))+' ','') +
isnull(rtrim(ltrim([SecondName]))+' ','') +
isnull(rtrim(ltrim([Surname]))+' ','') +
isnull(rtrim(ltrim([SecondSurname])),'')
from TableDat
if one column is null you would get an empty char
Compatible with Microsoft SQL Server 2008+
Use the CONCAT function available in SQL Server 2012 onward.
SELECT CONCAT([FirstName], ' , ' , [LastName]) FROM YOURTABLE
NULL does not equal anything. The case statement is basically saying when the value = NULL .. it will never hit.
There are also several system stored procedures that are written incorrectly with your syntax. See sp_addpullsubscription_agent and sp_who2.
Wish I knew how to notify Microsoft of those mistakes as I'm not able to change the system stored procs.
In SQL Server 2017, Microsoft introduced a Concatenate With Separator function, for just your situation:
SELECT CONCAT_WS(' ', first_name, last_name) FROM dbo.person
CONCAT_WS skips NULL values, but not empty strings.
Interestingly, MySQL introduced CONCAT_WS over a decade earlier.
You can use like this:
CASE IsNull(last_name,'') WHEN '' THEN 'Max' ELSE 'Peter' END AS Name

SQL Server LIKE search with [ ] not matching as expected

I'm trying to search the contents of a text column in a SQL Server DB. I do not have the ability to add a fulltext index to the column, so I'm trying to do so in a query:
SELECT t.DateBegin, t.Action, t.Detail, t.Status, t.ErrorText,
CASE WHEN t.ErrorText LIKE '%[IBM][CLI Driver][DB2/LINUXX8664]%' THEN 'IBM'
WHEN t.ErrorText LIKE '%WebApplicationContainerServer%' THEN 'WACS'
ELSE '' END AS ErrorType
FROM (
SELECT a.DateBegin, a.Action, a.Detail, a.Status,
CONVERT(varchar(max),
REPLACE(REPLACE(CAST(a.ExtInfo AS varchar(max)), CHAR(13), ' '), CHAR(10), ' ')
) as ErrorText
FROM bi4infoburst.dbo.IBT_RTS_ACTION a
WHERE a.DateBegin >= DATEADD(day, -7, GETDATE())
AND a.Status = 5
) t
ORDER BY t.DateBegin
My thought here is that if I converted it to a varchar then I could use the LIKE operator. But when I run this query, it always matches the first case regardless of what text or pattern I put in.
What am I doing wrong?
It is an issue with character escaping.
Square brackets have to be handled properly because otherwise are interpreted as special chars like % is; using square brackets you match any of the char inside the brackets and that's why the first CASE always matches.
The solution is to to declare an escape character in the statement to escape the brackets.
If used in a WHERE clause the syntax would be as follows:
WHERE t.ErrorText LIKE '%![IBM!]![CLI Driver!]![DB2/LINUXX8664!]%' ESCAPE '!'
I don't know if is possible and how to use it in a CASE statement...

Search Varchar and Integer Columns in T-SQL

I have a search functionality in my application which searches a string value of a particular column. The search text is passed as a parameter to the procedure. Now I have to search in another column which is an integer from another table. The input param is one but now I have to search in both the columns and return the result according to that.
//Query:
#SearchText VARCHAR(8000) = ''
SELECT DISTINCT Id, TransactionId, wfdFieldValue
where (wfdFieldValue LIKE '%' + #SearchText + '%' OR #SearchText = '')
In the above query I have to include the TransactionId in the where condition. So that if the user searched using the TransactionId or the Fieldvalue the query will return the result.
Note: The TransactionId is an Integer DataType.
If I understand you correctly, you'll want a hit in either column to make the row show up in search results. In that case, you'll need a simple OR;
SELECT DISTINCT Id, TransactionId, wfdFieldValue
FROM bop
WHERE #SearchText='' OR
wfdFieldValue LIKE '%' + #SearchText + '%' OR
TransactionId LIKE '%' + #SearchText + '%'
(You can just use LIKE on the integer column right away without manually converting.)
Of note though, this kind of wildcard search with a leading % does not use indexes in an optimal way, performance of this query will most likely not be great as the table grows.
SELECT DISTINCT Id, TransactionId, wfdFieldValue
where wfdFieldValue LIKE '%' + #SearchText + '%'
OR #SearchText = ''
OR CAST(TransactionId AS VARCHAR(50)) LIKE '%' + #SearchText + '%'
A cast should solve it, but I would first think about possible design improvements.

select query, compare null records

I'm writing an sql server query:
select * from vwApplicationList
where status='Unassigned' AND
BBL like '%' + #BBL + '%' AND
UnitOrAppartmentNumber like '%'+#Appartment+'%' AND
ResidenceTypeDescription like '%'+#ResidenceTypeDescription+'%' AND
SN1 like '%'+#SN1+'%' AND
SN2 like '%'+#SN2+'%'
The problem is that the field "SN2" is null in several records. So how can I compare them?
you can do
COALESCE(SN2 ,'') like '%'+#SN2+'%'
COALESCE
If you want a positive result on the comparison:
... AND COALESCE(SN2, #SN2) LIKE '%' + #SN2 + '%'
If you want a negative result (assuming #SN2 is not also an empty string):
... AND COALESCE(SN2, '') LIKE '%' + #SN2 + '%'
NULL and only be compared with IS NOT and IS NOT NULL.
If you want to always include NULL you could try
(SN2 like '%'+#SN2+'%' OR SN2 IS NULL)
If you only want to include NULL when #SN2 is empty, try something like
(SN2 like '%'+#SN2+'%' OR (#SN2 = '' AND SN2 IS NULL))

Resources