how to use substring and concat operation together - sql

i am trying to run a query but it gives me error on concat of missing expression. i am building an insert query where one columne values are inserted from another table by concatenating the two columns and getting first letter from first column and whole value from second column.
Heres my query.
SQL> INSERT INTO MSGG_USER (USERNAME)
SELECT substr (GIVEN_NAME, 1,1)GIVEN_NAME, || '' || SURNAME
FROM MSGG_PEOPLE;

You don't need the second mention of GIVEN_NAME, just concatenate the call to SUBSTR() directly:
INSERT INTO MSGG_USER (USERNAME)
SELECT SUBSTR(GIVEN_NAME, 1, 1) || SURNAME;
Off the top of my head, it looks as though you might have been trying to alias the substring call with GIVEN_NAME. In any case, you would not need any aliases in your select statement, as the columns are directly feeding into an insert operation.
Edit:
If you want to insert the first initial and last name in all lowercase, you can try wrapping these terms in LOWER:
INSERT INTO MSGG_USER (USERNAME)
SELECT LOWER(SUBSTR(GIVEN_NAME, 1, 1)) || LOWER(SURNAME);

Related

Why won't SQL return a value if I use "=" but will return if I use "like"?

Here are my queries:
(Won't return a value)
select * from T_VoucherHeaderEntry
where Vhe_VoucherNo = 'APV-1808-00160'
(Will return a value)
Select * from T_VoucherHeaderEntry where Vhe_VoucherNo like 'APV-1808-00160%'
I tried trimming my first query but it doesn't work.
You appear to have other control characters in your stored data, specifically carriage-return and line-feed. This highlights the issue and the final query finds all rows currently affected by this1:
;declare #t table (Val1 varchar(20))
insert into #t(Val1) values ('abc
'),('def')
select * from #t where Val1 = 'abc'
select * from #t where Val1 like 'abc%'
select * from #t where Val1 like '%
%'
So, fix those rows however you choose to do so. Next, add a CHECK constraint on this column:
ALTER TABLE T_VoucherHeaderEntry
ADD CONSTRAINT CK_T_VoucherHeaderEntry_NoExoticChars
CHECK (Vhe_VoucherNo not like '%[^-A-Za-z0-9]%')
(It's expressed as a double negative to say we want to disallow any character in the provided range. We have to put - as the first character so that it's interpreted literally and not as a range separator)
And finally update your applications to not attempt to insert such bogus data in the first place.
1The third query identifies those specifically affected by CR/LF issue. For a more general approach, once you've decided on the appropriate character range to specify in your check constraint, a variant of that same approach will find rows that won't satisfy the check constraint for you to fix.
if your Vde_VoucherNo column contain this 'APV-1808-00160' value then definitely below should work and return data
select * from T_VoucherHeaderEntry
where Vhe_VoucherNo = 'APV-1808-00160'
in case of white-space in your column value, you can use trim function
select * from T_VoucherHeaderEntry
where trim(Vhe_VoucherNo) = 'APV-1808-00160'
But if your column contain pattern of this values APV-1808-00160 then like will work which is your 2nd query
Select * from T_VoucherHeaderEntry
where Vhe_VoucherNo like 'APV-1808-00160%'
BTW noticed the two query is from two different table , so that may be also reason

Multiple columns in one SQL

Can i insert multiple values from different columns in one?
i have:
ref | alt | couple_refalt
--------------------------
A C AC ( i want)
A G AG Etc...
Is there a simple way?
I tried with:
INSERT INTO refalt(couple_refalt)
SELECT ref||alt
FROM refalt
WHERE ref='A';
Is it correct?
But it gives me the error:
null value in column violates not-null constraint
Postgres want a value for each colum, why can't i update or insert into specific column?
Storing comma separated value is not the SQLish way to store values. What you seem to want is a computed column. Postgres does not support that directly. One method is to declare a view:
create view v_refault
select r.*, ref || ',' || alt
from refault;
Other possibilities are:
Define a trigger to maintain the value.
Concatenate the values at the application level.
Use a function-based method to emulate a computed column.
In order to insert two values into one column, you need to concatenate them. In postgresql the syntax is the following.
SELECT ref::text || ', ' || alt::text FROM refalt
If you want more details, here is the string documentation

SQL Validating a string variable

Using SQL Scripts, I need to validate Comma Separate value. How should i validate the String Variable ?
Validation should be both Right / Left Trim for each value and there should not be any special characters such as Comma or Period for the last value.
create table #test
(col varchar(100))
insert into #test values
('1,2'),
('1,2,'),
('1,'),
('1,2,3,4,5')
select * from #test
In the above query, for the second value - Expected Result is 1,2
In the above query, for the Third value - Expected Result is 1
You can update your table to fix "offensive" values.
update #test
set col = substring(col, 1, len(col) - 1)
where col not like '%[0-9]'
This will remove last character where value doesn't end by a digit.
You can use a check constraint. You seem to want something like this:
alter table t add constraint chk_name as
(name like '%,%' and
name not like '%,%,%' and
name not like '%[^a-zA-Z,]%'
)
SQL Server doesn't have support for regular expressions. This implements the rules:
Name has to have a comma
Name does not have two commas
Name consists only of alphabetic characters and a comma
You may find that you need slightly more flexibility, but this handles the cases in your question.

SQL Select column as substring at first occurrence of characters?

I am using Postgresql and I need to run a query where I do a SELECT DISTINCT on a single string column. However I don't want to select the column as is, I need to substring it on the first occurrence of this string ' ('.
I do not know how to do this substring part..
Here is an example of the query without the substring part:
SELECT DISTINCT ON (Table.Column1) Table.Column2
FROM Table
ORDER BY Table.Column1
I am not sure what functions to use in postgres or possibly I need to use plpgsql to do this?
I managed to work this out. The function to be used is SPLIT_PART. Which takes three parameters ColumnName, Characters, And the substring occurance.
Here is an example of how I have used it.
SELECT DISTINCT ON (SPLIT_PART(Table.Column1, ' )', 1)) Table.Column2
FROM Table
ORDER BY SPLIT_PART(Table.Column1, ' )', 1)

How to concatenate row values for use in WHERE clause of T-SQL query

I want to write a query in T-SQL to perform a search on two concatenated columns. The two columns are fname and lname. Here is what I have so far:
SELECT
fname,
lname,
...
FROM
users
JOIN
othertable ON foo=bar
WHERE
fname+' '+lname LIKE '%query%'
SQL server doesn't like that syntax, though. How do I structure the query so that I can perform a WHERE LIKE operation that searches through two concatenated columns, allowing me to search the user's full name, rather than just first name and last name individually?
I can only suggest that one of fname or lname is NULL so the LIKE fails., (NULL concat anything is null)
Try
...
ISNULL(fname, '') + ' ' + ISNULL(lname, '') LIKE '%query%'
However, I would use a computed column and consider indexing it because this will run awfully.
My suggestion is to add a calculated column to your table for full_name
calculated column examples:
--drop table #test
create table #test (test varchar (10) , test2 varchar (5),[Calc] AS right(test, 3))
Insert #test
values('hello', 'Bye')
Insert #test
values('hello-bye', null)
Alter table #test
add [MyComputedColumn] AS substring(test,charindex('-',test), len(test)),
Concatenatedcolum as test+ ' ' +test2
select * from #test
As you can see you may have to play around a bit until you get the results you want. Do that in a temp table first to avoid having to restructure the database table multiple times. For names, especially if you are using middle name which is often blank, you may need to add some code to handle nulls. You may also need to have code sometimes to cast to the same datatype if one filed you are concatenating is an int for instance and the other a varchar.
I think one of the join conditions might be causing a problem. Try rewriting it, you may find the error goes away ;)