IF....THEN statement in T-SQL to perform UPDATE - sql-server

Disclaimer: I am still learning SQL so I apologize if my question comes off as amateur-ish or is otherwise a very simple answer. I have no formal training. I am teaching myself. Thanks.
A particular query was created to update the EMAIL column with data in the EMAIL2 column should the EMAIL column be blank. This query goes on to grab data from the EMAIL3-6 columns should any prior ones also be blank in an attempt to populate the EMAIL column. It currently sits as follows:
update Parents
set email = email2
where email = ' ' OR email is null
go
update Parents
set email = email3
where email = ' ' OR email is null
go
update Parents
set email = email6
where email = ' ' OR email is null
go
(and so on)
Is there a more simple way, using some sort of IF...THEN type conditions to check for blank or null in the EMAIL column and populate it based on data in the secondary column(s)? These columns would also need to be checked for blank or null values and skipped if blank or null is true.
I appreciate any advice that can be given. The above query works it just doesn't feel like the best way to go about doing it.

A handy function you will want to become aquainted with is NULLIF. It allows you to simplify your logic in cases where you might like to treat a value like a NULL. For example, if an application was putting a sentinel value of 'NA' in a NULLable column column1, NULLIF(column1, 'NA') would return the NULL value for all the NULLs and all the NAs. In your case, we'd use this trick to convert empty strings into NULLs.
The other thing we'll do is trim all the empty strings down so our NULLIF only needs to check for the scenario of '' (instead of '', ' ', ' ', ad nauseum). TSQL only provides LTRIM and RTRIM and I have a habit of using RTRIM although trimming an empty string from either direction will result in our desired state. NULLIF(RTRIM(column1),'')
Using that expression, we will now have the right thing to plug into the COALESCE function. Thus
UPDATE
P
SET
email = COALESCE(NULLIF(RTRIM(P.email2), ''), NULLIF(RTRIM(P.email3), ''), NULLIF(RTRIM(P.email4), ''))
FROM
dbo.Parents P
WHERE
-- This will force a table scan which can be a bad thing for performance
-- Generally, one should avoid wrapping functions around a column and instead
-- apply them to the thing being tested
NULLIF(RTRIM(P.email), '') IS NULL

Do you mean something like this?
update Parents
set email = COALESCE(
NULLIF(LTRIM(RTRIM(email2), '')),
NULLIF(LTRIM(RTRIM(email3), '')),
NULLIF(LTRIM(RTRIM(email4), '')),
NULLIF(LTRIM(RTRIM(email5), '')),
NULLIF(LTRIM(RTRIM(email6), ''))
)
where email = ' ' OR email is null
The
LTRIM(RTRIM(email2)
will make sure the convert ' ' to an empty string '', since SQL Server has no trim, but two separate functions LTRIM and RTRIM. NULLIF returns null if the two expressions are equal.
So if any of the email cols is null or just ' ' it will return null.
The COALESCE function will return the value of the first expression, that is not null.

Not to make this a tutorial on COALESCE and NULLIF but to proof everything #billinkc provided in his answer, this is why that works. (Sorry I was working on the solution as he answered it). Plop this into SSMS and have a look at the results. A simple update like shown above will do nicely though.
Just Discovered the SQL Fiddle Resource:
SQL Fiddle
--Setup the temp table
DECLARE #Parents TABLE (
ParentsId INT IDENTITY (1,1) PRIMARY KEY,
email varchar(max),
email2 varchar(max),
email3 varchar(max),
email4 varchar(max),
email5 varchar(max),
email6 varchar(max)
)
--This would be the pull from your real Parents Table.
INSERT INTO #Parents
SELECT
NULL,'test#domain.com',' ',NULL,NULL,NULL
UNION ALL
SELECT
NULL,' ',NULL,NULL,NULL,'test2#domain.com'
UNION ALL
SELECT
NULL,'',NULL,NULL,NULL,'test3#domain.com'
--Look at the data before we cleanse it
SELECT * FROM #Parents
--Take a look at what COALESCE looks like before the cleanse
SELECT ParentsId, COALESCE(email2,email3,email4,email5,email6) AS NewEmail FROM #Parents
--RUN the NULLIF
UPDATE #Parents SET
email2 = NULLIF(email2,' '),
email3 = NULLIF(email3,' '),
email4 = NULLIF(email4,' '),
email5 = NULLIF(email5,' '),
email6 = NULLIF(email6,' ')
SELECT * FROM #Parents
--Take a look at what COALESCE looks like after the cleanse
SELECT ParentsId, COALESCE(email2,email3,email4,email5,email6) AS NewEmail FROM #Parents

Related

TableAdapter List params for IN clause

SQL Server, VS 2019 Winforms, ReportViewer.
This doesn't work in straight SQL either, so I'll start there.
I have a query that looks like this:
declare #d1 Date, #d2 date, #SalesRep nvarchar(20), #customer nvarchar(300), #stype nvarchar(10)
set #d1 = '01 oct 2021'
set #d2 = getdate()
set #SalesRep = ''
set #customer = ''''',''A'',''B'',''C'',''D'',''E'',''F'',''G'',''H'',''I'''
set #sType=null
select #customer
select
vss.Customer, vss.Invoice_No, vss.Salesman,
vss.Invoice_Date, vss.Total_Amount, vss.Description
from
v_SaleSum vss
where
vss.Invoice_Date between #d1 and #d2
and ((ISNULL(#SalesRep,'') = '') OR (vss.Salesman = #SalesRep))
**and (Customer in (#customer))**
and [dbo].[Invoice_sType] (vss.Invoice_No, #sType)=1
Order by
vss.Invoice_No
You can see the crux of the issue here.
The #customer param needs to be a list. But if you comma separate it, as I have the query looks for customers that match the whole comma separated list as one string, not as a list of strings.
I do not have the option of modifying the SQL at runtime because of the whole Dataset, BindingSource, TableAdapter setup.
What am I missing here? There must be some way to add a list of values to a parameter.
--Update:
Here's why I can't use Table-Value Parameters:
There is no "Table" parameter type supported.
I found that I could do this:
... and (#customer like ('%'''+ Customer+'''%'))
Since the match string is in the format '''A'',''B''' (or more simply without having to escape the tick marks, ' 'A','B' '
searching for the customer name to exist in the parameter seems kind of backwards, but its working.
This way, I check if %'A'% is like 'A','B','C'

Is there a SQL Server collation option that will allow matching different apostrophes?

I'm currently using SQL Server 2016 with SQL_Latin1_General_CP1_CI_AI collation. As expected, queries with the letter e will match values with the letters e, è, é, ê, ë, etc because of the accent insensitive option of the collation. However, queries with a ' (U+0027) do not match values containing a ’ (U+2019). I would like to know if such a collation exists where this case would match, since it's easier to type ' than it is to know that ’ is keystroke Alt-0146.
I'm confident in saying no. The main thing, here, is that the two characters are different (although similar). With accents, e and ê are still both an e (just one has an accent). This enables you (for example) to do searches for things like SELECT * FROM Games WHERE [Name] LIKE 'Pokémon%'; and still have rows containing Pokemon return (because people haven't used the accent :P).
The best thing I could suggest would be to use REPLACE (at least in your WHERE clause) so that both rows are returned. That is, however, likely going to get expensive.
If you know what columns are going to be a problem, you could, therefore, add a PERSISTED Computed Column to that table. Then you could use that column in your WHERE clause, but display the one the original one. Something like:
USE Sandbox;
--Create Sample table and data
CREATE TABLE Sample (String varchar(500));
INSERT INTO Sample
VALUES ('This is a string that does not contain either apostrophe'),
('Where as this string, isn''t without at least one'),
('’I have one of them as well’'),
('’Well, I''m going to use both’');
GO
--First attempt (without the column)
SELECT String
FROM Sample
WHERE String LIKE '%''%'; --Only returns 2 of the rows
GO
--Create a PERSISTED Column
ALTER TABLE Sample ADD StringRplc AS REPLACE(String,'’','''') PERSISTED;
GO
--Second attempt
SELECT String
FROM Sample
WHERE StringRplc LIKE '%''%'; --Returns 3 rows
GO
--Clean up
DROP TABLE Sample;
GO
The other answer is correct. There is no such collation. You can easily verify this with the below.
DECLARE #dynSql NVARCHAR(MAX) =
'SELECT * FROM (' +
(
SELECT SUBSTRING(
(
SELECT ' UNION ALL SELECT ''' + name + ''' AS name, IIF( NCHAR(0x0027) = NCHAR(0x2019) COLLATE ' + name + ', 1,0) AS Equal'
FROM sys.fn_helpcollations()
FOR XML PATH('')
), 12, 0+ 0x7fffffff)
)
+ ') t
ORDER BY Equal, name';
PRINT #dynSql;
EXEC (#dynSql);

Microsoft SQL Server : CONCAT() with INSERT

I am currently attempting to combine 2 columns into 1 using CONCAT().
I have
SELECT
ApplicationTitle,
ApplicationVersion,
CONCAT(ApplicationTitle,' ',' - ',' ',ApplicationVersion) as ApplicationName
FROM
<DataBaseName>
-- Hid DataBase name due to privacy concerns.
Now this works and gives me the result I would like, but I need to not just see the result but actually insert it into the table as a new column with those values so I can delete ApplicationTitle and ApplicationVersion. Reasoning is when exporting the information (I do not have control over how the information gets exported out), it separates ApplicationTitle from ApplicationVersion (to clarify, they are application names and application versions, but I need it into 1 column with a - divider, e.g. ApplicationTitle = SQL, ApplicationVersion = 4.0, I want ApplicationName = SQL - 4.0). I've looked online but could not find something similar that worked for my current situation due to needing to delete ApplicationTitle and ApplicationVersion after ApplicationName has been populated.
What is the best way to go about doing this. My thought was INSERT INTO command with CONCAT but that doesn't seem to work for me (I'm sure I'm missing something).
Suppose You have a table named TableName and TableName has one column name ColumnName, then you can insert row at TableName by following query:
INSERT INTO TableName
(ColumnName)
SELECT
CONCAT(ApplicationTitle, ' ', ' - ', ' ', ApplicationVersion)
FROM YourTableName
This will create a new table and insert the records how you like, but if you try to run this again with the same table name it will give an error like "table already exists":
SELECT
ApplicationTitle,
ApplicationVersion,
ApplicationName = CONCAT(ApplicationTitle,' ',' - ',' ',ApplicationVersion)
INTO <NewTableName>
FROM <DataBaseName>

How can I append a column in SQL?

I've tried different things in SQL Server 2012 to append the columns. CONCAT merges the columns look like this: catdogparrot whereas I want it be in a list like:
cat
dog
parrot
I've also tried the + in SQL, giving me the same result. I saw '||' as well, but for some reason it says wrong syntax at the second pipe. Is there a way to append a column to a new one? Or would I have to create a new column from the multiple columns? These columns are in the same table. Any suggestions are advice are greatly appreciated, thanks!
I'm not sure what you are trying to do, but you could try to use
CONCAT(' - ', `column1`, ' - ', `column2`, ' - ', ... `column999`)
Eventually this could help you: You can set the separator to any sequence, even to CHAR(13)+CHAR(10), which is a windows line break.
DECLARE #dummyTbl TABLE(animal VARCHAR(10));
INSERT INTO #dummyTbl
VALUES('cat'),('dog'),('parrots');
DECLARE #separator VARCHAR(5)= ' / ';
SELECT STUFF(
(
SELECT #separator + animal
FROM #dummyTbl AS dt
FOR XML PATH(''),TYPE
).value('.','varchar(max)'),1,LEN(#separator),'');

Join query with user provided data-T-sql

I have a requriment where user will provide many Ids(in Hundres/thousands) in a Text area in vb.net app, I need to use these IDs in T-Sql(Sql Server) to get the data. I dont want to save these Ids in any database table. Just want to pass using a paramater (type of varchar(max)) and use in the procedure.
actually, only read access is permitted for the vb application users.It is Sql-2005 database.Id field is atleaset 12 to 15 characters length.The user will copy/paste data from other source may be CSV or Excel file.
any idea how can i achive this.
any help is appreciated.
Thanks
If you do not want to use Table Valued Parameters, as suggested elsewhere, and you don't want to store the ID's in a temporary table, you can do the following.
Assuming your ID's are integer values, that are separated by commas, in the parameter string, you can use the LIKE operator in your SQL-statement's WHERE filter:
Say you have a parameter, #IDs of type varchar(max). You want to get only those records from MyTable where the ID-column contains a value that has been typed into the #IDs-parameter. Then your query should look something like this:
SELECT * FROM MyTable
WHERE ',' + #IDs + ',' LIKE '%,' + CAST(ID AS varchar) + ',%'
Notice how I prepend and append an extra comma to the #IDs parameter. This is to ensure that the LIKE operator will still work as expected for the first and last ID in the string. Make sure to take the nescessary precautions against SQL injection, for example by validating that users are only allowed to input integer digits and commas.
Try using CHARINDEX:
DECLARE #selectedIDs varchar(max) = '1,2,3'
Set #selectedIDs = ',' + #selectedIds + ',' --string should always start and end with ','
SELECT * FROM YourTable
WHERE CHARINDEX(',' + CAST(IDColumn as VARCHAR(10)) + ',', #selectedIds) > 0

Resources