Does Space Take Up a Character Count in SQL Server - sql

I have 3 columns:
Name nvarchar 100
FirstName nvarchar 50
LastName nvarchar 50
Can Name take in FirstName + ' ' + LastName if both are maxed out (50 characters)?

Can Name take in FirstName + ' ' + LastName if both are maxed out (50 characters)?
No.
If FirstName and LastName actually use the entire 50 char column width, there's no room for the space, and the new string containing the space won't fit.
OTOH, there's really no reason to store first and last then another column containing them both glued together. This could easily be done in the SQL query as needed with SELECT Firstname + ' ' + LastName as [Name] FROM TableName
In fact, it's better to not store first + space + last because once you get two copies of any chunk of data, eventually one is going to be wrong. Someone will have a name change and the "Name" field won't get updated.

Related

SQL Server : separate first and last name and remove middle initial into just two columns

I have a column that has an individuals full name, including the middle initial. I am trying to separate the full name into just first name and last name and eliminating the middle name/initial. Some of the names in my database have a middle name/initial and some don't. The following query's are what I am using and they both do only half the trick.
Query #1: returns the first name and middle name/initial in the 2nd column and eliminates the last name:
FirstName = LEFT(fullname, CHARINDEX(' ', fullname)),
LastName = RIGHT(fullname, CHARINDEX(' ', REVERSE(fullname)))
Query #2: returns the first name and combines the middle name/initial with the last name (with a space between the two):
FirstName = left(fullname,CHARINDEX(' ',fullname)),
LastName = SUBSTRING(fullname, CHARINDEX(' ',fullname)+1,LEN(fullname)-(CHARINDEX(' ',fullname)-1))
How about this?
select left(fullname, charindex(' ', fullname + ' ') - 1) as firstname,
right(fullname, charindex(' ', reverse(fullname) + ' ') - 1) as lastname
Note this handles names with no spaces without giving an error. However, the first and last name are the same (the full string). It is unclear what you want to do in this case.

How to glue 3 rows in one string?

In table pics I have 3 different rows, where surname, name and middle name are stored.
In query I need to glue them to one string:
pics.e_family + ' ' + pics.e_name + ' ' + pics.e_sname AS fio
All works perfectly, but one entry don't have middle name at all (no mistake, its really so). And in fio I get NULL. Is there any possibility to modify code, or make additional check if one or more of the rows are null, replace it with space symbol or just show remain rows?
If you mean columns instead of rows, you can use COALESCE or ISNULL:
pics.e_family + ' ' + COALESCE(pics.e_name, '') + ' ' + pics.e_sname AS fio

joining two columns, display in merged columns becomes null

I have a query that I am going to use in one of my stored procs. Specs dictate that, in the table, there should are 3 parts of an address, but there are going to be merged into one column: AddressLine1 which is the Street, AddressLine2 which is the Subdivision/, and AddressLine3 which is the City/State/Province. out of all these 3 fields, only AddressLine1 is mandatory, the other 2 can be left NULL. here is my code joining the 3 address fields into one:
(tb_TransactionName.AddressLine1 + ' ' + tb_TransactionName.AddressLine2 + ' ' + tb_TransactionName.AddressLine3) as 'Address'
Given that, I have entered text in the AddressLine1. but whenever I run the query, the Address column displays NULL. The moment i don't add up the 3 address fields and display only AddressLine1, the text under the AddressLine1 column appears in the results. For other info, the table tb_Transaction is left-joined to another table tb_TransactionDetails:
FROM (tb_TransactionType inner join tb_TransactionDetails
on tb_TransactionType.TxnTypeCode = tb_TransactionDetails.TxnType)
LEFT JOIN tb_TransactionName on tb_TransactionDetails.TxnID = tb_TransactionName.TxnID
any idea what could be the possible problem as to why the merged column displays NULL?
I bet one of the columns you are joining are with NULL value, which makes the result NULL.
try this:
(tb_TransactionName.AddressLine1 + ' ' + ISNULL(tb_TransactionName.AddressLine2, '') + ' ' + ISNULL(tb_TransactionName.AddressLine3, '')) as 'Address'
(ISNULL(tb_TransactionName.AddressLine1,'') + ' ' +
ISNULL(tb_TransactionName.AddressLine2,'') + ' ' +
ISNULL(tb_TransactionName.AddressLine3,'') as 'Address'

Obtain first name, last name with unknown number of space

I'm new in VB.NET programming but I have to do a research bar in my program. Actually, the search bar is looking for first name or for last name (If first name or last name have space it's working) but if I want to search for both, how can I build my sql query? I don't know if the user will put first name first or last name first. What I've tried is to split the names if there is a space but if the person have a name like 'Robert Downey Junior', how can I know which part of the space will be the first name or the last name?
So,
how can I build my sql query for this task?
how can I know which part of the space will be the first name or the last name?
Actual regex
' value of query = "% + search bar + %" for sql LIKE parameter
Regex.IsMatch(query, "^'%(\w+ \w+)+%'$")
How I detect the first and last names
Dim name As String = query.ToString.Substring(2, query.Length - 4)
Dim nameParts As String() = name.Split(" ")
'So nameParts(0) = "robert", nameParts(1) = "downey" and namePart(2) = "junior"
Actual sql selector
SELECT id, firstName, lastName
FROM beneficiaire
WHERE LOWER(lastName) LIKE '%robert downey junior%'
OR LOWER(firstName) LIKE '%robert downey junior%'
CREATE PROCEDURE SEARCH( #SearchName VARCHAR(50)) AS
DECLARE #name VARCHAR(52)
SET '%' + #SearchName + '%'
SELECT id,
firstName,
lastName
FROM beneficiaire
WHERE (
LOWER(lastName) + ' ' + LOWER(firstName) LIKE #name
) OR
(
LOWER(firstName) + ' ' + LOWER(lastName) LIKE #name
)

MS SQL: Retrieving "Varchar" data from 3 columns and combine them together

I have three columns in my sql table, "FirstName", "MiddleName","LastName". When retrieving, I need to display these 3 together, for an example
FirstName = "John"
MiddleName = "Ned"
LastName = "Carter".
On retrieving, these should be displayed as "John Ned Carter".
I tried the following
select FirstName+MiddleName+LastName from PhoneData
There is a problem!!! There are number of Names which the middle name is NULL. There are number of names which the last name is NULL, and so on. This is not retrieving those!!! It simply retrieve names where all the fields are not null!!!! If at least one column is null for a particular name, then it shows the whole name as NULL!!! For an example,
FirstName = "John"
MiddleName = NULL
LastName = NULL
on retrieval, the out put is ' NULL ' , not "John"
Please help!
Try the ISNULL() function around each field. Then you can set a value for when the selected value is null.
Like this;
select ISNULL(FirstName, '') + ISNULL(MiddleName, '') + ISNULL(LastName, '') from PhoneData