column values into one string in sql stored procedure - sql

I have and address table with the following columns:
addressId
buildingName
streetNo
streetName
streetType
subAddressNo
subAddressType
suburb
postCode
state
What I want is to SELECT * from address where addressId = #addressId, and that return would be all columns concatenated into one string. Something like:
set #addressString = (SELECT * from address where addressId = #addressId)

select coalesce(cast(addressId as varchar)+ ',', '') + coalesce(buildingName+ ',', '')
coalesce(cast(streetNo as varchar)+ ',', '') -- + and so on
from address
remember to cast the numeric types as varchar

Try this:
SELECT buldingName + ' ' + streetNo + ' ' + streetName + ' ' + theRestOfYourColumns AS ClientAddress
FROM YourAddressTable
WHERE addressId = #addressIdPreviouslyAssignedVariable

Related

How can I store a SELECT query result(one column) into a new table's column?

This is my query:-
select
(FirstName + ' ' + Lastname) as [Name]
from
c_client
where
MiddleInitial is null
union
select
(FirstName + ' ' + MiddleInitial + '''' + Lastname) as [Name]
from
c_client
where
MiddleInitial is not null
After executing it, I'm getting this output:
This is my new table:
CREATE TABLE AddData(Name VARCHAR(MAX))
I want to insert the result generated by the SELECT query into my new table AddData. Can you help me do this?
You would use insert:
insert into AddData (Name)
Select (FirstName + ' ' + Lastname) as [Name]
from c_client
where MiddleInitial IS NULL
UNION
Select (FirstName + ' ' + MiddleInitial +''''+ Lastname) as [Name]
from c_client
where MiddleInitial IS NOT NULL;
I would instead suggest writing the logic as :
select (FirstName +
coalesce(MiddleInitial + '''', '') +
' ' +
Lastname
) as Name
into AddData
from c_client c;
You won't have to create the table first.
Also, if you do want to remove duplicates then use select distinct. It is not clear if you are using union intentionally to remove duplicates or just to combine two separate subqueries.

SQL Server Computed Column Trim

I am trying to add a computed column to an SQL database. The Computed Column Specification looks like this
BusinessName + ' ' + Surname + ' ' + FirstName
which works fine.
Often the BusinessName is blank so I want to Trim it
Trim(BusinessName + ' ' + Surname + ' ' + FirstName)
But when I do I get an error
You can use below logic,
First option: check if value is null, replace it by empty string, you will have leading empty space if BusinessName is null
isnull(BusinessName, '') + ' ' + Surname + ' ' + FirstName
or
Second option: check if BusinessName is empty or null, do not take into account if so. You won't have leading empty space if BusinessName is null or empty
case when isnull(BusinessName, ' ') <> ' '
then BusinessName + ' ' + Surname + ' ' + FirstName
else Surname + ' ' + FirstName
end as FullName
I think it depends by column datatypes and constraints. A more general approach could be the following. Starting from this if as a rule SURNAME and NAME (for instance) are not NULLABLE and cannot be blank, you can omit some of the functions:
DECLARE #T AS TABLE (BUSIN_NAME VARCHAR(20), SURNAME VARCHAR(20), NAME VARCHAR(20))
INSERT INTO #T VALUES(NULL, NULL, NULL);
INSERT INTO #T VALUES(NULL, 'A', NULL);
INSERT INTO #T VALUES(NULL, 'B ', NULL);
INSERT INTO #T VALUES('', 'Karl ', ' Smith');
INSERT INTO #T VALUES('Dr.', 'Karl ', ' Smith');
INSERT INTO #T VALUES('Dr. ', 'Joe ', ' Martin ');
SELECT BUSIN_NAME, SURNAME, NAME
, LTRIM(RTRIM( LTRIM(RTRIM(ISNULL(BUSIN_NAME,'')))+ ' ' + LTRIM(RTRIM(ISNULL(SURNAME,'')))+' ' +LTRIM(RTRIM(ISNULL(NAME,''))))) DESCR
, DATALENGTH ( LTRIM(RTRIM( LTRIM(RTRIM(ISNULL(BUSIN_NAME,'')))+ ' ' + LTRIM(RTRIM(ISNULL(SURNAME,'')))+' ' +LTRIM(RTRIM(ISNULL(NAME,''))))) ) AS LENGTH
FROM #T
OUtput:
BUSIN_NAME SURNAME NAME DESCR LENGTH
NULL NULL NULL 0
NULL A NULL A 1
NULL B NULL B 1
Karl Smith Karl Smith 10
Dr. Karl Smith Dr. Karl Smith 14
Dr. Joe Martin Dr. Joe Martin 14
I would suggest doing the following:
select stuff( (coalesce(' ' + BusinessName, '') +
coalesce(' ' + Surname, '') +
coalesce(' ' + Firstname, '')
), 1, 1, '')
These easily generalizes to more fields. You can use ltrim() rather than stuff(), because you are using spaces as a separator. stuff() is more general, because it handles other separators (notably commas).
As a computed column:
alter table t add newcol as
(stuff( (coalesce(' ' + BusinessName, '') +
coalesce(' ' + Surname, '') +
coalesce(' ' + Firstname, '')
), 1, 1, ''
)
)

Query display names

I have a 2 columns in a SQL Server table.
One is characters like 'Attn: firstname lastname', and the other column has a number associated with that person.
I can't figure out how to query them so the info comes out as:
Lastname, Firstname - number
If you are in SQL Server you can do this
Select Col1 + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
If you mean you need to drop the "attn:" you can do something like this
Select substring(Col1, 5,len(Col1)-5) + ' - ' + Col2
From dbo.{Table}
Where {Conditions}
Instead of hardcoding the index of ':', you can do something like this
SELECT REPLACE(RIGHT(Col1, (LEN(Col1) - CHARINDEX(':', Col1)-1)),' ',', ') + ' - ' + Col2
To test this you can run the following
DECLARE #column1string AS VARCHAR(30), #column2number AS VARCHAR(10)
SET #column1string = 'Attn: Firstname Lastname'
SET #column2number = '12345678'
SELECT REPLACE(RIGHT(#column1string, (LEN(#column1string) - CHARINDEX(':', #column1string)-1)),' ',', ') + ' - ' + #column2number

How to join two SQL queries?

Making two SQL queries into the same table
SELECT FirstName,
LastName,
LEFT(FirstName, 1) + '.' + LEFT(LastName, 1) AS Initial
FROM ContactUpdates
This outputs:
FirstName LastName Initial
I want this to join this other one on the right side where Initial ends
SELECT LOWER(LEFT(FirstName, 1) + REPLACE(LastName,'''','' ) )
+ '#email.com' AS Email
FROM ContactUpdates
In the end I want it to be
FirstName LastName Initial Email
But I can't figure out how to make them join any help?
SELECT FirstName,
LastName,
LEFT(FirstName,1) + '.' + LEFT(LastName,1) AS Initial,
LOWER(LEFT(FirstName, 1) + REPLACE(LastName, '''', '')) + '#email.com' AS Email
FROM ContactUpdates
Then simply append that column to the query:
SELECT FirstName
, LastName
, LEFT(FirstName,1) + '.' + LEFT(LastName,1) AS Initial
, LOWER(LEFT(FirstName,1) + REPLACE(LastName,'''','' ) ) + '#email.com' AS Email
FROM ContactUpdates

Combine First, Middle Initial, Last name and Suffix in T-SQL (No extra spaces)

I'm trying not to reinvent the wheel here...I have these four fields:
[tbl_Contacts].[FirstName],
[tbl_Contacts].[MiddleInitial],
[tbl_Contacts].[LastName],
[tbl_Contacts].[Suffix]
And I want to create a FullName field in a view, but I can't have extra spaces if fields are blank...
So I can't do FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix... Because if there is no middle initial or suffix I'd have 2 extra spaces in the field. I think I need a Case statement, but I thought someone would have a handy method for this...Also, the middleinitial and suffix may be null.
Assuming that all columns could be nullable, you can do something like:
RTrim(Coalesce(FirstName + ' ','')
+ Coalesce(MiddleInitial + ' ', '')
+ Coalesce(LastName + ' ', '')
+ Coalesce(Suffix, ''))
This relies on the fact that adding to a NULL value yields a NULL.
Whichever options you choose, here's something to think about: this will be a rather involved and thus time consuming option, especially if you have it in a view which gets evaluated each and every time for each and every row in question.
If you need this frequently, I would recommend you add this to your base table as a persisted, computed field - something like:
ALTER TABLE dbo.tbl_Contacts
ADD FullName AS (insert the statement of your choice here) PERSISTED
When it's persisted, it becomes part of the underlying table, and it's stored and kept up to date by SQL Server. When you query it, you get back the current value without incurring the cost of having to concatenate together the fields and determine which to use and which to ignore...
Just something to think about - something that too many DBA's and database devs tend to ignore and/or not know about....
You may want to pass the FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix concatenation through the REPLACE() function in order to substitute duplicate spaces into a single space.
REPLACE(FirstName + ' ' + MiddleInitial + ' ' + LastName + ' ' + Suffix, ' ', ' ')
-- -- -
EDIT:
Just noticed that some of your fields may be NULL, and therefore the above would not work in that case, as the whole string would become NULL. In this case, you can use the COALESCE() method as suggested by Thomas, but still wrapped it in a REPLACE():
REPLACE(RTRIM(COALESCE(FirstName + ' ', '') +
COALESCE(MiddleInitial + ' ', '') +
COALESCE(LastName + ' ', '') +
COALESCE(Suffix, '')), ' ', ' ')
Test:
SELECT REPLACE(RTRIM(COALESCE('John' + ' ', '') +
COALESCE('' + ' ', '') +
COALESCE('Doe' + ' ', '') +
COALESCE(NULL, '')), ' ', ' ')
-- Returns: John Doe
I had to join Firstname, Middlename, and Lastname. my challenge was to handle NULL values, used following code.
RTRIM(LTRIM(RTRIM(isnull(#firstname,'') + ' ' + isnull(#middlename,'')) + ' ' + isnull(#lastname,'')))
Test different scenarios if you are interested :)
DECLARE #firstname VARCHAR(MAX)
DECLARE #middlename VARCHAR(MAX)
DECLARE #lastname VARCHAR(MAX)
set #firstname = 'FirstName'
set #middlename = NULL
set #lastname = 'LastName'
SELECT '|'+RTRIM(LTRIM(RTRIM(isnull(#firstname,'') + ' ' + isnull(#middlename,'')) + ' ' + isnull(#lastname,'')))+'|'
--
If you are using SQL Server 2012+ you could use CONCAT and +:
SELECT RTRIM(
CONCAT(FirstName + ' ', MiddleInitial + ' ', LastName + ' ', Suffix)
) AS [FullName]
FROM tbl_Contacts;
How it works:
If any part of full name is NULL then NULL + ' ' → NULL
CONCAT handles NULL
In case that after part of name there are only NULLs, TRIM last space.
LiveDemo
Here is a solution:
CREATE FUNCTION dbo.udf_IsNullOrEmpty
(
#vchCheckValue VARCHAR(MAX)
,#vchTrueValue VARCHAR(MAX)
,#vchFalseValue VARCHAR(MAX)
)
RETURNS VARCHAR(MAX)
AS
BEGIN
RETURN CASE WHEN NULLIF(RTRIM(LTRIM(#vchCheckValue)),'') IS NULL THEN #vchTrueValue ELSE #vchFalseValue END
END
SELECT FirstName + ' ' +
dbo.udf_IsNullOrEmpty(MiddleInitial,'',MiddleInitial + ' ') +
LastName +
dbo.udf_IsNullOrEmpty(Suffix,'',' ' + Suffix)
FROM tbl_Contacts
select CONCAT(IFNULL(FirstName, ''),
'',
IFNULL(MiddleName, ''),
'',
IFNULL(LastName, '')) AS name
from table
Why not like this:
select concat(fName,' ',
case length(mName)
when 0 then ''
else concat(mName, ' ') end, lName) as fullName
My columns are not null, so this works for me.
create function getfname(#n varchar(30))
returns varchar(30)
as
begin
declare #s varchar(30)
set #s=LEFT(#n,charindex(' ',#n)-1)
return #s
end
create function getLname(#n varchar(30))
returns varchar(30)
as
begin
declare #s varchar(30)
set #s=substring(#n,charindex(' ',#n+1),Len(#n))
return #s
end
the query:
SELECT retire.employeehrmsid,
Isnull(retire.firstname, '') + ' '
+ Isnull(retire.middlename, '') + ' '
+ Isnull(retire.lastname, '') AS FullName,
retire.dojtoservice,
retire.designation,
emphistory.currentdoj,
emphistory.presentddo,
emphistory.office,
transfer.generatetid AS TransferID,
transfer.transferdate,
transfer.currentlocation,
transfer.newlocation,
transfer.datas AS Transfer_Doc,
release.generaterid AS ReleaseID,
release.releasedate,
release.datar AS Release_Doc,
employeeserviceupdate.dataeu AS Join_Doc
FROM retire
INNER JOIN emphistory
ON retire.id = emphistory.id
INNER JOIN employeeserviceupdate
ON retire.id = employeeserviceupdate.id
INNER JOIN transfer
ON retire.id = transfer.id
AND emphistory.ehrid = transfer.ehrid
INNER JOIN release
ON transfer.tid = release.tid