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.
Related
I have a query that returns some demographics like firstName, lastName, MiddleName and i need to use LEFT function on each to filter the First Letter of each column like LEFt(firstName, 1).This is working fine when each column is not a null value. when it is null value
select otherColumns, LEFT(sub.LastName, 1) + ',' + LEFT(sub.FirstName, 1) + ' ' + LEFT(sub.MiddleName, 1) as patientInitials from <table> <inner joins> <some where conditions>;
But when one of demographics like middleName is null and other firstName, lastName are not null , patientInitials are evaulating to NULL, not sure why?
I resolved my issue by adding COALESCE
LEFT(sub.LastName, 1) + ',' + LEFT(sub.FirstName, 1) + ' ' + COALESCE((LEFT(sub.MiddleName, 1)),'') as patientInitials
But is there any other good way to check for notNull on the LEFT function ??
Help Appreciated!
But is there any other good way to check for notNull on the LEFT function ??
CONCAT function ignores NULLs:
SELECT CONCAT(LEFT(sub.LastName, 1), ',' ,
LEFT(sub.FirstName, 1),
' ' + LEFT(sub.MiddleName, 1)) patientInitials
FROM tab;
' ' + LEFT(sub.MiddleName, 1)) using ' ' will remove leading space in case if Middle Name is NULL.
The CONCAT_WS function also has a similar function:CONCAT_WS (Transact-SQL)
I have a table called Configuration. It contains the values like below,
Id SourceColumns TargetColumns SourceTable TargetTable
1 Name, Age CName, CAge STable TTable
2 EId EmplId EmpTable TTable
In a stored procedure, I have to get the column names from the above table and I have to compare the source table and target table.
I am able to do that easily for the 2nd record as it has only one column name, so in the where clause I can write sourcecolumn = targetcolumn like,
SELECT
EId
, EmplId
FROM
EmpTable E
JOIN TTable T ON E.Eid = T.EmplId
The first record in the table has 2 columns separated by comma (,).
I have to compare like this,
SELECT
Name
, Age
FROM
STable S
JOIN TTable T ON S.Name = T.CName AND S.Age = T.CAge
In some cases the source columns and target columns may have more column names separated by comma(,)
Please help me on this.
As I don't know whether you have completely understood the data model I suggested in the request comments and in order to properly answer the question:
Your table is not normalized, as the data in the columns SourceColumns and TargetColumns is not atomic. And one even has to interpret the data (the separator is the comma and the nth element in one column relates to the nth element in the other column).
This is how your tables should look like instead (the create statements are pseudo code):
create table configuration_tables
(
id_configuration_tables int,
source_table text,
target_table text,
primary key (id_configuration_tables),
unique key (source_table),
unique key (target_table) -- or not? in your sample two souce table map to the same target table
);
create table configuration_columns
(
id_configuration_columns int,
id_configuration_tables int,
source_column text,
target_column text,
primary key (id_configuration_columns),
foreign key (id_configuration_tables) references configuration_tables (id_configuration_tables)
);
Your sample data would then become
configuration_tables
id_configuration_tables | source_table | target_table
------------------------+--------------+-------------
1 | STable | TTable
2 | EmpTable | TTable
configuration_columns
id_configuration_columns | id_configuration_tables | source_column | target_column
-------------------------+-------------------------+---------------+--------------
1 | 1 | Name | CName
2 | 1 | Age | CAge
3 | 2 | EId | EmplId
As of SQL Server 2017 you can use STRING_AGG to create your queries is. In earlier versions this was also possible with some STRING_AGG emulation you will easily find wit Google or SO.
select
'select s.' + string_agg (c.source_column + ', t.' + c.target_column, ', ') +
' from ' + t.source_table + ' s' +
' join ' + t.target_table + ' t' +
' on ' + string_agg('t.' + c.target_column + ' = s.' + c.source_column, ' and ') +
';' as query
from configuration_tables t
join configuration_columns c on c.id_configuration_tables = t.id_configuration_tables
group by t.source_table, t.target_table
order by t.source_table, t.target_table;
Demo: https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=8866b2485ba9bba92c2391c67bb8cae0
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
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....
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.