Removing spaces in concatenated string column in a view - sql

I have run the following script to create a view which provides the results I want:
SELECT ACTNUMBR_1, ACTNUMBR_2, ACTNUMBR_3, ACTNUMBR_1 + '-' + ACTNUMBR_2 AS Match_Account, ACTNUMBR_1 + '-' + ACTNUMBR_2 + '-' + ACTNUMBR_3 AS Full_Account_Number
FROM dbo.GL00100
but my data has gaps in the Match_Account and Full_Account_Number columns. See the output below:
I would like my account numbers to appear like - A100-000 OR A100-000-000.
What is the easiest way to remove all spaces on this view to achieve this?
Thanks,

Just use REPLACE to remove your spaces:
SELECT ACTNUMBR_1, ACTNUMBR_2, ACTNUMBR_3,
REPLACE(ACTNUMBR_1 + '-' + ACTNUMBR_2, ' ', '') AS Match_Account,
REPLACE(ACTNUMBR_1 + '-' + ACTNUMBR_2 + '-' + ACTNUMBR_3, ' ', '') AS Full_Account

Related

SQL subtracting a string column from another string column

I am looking to subtract two string columns from another string column.
I have acheived this in the past in Oracle using the below
SELECT
C.MANUFACTURER,
C.MODEL_GROUP,
REGEXP_REPLACE(C.VARIANT, '^'||C.MANUFACTURER || ' +' || C.MODEL_GROUP) "VAR DESC",
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C
I now need to acheive the same in MS SQL any suggestions would be appreciated.
"Subtract" is surely a misnomer here. The OP appears to want to remove a string at the beginning of another string.
From this point on, I am assuming that MANUFACTURER and MODEL_GROUP do not contain any regular expression wildcard characters.
There is a challenge with multiple spaces. If there are not too many, then you can do. For instance, this handles one or two spaces:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%'
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
WHEN C.VARIANT LIKE C.MANUFACTURER + ' ' + C.MODEL_GROUP + '%' AND
THEN STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER + ' ' + C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
EDIT:
Here is a more complete solution:
SELECT C.MANUFACTURER, C.MODEL_GROUP,
(CASE WHEN C.VARIANT LIKE C.MANUFACTURER + ' %' + C.MODEL_GROUP + '%' AND
C.VARIANT NOT LIKE C.MANUFACTURER + ' %[^ ]%' + C.MODEL_GROUP + '%'
THEN STUFF(LTRIM(STUFF(C.VARIANT, 1, LEN(C.MANUFACTURER), '')), 1, LEN(C.MODEL_GROUP), '')
ELSE C.VARIANT
END) as [VAR DESC],
C.VARIANT
FROM STD_BI.RL2_CONTRACTS_VW C;
This tests for just spaces between the two strings (the not like is checking for something other than a space). The replacement is then in three steps -- remove the manufacturer, then the spaces, then the model group.
I have spent some time on this one as much for my own satisfaction and in addition to the answer above, I have come up with the following which also generates the correct answer.
SUBSTRING(FV.VARIANT, LEN(FV.MANUFACTURER + ' ' + FV.MODEL_RANGE + ' ') + 1 , LEN(FV.VARIANT))
My problem was kinda easier to solve, I just needed to remove "xxxx" (the first 4 characters) at the start of a column from some rows:
UPDATE coluna SET columToEdit = SUBSTRING(columToEdit, 5, 200);

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)

Absolute value of varchar from table

I have a field with value= '''VALUE'''.when i am selecting it return me 'VALUE' with single quotes.
I want the VALUE in a varchar variable without quotes.how to achieve this without replace function.
THIS IS MY DYNAMIC QUERY WHICH IS IN THE TABLE
''''+'SELECT'+''''+'+'+''''''''''+'+ RTRIM(#start_Insert) +'+''' ''''+'''+'+ '+'''''''('''+' + RTRIM(#Column_List_Insert) +'+'''''''+'''+'+' +''''''')''''''+'+
''' ''''+''+ ''''''(''+ RTRIM(#Column_List_Insert) + ''''''+''+'''''')''''''+'+
''' + '''' VALUES(''''+ '' +#Actual_Values_Insert+ ''+'''')''''''+'' ''+' +
''' FROM ''+ ''['' +RTRIM(#source) + '']''+ '+
'''where (<col_name> BETWEEN ''''<col_value1>'''' AND ''''<col_value2>'''') AND <col_name3> <> ''''<col_value3>'''''''
How about using SUBSTRING(), and removing the leading and trailing single apostrophes?
SELECT SUBSTRING(field, 2, LEN(field)-2) FROM table
As #RahulTripathi alluded in his comment, I would prefer to just REPLACE() out the apostrophes:
SELECT REPLACE(field, '''', '') FROM table

How to CONVERT/CAST a column in sql query

can someone help with this. I'm having a problem with this sql query.
I need to convert price1_split to int. But it seems it also gets the concatenation i made beside it.
SELECT product_number,product_name,description,
price1+ ' ' + CONVERT(INT, price1_split) + '% |' +
price2+ ' ' + CONVERT(INT, price2_split) + '% |' +
price3+ ' ' + CONVERT(INT, price3_split) + '%' as price_split
from tbl_products
error msg says:
Conversion failed when converting the varchar value '% |' to data type int.
Since you ultimately need a string value separated by % |, you should not be casting these to integers at all. You would need to cast them to INT if you were adding the values together, but you are not adding them, you're concatenating them onto strings. Assuming they are already string (CHAR, VARCHAR) values, just concatenate them onto the other character elements.
SELECT product_number,product_name,description,
price1+ ' ' + price1_split + '% |' +
price2+ ' ' + price2_split + '% |' +
price3+ ' ' + price3_split + '%' as price_split
from tbl_products
If however, they are float values you are trying to truncate to integers, you can CONVERT() them to INT and then CONVERT() them back to strings to concatenate. In this case, it would probably be better to use FLOOR() for the truncation as in CONVERT(VARCHAR(n), FLOOR(price1_split))
-- Using CONVERT(INT) for truncation
SELECT product_number,product_name,description,
price1+ ' ' + CONVERT(VARCHAR(32), CONVERT(INT, price1_split)) + '% |' +
price2+ ' ' + CONVERT(VARCHAR(32), CONVERT(INT, price2_split)) + '% |' +
price3+ ' ' + CONVERT(VARCHAR(32), CONVERT(INT, price3_split)) + '%' as price_split
from tbl_products

SQL Query to concatenate strings or add default value

I have to create a View that shows a field created by concatenating some other fields. The simple query that I use is this one:
SELECT
CODPROY, DESCPROY, USER, CODPROY + ' - ' + USER + ' - ' + DESCPROY AS Expr
FROM
dbo.PROY
The problem is that USER may be NULL and in this case I have to insert a default text, something like 'NOT AVAILABLE'. Can this be done in a view?
Use coalesce if you are using SQL Server
SELECT
CODPROY,
DESCPROY,
USER,
CODPROY + ' - ' + COALESCE(USER,'NOT AVAILABLE') + ' - ' + DESCPROY AS Expr
FROM dbo.PROY
Yes, it can be done. You need to use IFNULL (mySQL) or something like it ..
CREATE VIEW foobar as
SELECT CODPROY
, DESCPROY
, USER
, CODPROY + ' - ' + IFNULL(USER,'DEFAULT') + ' - ' + DESCPROY AS Expr
FROM dbo.PROY;