I am working in SQL data base. I have to make two columns. The first column is the length of the employee name(I have that) The second column is title of courtesy and then the last name of employee.
My problem is how do i add the case function to have Ms displayed as miss, mr. as mister and dr as doctor. I do not understand. Any help would be appreciated. Here is the code i have so far:
SELECT 'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name]
, TitleOfCourtesy + LastName AS Title
FROM dbo.Employees
You can do something like this:
select
... your previous things here ...
case TitleOfCourtesy
when 'Mr' then 'Mister '
when 'Ms' then 'Miss '
when 'Dr' then 'Doctor '
else '' end + LastName as Title
try this
SELECT 'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name],
CASE
WHEN TitleOfCourtesy = 'Ms' THEN 'Miss '
WHEN TitleOfCourtesy = 'Mr' THEN 'Mister '
WHEN TitleOfCourtesy = 'dr' THEN 'Doctor ' END + LastName AS Title
FROM dbo.Employees
Thanks all I fixed it I had to add the period after like mr.
SELECT 'Your full name is ' + CAST(LEN(FirstName) + LEN(LastName) AS VARCHAR(12)) + ' character(s).' AS [Length of Employee Name]
, CASE TitleOfCourtesy
WHEN 'Mr.' THEN 'Mister '
WHEN 'mrs. ' THEN 'Miss'
WHEN 'Ms.' THEN 'Miss '
WHEN 'Dr.' THEN 'Doctor '
ELSE '' END
+ TitleOfCourtesy + LastName
AS title
FROM dbo.Employees
Related
how to use union in a loop I was trying to union the results thanks
DECLARE #STACODE INT;
SET #STACODE = 001
WHILE #STACODE <= 095
BEGIN
SELECT
DIVCODE,
STACODE,
EMPNO,
LNAME +', '+ FNAME + ' '+MI EMPNAME,
POLICYNO,
EFFMM +'/'+EFFYY EFDATE,
TERMM +'/'+TERYY TERDATE,
DEDAMT
FROM TBL_LISTING_NCR
WHERE DIVCODE='089'
AND STACODE= #STACODE
UNION ALL
SELECT
'STATION TOTAL'DIVCODE,
' 'STACODE,
' ' EMPNO,
' ' EMPNAME,
' 'POLICYNO,
' ' EFDATE,
' ' TERDATE,
SUM(DEDAMT)
FROM TBL_LISTING_NCR
WHERE DIVCODE='089'
AND STACODE= #STACODE
--UNION ALL
SET #STACODE = #STACODE + 1
END
In SQL we don't use loops like in a programming language where we must tell the machine how to do things. In SQL we tell the machine what data to select. In your case you want the rows for the codes 1 to 95 plus one row for the dedamt sum.
Here is a solution with UNION ALL:
SELECT
divcode,
stacode,
empno,
lname + ', '+ fname + ' ' + mi AS empname,
policyno,
effmm + '/' + effyy AS efdate,
termm + '/' + teryy AS terdate,
dedamt
FROM tbl_listing_ncr
WHERE divcode = '089'
AND stacode BETWEEN 1 AND 95
UNION ALL
SELECT
'STATION TOTAL' AS divcode,
' ' AS stacode,
' ' AS empno,
' ' AS empname,
' ' AS policyno,
' ' AS efdate,
' ' AS terdate,
SUM(dedamt) AS dedamt
FROM tbl_listing_ncr
WHERE divcode = '089'
AND stacode BETWEEN 1 AND 95
ORDER BY
stacode,
CASE WHEN divcode = 'STATION TOTAL' THEN 2 ELSE 1 END;
My objective, is to select the first available address. If there isn't a AddressCity, then I should select the AddressRegion, and if there is not a AddressRegion I should select the AddressCountry.
IF AddressCity IS NOT NULL
SELECT AddressName + ' is from ' + AddressCity
ELSE
IF AddressRegion IS NOT NULL
SELECT Address+ ' is from ' + AddressRegion
ElSE
IF AddressCountry IS NOT NULL
SELECT AddressName + ' is from ' + AddressCountry
FROM DBO.Address
When I execute it, I get Invalid column name 'AddressCity'
You want something like case or coalesce():
SELECT coalesce(AddressName + ' is from ' + AddressCity,
AddressName + ' is from ' + AddressRegion
AddressName + ' is from ' + AddressCountry
)
FROM DBO.Address;
I am trying to combine FirstName, MiddleName, LastName then a comma and suffix.
An example here describes this:
Sno. FirstName MiddleName LastName Suffix Result
---------------------------------------------------------------
1. ROBERT NULL SMALLWOOD NULL ROBERT SMALLWOOD
2. KIRK NULL ROBERTS MR KIRK ROBERTS, MR
3. WILLIAM DARRELL WATTENBARGER Jr. WILLIAM DARRELL WATTENBARGER, MR
If there is no Suffix, then comma (,) should not be appended also there should be only one space after every column.
So far I tried
REPLACE(RTRIM(Coalesce(FirstName + ' ', '') + Coalesce(MiddleName + ' ', '') + Coalesce(LastName + ' ', '') + Coalesce(SuffixId + ' ', '')),' ',' ')
Now how should I add a comma before suffix in case if it only exists without using case.
Thanks
You can use CONCAT:
SELECT CONCAT(FirstName,' ',MiddleName + ' ' ,LastName,', '+NULLIF(Suffix,'')) [Result]
FROM dbo.YourTable;
Use ISNULL:
SELECT ISNULL(FirstName + ' ', '') + ISNULL(MiddleName + ' ', '') + ISNULL(LastName + ' ', '') + ISNULL(', ' + SuffixId, '')
FROM My_Table_Name
This is the solution I came up with, please try and see if that worked for you.
create table #FullNameCalculation
(
Sno int not null,
FirstName varchar(10),
MiddleName varchar(10),
LastName varchar(15),
Suffix varchar(5)
)
insert into #FullNameCalculation
(Sno,FirstName,MiddleName,LastName,Suffix)
values
(1, 'ROBERT', NULL,'SMALLWOOD', NULL),
(2, 'KIRK', NULL, 'ROBERTS', 'MR'),
(3,'WILLIAM', 'DARRELL', 'WATTENBARGER', 'JR.'),
(4,NULL,'BARBER','SINK','MS'),
(5,NULL,NULL,'SANDERS','MRS.'),
(6,'SARA',' D','WILLIAMS ',' MS');
SELECT * FROM #FullNameCalculation;
SELECT *,
RTRIM(LTRIM(CONCAT(RTRIM(LTRIM(FirstName)),' ',
RTRIM(LTRIM(MiddleName)),' ',
RTRIM(LTRIM(LastName)),
iif(RTRIM(LTRIM(Suffix)) IS NULL OR RTRIM(LTRIM(Suffix))='','',', '+RTRIM(LTRIM(Suffix)))))) AS FullName
FROM #FullNameCalculation;
I wanted to do something similar and I used COALESCE like OP. Using the suggestion from Mark Adelsberger my query looked like this:
SELECT
RTrim(Coalesce(FirstName + ' ', '')
+ Coalesce(MiddleName + ' ', '')
+ Coalesce(LastName + ' ', '')
+ Coalesce(', ' + Suffix, '')) FullName
FROM dbo.YourTable
I have a select statement that strings the name together. The issue is that I want to put a period after the middle initial but when it is blank the period is after the firstname. Here is the select
select LTrim(RTrim(FirstName + ' ' + Left(MiddleName,1))) + '. ' + LastName as FullName,
Just add a case
select LTrim(RTrim(FirstName + ' ' + case when isnull(MiddleName,'')='' then else Left(MiddleName,1))) + '. ' end + LastName as FullName,
You could test for the existance of the MiddleName and only include your period if its not null
SELECT
LTRIM(RTRIM(FirstName + ' ' + IIF(ISNULL(MiddleName,'')<>'',LEFT(MiddleName,1) + '.', ''))) + ' ' + LastName as FullName
Use Case statement to check if the string is not empty
select LTrim(RTrim(FirstName + ' ' + Left(MiddleName,1))) +
case when Left(MiddleName,1) <> '' then '. ' else '' end + LastName as FullName,
Here is my SQL as I have it now. The first and last name should never be missing because they are required on the interface with validation. However, if the middle initial is missing, I don't want two spaces in the result; I just want one space. I could put a case statement in, but that seems like overkill if SQL already has a function for this purpose. Does anyone know if there is a function for this purpose? What would the code look like?
SELECT ISNULL(contact.firstname, '')
+ ' '
+ ISNULL(contact.middleinitial, '')
+ ' '
+ ISNULL(contact.lastname, '')
FROM dbo.contact
SELECT
ISNULL(contact.firstname, '') +
ISNULL(' ' + contact.middleinitial + ' ', ' ') +
ISNULL(contact.lastname, '')
However, you either should remove ISNULL for firstname, lastname (defined NOT NULL?) or add some trims
SELECT
LTRIM(RTRIM(
ISNULL(contact.firstname, '') +
ISNULL(' ' + contact.middleinitial + ' ', ' ') +
ISNULL(contact.lastname, '')
))
In SQL Server 2012 and up you can use CONCAT function:
SELECT CONCAT(contact.firstname, ' ' + contact.middleinitial, ' ' + contact.lastname)
FROM dbo.contact
If middleinitial is null then appending space to null value will result in null value and CONCAT will skip this null value during concatenation.
SELECT REPLACE(ISNULL(contact.firstname, '') + ' ' + ISNULL(contact.middleinitial, '') + ' ' + ISNULL(contact.lastname, '')
,' ',' ')
FROM dbo.contact