How to update a table concatenating the strings in two columns? - sql

I want to update a column setting it up as the concatenation of the string in one column + the file extension (in this case, '.AVI').
I tried the following but it affects 0 rows; I know for a fact that there are records which have a 'FALSE' value for Video_Name.
Any ideas what I'm doing wrong? Thanks!
declare #creativity varchar
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name=concat(#creativity,'.AVI') where Video_Name = 'false' and
#creativity=Creativity

I would expect something like this:
declare #creativity varchar(255); -- length is really important
set #creativity = <some value goes here> ; -- value is really important
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name = concat(#creativity, '.AVI')
where Video_Name = 'false' and
Creativity = #creativity;
Or perhaps you just want:
update [UK_Telco_Pressure_2018Q3_2019-02-26 W6]
set Video_Name = concat(creativity, '.AVI')
where Video_Name = 'false' ;

Related

SQL : Update value when the value in the database is null

I know this is already asked question and possible to be close.
But i really want a answer, I already searched through the internet, Read documentations, Blogs, and Question to SO.
This is my Query so Far,
declare #count numeric
select #count = (select count(1) from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq))
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=isnull(-1,Is_AESM)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
if ##rowcount <> #count
begin
rollback tran
Print "Error: There is an error on table E496_TitleReference."
return
end
go
For eg, In my table in Database i have column name Is_AESM, In Is_AESM column it have 4 values.
Is_AESM
NULL
NULL
-1
-2
Something like this.
Now when i run my script, it has no problem when i run it,
Is_AESM=isnull(-1,Is_AESM)
In this query it will detect if Is_AESM is null, it will update Is_AESM = -1 if not it will retain the value.
Now my problem is, if my query detect Is_AESM has a null value, it will update all the value to -1.
Is_AESM
-1
-1
-1
-1
The result is something like that. Now i want is update only the null value not all the value in column Is_AESM.
I think this query is wrong Is_AESM=isnull(-1,Is_AESM).
Any ideas will be a big help.
You may try with coalsece() function
update E496_TitleReference
set PrintStatus = '{0}',Is_AESM=coalsece(Is_AESM,-1)
from E496_TitleReference a where
exists (select 1 from #tempTransactions b where a.EPEB_RoD = b.tEPEB_RoD and
a.EPEB_ENO = b.tEPEB_ENO and a.EPEB_ID = b.tEPEB_ID and a.Title_Seq = b.tTitle_Seq)
you need to replace order of parameters.
Is_AESM=isnull(Is_AESM, -1)
You can use COALSECE function. It returns the first non-null entry from the given list. So:
Is_AESM= COALSECE(IS_AESM,-1)
This will return IS_AESM value if it is not null (since it is the first non-null value)
Else if IS_AESM is NULL then it returns -1 (since it is the non-null value)

How to set optional parameter for int type variable

Have two variable StatusTypeTestID and StatusTestID want to set them as optional in where clause as like bellow,but optional option not work for int variable.
Note: default value for int variable is 0
DECLARE #StatusTypeTestID as int
SET #StatusTypeTestID = 1
DECLARE #StatusTestID as int
SET #StatusTestID = 0
select *
from LiveCustomerStatus
where (StatusType=#StatusTypeTestID
and (Status = #StatusTestID or #StatusTestID is null))
As you were already trying, you can check if your parameters have been set as null or if they match the correspondent field.
This way you can leave a parameter as null if you want it to be optional, not affecting the result.
select *
from LiveCustomerStatus
where (#StatusTypeTestID is null or StatusType=#StatusTypeTestID)
and (#StatusTestID is null or Status = #StatusTestID)
option(recompile)
I have added a option(recompile) clause that will force SQL Server to recompile the query at every execution. This way it will use the appropriate indexes to optimize it depending of the value of the parameters (wether they are null or not).
You can comment both set statements and below query will work:
select * from LiveCustomerStatus
where (#StatusTypeTestID is null or StatusType=#StatusTypeTestID)
and (#StatusTestID is null or Status = #StatusTestID)
Use CASE Statement in WHERE clause :
SELECT *
FROM LiveCustomerStatus
WHERE StatusType = CASE WHEN ISNULL(#StatusTypeTestID,'') = '' THEN
StatusType ELSE #StatusTypeTestID END
AND Status = CASE WHEN ISNULL(#StatusTestID,'')= '' THEN Status ELSE
#StatusTestID END

Can Multiple Parameter be used with LIKE in SQL

I am getting data from database in a format like "chem*,bio*" what i want to do is after i split the string into two i want to fetch all records containing "chem" and "bio" .. using LIKE with multiple parameter is something i want since CONTAIN will bring in irrelevant data too. Kindly help.
its something like this
assume:
#cwork2 ='chem*,bio*'
#cw1=#cw1 +'OR contains (name,'''+#Cwork1+''')'
#cw1=#cw1 +'OR name LIKE ('''+#Cwork1+''','%')'
Try this:
You can use pipeline (|) to achieve Or Condition
select * from Tablename where name like '[chem|bio]%';
just add them in an OR condition.
#cw1 = #cw1 OR name like '%chem%' OR name like '%bio%'
i found another way... although he answers provided are right on track but when talking about variables we cannot simply add on variables in the code. Hence forth i decided to put the variable in a #temp table, loop it through and then accordingly fetch data
insert into #publication select item from fsplit(#Work,',')
Declare #loopc int=1
while (#loopc <= (SELECT count(*) from #pub))
Begin
set #Cwork1= (select name from #pub where id= #loopc);
if CHARINDEX(#Cwork1,'*')<0
Begin
set #cw1='or pub.name ='''+#Cwork1+''')'
end
else
begin
set #Cwork1 = REPLACE(#Cwork1,'*','%');
set #cw1=#cw1 +'OR pub.name LIKE ('''+#Cwork1+''')'
end
set #loopc= #loopc +1;
end
set #cw1= (SELECT STUFF(#cw1, CHARINDEX('or', #cw1), LEN('or'), ''))
set #cw1= '('+#cw1+')'
set #Au2= #Au2 + ' and '+ #cw1

Conversion from nvarchar to float isn't working

I am unable to get the following query to work due to errors upon conversion from nvarchar to float; I need to convert theData field to float in order to round it, but the data is originally an NVARCHAR(20) because the column holds character data as well. I've tried casting each of the instances of theData to float but it still didn't work, can anyone tell me what I'm missing?
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Round(Convert(float, [theData]),(Len([theData])-Charindex('.',[theData])))
ELSE Null END
WHERE tblData.theFlag =1;
I have tried the following two variants...
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Round(Convert(float, [theData]),(Len(Convert(float, [theData]))-
Charindex('.',Convert(float, [theData]))))
ELSE Null END
WHERE tblData.theFlag =1;
and...
UPDATE tblData SET tblData.theNumericData = CASE WHEN IsNumeric([theData]) = 1
THEN Convert(nvarchar(20),Round(Convert(float, [theData]),(Len(Convert(float,
[theData]))- Charindex('.',Convert(float, [theData]))))) ELSE Null END
WHERE tblData.theFlag =1;
Can't answer the question because we don't know the error you're getting but... you should move the case to where... it'll make it more readable.
UPDATE tblData
SET tblData.theNumericData = Convert(float, [theData])
WHERE IsNumeric([theData]) = 1 AND tblData.theFlag =1;

Update of column happens even though I don't want it to happen

I've got a case statement:
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN LEN(#AnsweredBy) > 0 THEN #AnsweredBy END
Even when #AnsweredBy is NULL it still sets the AnsweredBy column to null.
I've tried to test for null as well by doing this:
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN #AnsweredBy IS NOT NULL THEN #AnsweredBy END
Meaning I do not want to update the answeredby column unless there is a value.
Even in my C# code I do not pass a value to my stored procedure:
if (answeredBy.Length > 0)
cmdSelect.Parameters.Add("#AnsweredBy", SqlDbType.VarChar).Value = answeredBy;
unless there is a value. And in my sproc I default that column variable to null:
#RunoffAnswerID bigint,
#AnswerID varchar(3)=NULL,
#AnsweredBy varchar(50)=NULL,
So my question is how do I perform the rest of my updates, because there are about 5-10 more columns but only update the answeredby if there is a value in #AnsweredBy?
SET AnsweredBy = ISNULL(#AnsweredBy, AnsweredBy)
UPDATE
Answer
SET
AnswerID = #AnswerID,
AnsweredBy = CASE WHEN LEN(#AnsweredBy) > 0 THEN #AnsweredBy ELSE AnsweredBy END