How to glue 3 rows in one string? - sql-server-2005

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

Related

Using ISNULL does not produce the expected results

I am trying to concatenate several columns of a person's name into one column and ultimately into a temporary table. I am unable to get the ISNULL function to work correctly.
I have tried using ISNULL to effectively say "if this column is blank, then just ignore it". I've read about a command called ISBLANK but that doesn't seem to work on my version of SQL Server.
SELECT
co.serialnumber, co.envelopesalutation,
co.title + ' ' + LEFT(co.firstname, 1) + ' ' + ISNULL(LEFT(co.otherinitial, 1), '') +
' ' + co.keyname + ' ' + ISNULL(co.POSTNOMINAL, '') [Correct]
INTO
TEMPENVSALUTATION
FROM
contact co
WHERE
co.contacttype = 'Individual'
AND co.title IN ('Mr', 'Mrs', 'Ms', 'Miss', 'Mx', 'Dr')
I expect, for example, that someone with co.title of Mr, a co.firstname of Jon, no co.otherinitial a co.keyname of Smith and no co.postnominal to appear in the TEMPENVSALUTATION table as Mr J Smith
What I'm actually getting is only people who have a co.otherinitial appearing e.g. Mr S D Smith.

Split String and Mix with another string in redshift

I have two columns one is the description and its corresponding tags in another column is a redshift table
description | Tags
John Plays Football | name, Verb , object
I want the output as description with tag
John name plays verb football object
The one more addition for this is where ever there is a description which contains colon ( : ) I would like to separate the words without removing :
For eg:
description | Tags
Des:John Plays Football | constant,name, Verb, object
Output
Des: constant John name plays verb football object
also need exclude : rule for numbers on both sides of colon to make sure time is getting (eg: 10:10) separated
I have no clue where I will start only
You have create UDF in Python to do this .
Sample code
create function f_py_conc (a srting, b srting)
returns srting
stable
as $$
c=a.split(" ")
d=b.split(",")
e =""
for i in range(len(c))
d=c[i]+" " +d[i]
return e
$$ language plpythonu;
Note: Python is dependent on space so may be this code can not run as it may need some change..
you can actually achieve this using Redshift functions, specifically the split part method. I am not sure if this will address the : as well but give this a go and then comment back with what you need to do next.
The idea is building a concatenated field assembling all the distinct parts of both fields. You can do this as follows:
select description, tags,
SPLIT_PART(description, ' ', 1) + ' ' + SPLIT_PART(tags, ',', 1) + ' ' +
SPLIT_PART(description, ' ', 2) + ' ' + SPLIT_PART(tags, ',', 2) + ' ' +
SPLIT_PART(description, ' ', 3) + ' ' + SPLIT_PART(tags, ',', 3)
as description_with_tag
from table_name
Find out more about this function here

SQL Select query to pick the field value which has more than one empty space?

In my LastName Column, I have either one name or two names. In some records, I have more than one empty space between the two names.
I will have to select the records which has more than one empty space in the field name.
declare #nam nvarchar(4000)
declare #nam1 nvarchar(4000)
set #nam = 'sam' + ' ' + 'Dev'
set #nam1 = 'ed' + ' ' + ' ' + 'Dev'
In the sample query, i expect the output value should be #nam1.
You can do this using LEN and REPLACE to replace the spaces from string and then get original length - replaced length and then check that in WHERE clause,
SELECT *
FROM
mytTable
WHERE
LEN(LastName)-LEN(REPLACE(LastName, ' ', '')) > 1

SELECT DISTINCT is omitting NULL values when not desired

I am trying to build a distinct string in a query, which works unless one of the values is NULL. I've tested removing LOCATION_ADDR_LINE_2, and the query will work just fine. When I do not SELECT DISTINCT, I find that LOCATION_ADDR_LINE_2 values are NULL. How can I gather these values in the SELECT DISTINCT even if NULL?
SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_ADDR_LINE_2 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Returns:
Addresses
NULL
SELECT DISTINCT(LOCATION_ADDR_LINE_1 + ', ' + LOCATION_CITY + ', ' + LOCATION_WORK_STATE) AS Addresses
FROM [OperationReport].[dbo].[OracleReport]
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%' OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Returns:
Addresses
1135 Auto...
189-199 Easy...
Assuming you don't mind text,,text,... (empty string) when a value is NULL...
SELECT DISTINCT(coalesce(LOCATION_ADDR_LINE_1,'') + ', ' +
coalesce(LOCATION_ADDR_LINE_2,'') + ', ' +
coalesce(LOCATION_CITY,'') + ', ' +
coalesce(LOCATION_WORK_STATE,'')) AS Addresses
FROM OracleReport
WHERE (LOCATION_ADDR_LINE_1 LIKE '%1135 Auto%'
OR LOCATION_ADDR_LINE_1 LIKE '%199 Easy%')
Coalesce will take the first non-null value and return it. It requires consistent data types and will early exit once the first non-null value in a series is encountered. (more details Oracle Differences between NVL and Coalesce)

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'