How can I shorten this LIKE statement in SQL Server? - sql-server

+ case when
(
(
PYMT.element like '____.T.T-0_______.____.________' or
PYMT.element like '____.T.T-K_______.____.________'
)
and len(PYMT.element) = 31
)
then ''
else '12|'
end
I'm trying to find a more elegant way of doing this like statement. Is there another way of doing it?

Only thing I can see you could do instead is to replace both LIKEs with a single one:
PYMT.element LIKE '____.T.T-[0K]_______.____.________'
And, as WEI_DBA mentions, you can remove the len(PYMT.element) = 31, as the LIKE handles that already.

Related

How to replace regular expression?

I do replacing
SET #data = REPLACE(#data, 'Riched20 10.0.19041}', '');
It is ok, but recently I have detected that it can be
Riched20 10.0.18362 etc
How can I replace in common case like 'Riched20 ...}'?
Can I use a regular-expression?
It should be implementation in T-SQL
If the problem is as simple as this question makes it out to be then you could, instead, find the position of the string 'Riched20' in your value, and then the position of the first } that appears after it and use STUFF to remove the text in that range.
This assumes that the value will always have a } after 'Riched20', and that if there is a } then 'Riched20' also appears. If this isn't the case you will get the value NULL.
DECLARE #data varchar(100) = 'sdjkafhbgtajl asdgasdf, Riched20 10.0.19041} dlkghbsdfl';
SET #data = STUFF(#data, CHARINDEX('Riched20',#data),CHARINDEX('}',#data,CHARINDEX('Riched20',#data)) - CHARINDEX('Riched20',#data) +1,'');
SELECT #data;
If you need "true" pattern replacement, then you are out of luck; T-SQL does not support this as the comments mention.

if a number in column is negative,show positive,else show the number

I have a [Preko]column in a query that is calculating the difference between two columns.If the number is negative,I need to show it positive,and if it is positive,it should stay the same.I can't use ABS in this one.I tried with the case,but it didn't work.
The problem I am getting is that column Preko is invalid.
This is the code for my second try with iif:
SELECT FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta
,FP.Partner
,FP.NazivPartnera
,Sum(FP.Duguje) AS dug
,Sum(FP.Potrazuje) AS pot
,Sum(IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje])) AS USaldo
,Sum(IIf([datumval]<= '1.1.2017'
,IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje]),0)) AS [Preko]
,IIf([Preko]<0,0,[Preko]) AS Preko1
FROM tblFinansijskiPodaci FP
Where FP.Firma = 1
AND FP.Partner=1110
GROUP BY FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta,
,FP.Partner
,FP.NazivPartnera
HAVING (((FP.Konto)=2040))
You can also use a case statement instead of the iif section.
CASE WHEN Preko<0 THEN 0 ELSE Preko END
or
CASE WHEN Preko<0 THEN -Preko ELSE Preko END
That seems to be more in line with your logic depending on how you want to handle the negatives.
Agree with HoneyBadger - ABS is the way to go ABS(-1) returns 1. Have a look at the APEX SQL tools for a quick formatting option. It's free and makes your code a lot easier to read, which means you'll find you get more answers.
WITH cte AS (
SELECT FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta
,FP.Partner
,FP.NazivPartnera
,Sum(FP.Duguje) AS dug
,Sum(FP.Potrazuje) AS pot
,Sum(IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje])) AS USaldo
,Sum(IIf([datumval]<= '1.1.2017'
,IIf([FP].[Konto] Like '2*'
,[duguje]-[potrazuje]
,[potrazuje]-[duguje]),0)) AS [Preko]
FROM tblFinansijskiPodaci FP
Where FP.Firma = 1
AND FP.Partner=1110
GROUP BY FP.Firma
,FP.NazFirme
,FP.Konto
,FP.NazivKonta,
,FP.Partner
,FP.NazivPartnera
HAVING (((FP.Konto)=2040))
)
SELECT *, CASE WHEN Preko<0 THEN 0 ELSE Preko END preko1 FROM cte

Different conditions in WHERE clause

Is there a way to write this simplier?
WHERE
(
(#IdAgent IS NULL AND IdAgent IS NULL)
OR
(#IdAgent IS NOT NULL AND IdAgent = #IdAgent)
)
You can trivially remove one test, since = will never match a NULL and a non-NULL value:
WHERE
(#IdAgent IS NULL AND IdAgent IS NULL)
OR
IdAgent = #IdAgent
You could try using the method from this answer, which, when applied to your situation, would look something like this:
WHERE EXISTS (SELECT IdAgent
INTERSECT
SELECT #IdAgent)
You'll probably need to test it for performance in your particular environment to see if it doesn't run significantly slower than your present solution.

How do I search for multiple values in a column where I need to use wildcards?

In SQL Server, I need to search a column for multiple values, but I don't have the exact values, so I need to use wildcards as well.
My current query looks like this:
SELECT *
FROM table
WHERE fieldname in ( '%abc1234%',
'%cde456%',
'%efg8976%')
This doesn't return any results, and yet if I search for any one individual value, I find it, so I know they're in there. Short of doing multiple OR's, which is a bit unwieldy with several hundred values, is there a way to do this?
I'd also be interested to know why this query doesn't work, since the same query without the %'s works just fine (except for the small problem of only catching the few exact matches).
Look at using a Fulltext Index. That should do a much better job with your search, and make your "OR" problem a little nicer to boot:
SELECT *
FROM table
WHERE CONTAINS(fieldname, '"abc1234" OR "cde456" OR "efg8976"')
See also:
http://www.simple-talk.com/sql/learn-sql-server/full-text-indexing-workbench/
The reason the query doesn't work is that it looks for an exact match for fieldname within the list of values in the parens. It doen't do a LIKE comparison where the wildcards are taken into account.
So your query is equivalent to:
SELECT * from table
where fieldname = '%abc1234%' OR
fieldname = '%cde456%' OR
fieldname = '%efg8976%'
Obviously not what you want.
select table.* from (
table join
( select * from values (
( '%abc1234%' ), ( '%cde456%' ), ( '%efg8976%' )
) ) as search( exp ) on 0 = 0
) where fieldname like exp
or perhaps
select table.* from
table join
( select * from values (
( '%abc1234%' ), ( '%cde456%' ), ( '%efg8976%' )
) ) as search( exp )
on fieldname like exp
modulo some syntax I'm sure.
The point being that this comes close to allowing the list of values to be the only parameter.

How do I handle Error in sql queries?

For sql queries like..
select Quantity_Books/datepart(hour,Rent_Hour) from Rent_Book where (some conditions..)
They will return error when datepart(hour,Rent_Hour) is 0.
If sth like that Happens, I would like to show 0.
I know I should use case when But I am not really sure how..
Or any other better method?
You'd simply test the value first
select
CASE
WHEN datepart(hour,Rent_Hour) = 0 THEN 0
ELSE Quantity_Books/datepart(hour,Rent_Hour)
END
from
Rent_Book where (some conditions..)
Alternatively, use NULL rules
ISNULL((Quantity_Books / (NULLIF(datepart(hour,Rent_Hour), 0))), 0)
select case when datepart(hour,Rent_Hour)<>0 then Quantity_Books/datepart(hour,Rent_Hour) else 0 end as col ...

Resources