How to combined first_name and last_name using sql query? - sql

There are two columns in database as name FIRST_NAME and LAST_NAME.i want to join both the name and display in single column. The query which is used by me is given below but it gives error as Incorrect syntax near 'NAME'.
Modify the query:
SELECT [CREATED_ON], MUD.PK_ID AS USER_ID,
(MUD.FIRST_NAME + ' ' + MUD.LAST_NAME NAME)
AS NAME FROM USER_TABLE

Remove the erroneous 'NAME'. e.g.
SELECT [CREATED_ON], MUD.PK_ID AS USER_ID, (MUD.FIRST_NAME + ' ' + MUD.LAST_NAME)
AS NAME FROM USER_TABLE

SELECT
[CREATED_ON],
MUD.PK_ID AS USER_ID,
(MUD.FIRST_NAME + ' ' + MUD.LAST_NAME) AS [NAME]
FROM
USER_TABLE
The parentheses around the expression MUD.FIRST_NAME + ' ' + MUD.LAST_NAME are optional.

Related

SQL Group by with string concat and CASE statements

I am trying to understand SQL Group By concept along with the Select statement having:
String concat operation
CASE statement
I understand the Select clause is evaluated after the Group by clause and that any non aggregated columns in the Select list also need to be present in the Group By list. I will try to explain my questsion with an example ORDERS table that has one record for each order:
SELECT FIRSTNAME +' '+LASTNAME
,EMP_TITLE
,EMAILADDR
,INACTIVE
,CASE WHEN ISNULL(EMAILADDR,'')='' THEN 'ADMIN#MYCOMPANY.COM' ELSE EMAILADDR END AS ALTEMAIL
,CASE WHEN AGE>60 THEN 'SENIOR' ELSE 'EMP' END AS EMPTYPE
,COUNT(*) AS TOTAL_ORDERS
FROM ORDERS
GROUP BY FIRSTNAME + ' '+ LASTNAME
,EMP_TITLE
,EMAILADDR
,INACTIVE
,CASE WHEN ISNULL(EMAILADDR,'')='' THEN 'ADMIN#MYCOMPANY.COM' ELSE EMAILADDREND
,CASE WHEN AGE>60 THEN 'SENIOR' ELSE 'EMP' END
I understand the Select clause is evaluated after the Group by clause. What I am confused/trying to get my head around is - in what situations do we use the column name vs the contents from the select statement (concat/case in this example) in the group by clause?
Example: In group by above, we could get rid of FIRSTNAME + ' '+ LASTNAME and replace it with FIRSTNAME,LASTNAME. Also we could replace the CASE statements with the EMAILADDR, AGE.
Question:
I am trying to understand in which situations this is OK to do and in which situations is it absolutely necessary that we must place the CASE statement (or the concat string operation) from select clause into the Group By clause?
In situations where it is OK to use either, what is the best practice to follow? Use the case statement as is from select in the group by? Or only use column names involved in the case statement?
The GROUP BY clause defines expressions that can be referenced without aggregation in the SELECT. So, this is allowed:
SELECT FIRSTNAME + ' ' + LASTNAME
. . .
GROUP BY FIRSTNAME, LASTNAME
because FIRSTNAME and LASTNAME can be referenced without aggregation functions. So, any expressions are allowed as well.
On the other hand, this is not allowed:
SELECT FIRSTNAME, LASTNAME
. . .
GROUP BY
In this case, FIRSTNAME and LASTNAME are not in the GROUP BY, so they are not allowed. This is also allowed:
SELECT HONORIFIC + ' ' + FIRSTNAME + ' ' + LASTNAME
. . .
GROUP BY FIRSTNAME + ' ' + LASTNAME, HONORIFIC
However, this is not allowed:
SELECT HONORIFIC + ' ' + FIRSTNAME + ' ' + LASTNAME
. . .
GROUP BY FIRSTNAME + ' ' + LASTNAME + HONORIFIC

Manipulating duplicate values?

I have a table, with an ID, FirstName & Lastname.
I'm selecting that using the following query:
SELECT USER_ID as [ID], First_P + ' ' + Last_P as FullName FROM Persons
It's working fine. I'm basically having a list of ID's and full names.
Full names could be the same. How is it possible for me to find them and add the ID on the Full name cell as well? only when the names are the same.
Example:
1 John Wick (1)
50 John Wick (50)
I haven't found any similar questions to be honest, at least not for MSSQL. So If there are any, feel free to link me.
please take a look my answer. I used nested query to identify number of duplicated names
SELECT
ID,
IIF(NUMBEROFDUPS =1, NAME, CONCAT(NAME, ' (', ID, ')')) AS NAME
FROM
(
SELECT
ID,
CONCAT(First_P, ' ', Last_P) AS NAME,
COUNT(*) OVER (PARTITION BY First_P,Last_P) AS NUMBEROFDUPS
FROM
Table1
) tmp;
You can use outer apply to group the items via First_P + ' ' + Last_P
and then add case for multiple items.
The select stuff should look like:
SELECT USER_ID as [ID], p1.First_P + ' ' + p1.Last_P + case when cnt.FullName is not null
then '(' + p2.[sum] + ')' else '' end as FullName FROM Persons p1
outer apply (select First_P + ' ' + Last_P as FullName,count(1) as [sum]
from Persons p2
where p2.First_P + ' ' + p2.Last_P = p1.First_P + ' ' + p1.Last_P
group by First_P + ' ' + Last_P
having count(1) > 1) cnt

Merge and Duplicates

I have this Address table which has multiple columns. I want to concatenate the street number and street name column as Address and check if the address is unique.
SELECT ( street_num + ' ' + street_name ) AS Addr
FROM [propertyaddress_workfinal] AS t1
INNER JOIN (SELECT ( street_num + ' ' + street_name ) AS A2
FROM [propertyaddress_workfinal]) AS t2
ON t1.addr = t2.a2
WHERE Count(*) > 1
I get and error stating invalid column name 'Addr'.
Doing something silly here.
This is the way to do it
SELECT street_num + ' ' + street_name AS Addr
FROM [propertyaddress_workfinal]
GROUP BY street_num + ' ' + street_name
HAVING Count(*) > 1
Though if the concatenated space isn't integral to your duplicate matching
GROUP BY street_num, street_name
Will perform better if you have a composite index on those two columns.

Add selected values from multi column into one column separated by ','

I want to select values from multiple columns into one column. I have 2 separate columns from which i want to get name,address,state,zip in following format in SQL Server 2008
Name (new line)
address,state,zip
Name (new line)
address,state,zip
Query:
select
name + char(13) + concat(address,',', state,',', zip)
from
tbl1
join
tbl2 on....
I am not able to get the desired output. I get concat is not a recognized built in function name.
You could use + operator and cast the zip field as varchar directly like this:
For example:
select 'Dara Singh' + char(13) + '1234 Main Street' + ',' + 'NY' + ','
+ cast(95825 as varchar(10))
This is how your query would look:
select name + char(13) + [address] + ',' + [state] + ',' + cast([zip] as varchar(10))
from tbl1 join tbl2 on....

SQL Server Inner Query Problem

I have a very simple query in SQL Server. But it is giving errors.
select * from(
select emp_name + ' ' + emp.surname as employee from ca_contact
)
This query is not working.
But when I write like the below, it is working:
select emp_name + ' ' + emp.surname as employee from ca_contact
You'd need an alias. In this case foobar
select * from
(select emp_name + ' ' + emp.surname as employee from ca_contact) foobar
I think you need to specify table alias --
select * from(
select emp_name + ' ' + emp.surname as employee from ca_contact
) t1
In SQL Server, all derived tables must be given an alias [exception is if do not select anything from them, e.g. in an IN/EXISTS clause]. An alternative for what you are doing from SQL Server 2005 onwards is to use a Common Table Expression, which incidentally is also now available in recent versions of Oracle.
A simple meaningless alias
select * from
(select emp_name + ' ' + surname as employee from ca_contact) [ ]
A Common Table Expression, also naming the column at the sametime
;WITH [ ](employee) AS (
select emp_name + ' ' + surname
from ca_contact)
select * from [ ]
FWIW, you can omit the CTE column names and derive them from the query
WITH [ ] AS (
select emp_name + ' ' + surname as employee
from ca_contact)
select * from [ ]
Note: Not sure how you can have emp.surname since there is no table/alias emp defined in your query
Please try the below query
select employee from (select emp_name + ' ' + emp.surname as employee from ca_contact) as test