Stored procedure setting a value if row is empty - sql

How would I go about setting a value if a row is empty ('')?
I was thinking something like,
Got var with default value called #defaultValue to set it where the row in a table is ''.
if (select col1 from table1 where col1 = '')
set (select col1 from table1 where col1 = '') = #DefaultValue
is there a better way?
code is just a draft its not even tested..

If you want to update the table with #DefaultValue, you can use WHERE clause in the UPDATE query:
UPDATE table1
SET col1=#DefaultValue
WHERE col1=''
OR col1 IS NULL
OR
If you are trying to select #DefaultValue if the column is empty or null, you can do this:
SELECT CASE WHEN (col1 IS NULL OR col1='')
THEN #DefaultValue
ELSE col1
END AS Col1
FROM table1

select case when col1 ='' then #DefaultValues else col1 end from table
DEMO
declare #default int
set #default=1
declare #tbl table(col1 int)
insert into #tbl values(1),(''),(2)
select case when col1='' or col1 is null then #default else col1 end from #tbl

Related

Show other column value if the value of the column is NULL or blank

I want to display value of other column if the value of my column is NULL or blank. Below is my table.
DECLARE #Tab TABLE(ID INT, suser VARCHAR(10), sgroup VARCHAR(10), sregion VARCHAR(10))
INSERT INTO #Tab VALUES(1,'Test',NULL,NULL),(2,'','Group',NULL),(3,NULL,NULL,'Region'),(4,NULL,NULL,NULL)
SELECT * from #Tab
My Query:
SELECT ID
,Case WHEN suser IS NULL OR suser = ''
THEN sgroup
WHEN sgroup IS NULL OR sgroup = ''
THEN sregion
ELSE NULL
END AS col
from #Tab
I want oupput as:-
DECLARE #Tab1 TABLE(ID INT, col VARCHAR(10))
INSERT INTO #Tab1 VALUES(1,'Test'),(2,'Group'),(3,'Region'),(4,NULL)
SELECT * from #Tab1
Thanks
Blank and NULL are not the same. If you want to treat '' and NULL as the same value, one method would be to use NULLIF:
ISNULL(NULLIF(YourFirstColumn,''),YourOtherColumn)
Ideally, however, if either could be stored in your data but they should be treated as the same, don't allow one of them. Personally, I would update all the values of the column to NULL where they have a value of '' and then add a constraint that doesn't allow the value ''. Something like:
UPDATE YourTable
SET YourColumn = NULL
WHERE YourColumn = '';
ALTER TABLE YourTable ADD CONSTRAINT YourColumn_NotBlank CHECK (YourColumn IS NULL OR YourColumn <> '');
use COALESCE function it will return 1st non null value
SELECT ID ,COALESCE(suser , sgroup, sregion)
col
from #Tab

remove first comma in string using sql

my string like that :
1 ,QCIM1J25836, QCIM1J27637
2 ,QCIM1J25836, QCIM1J27637, QCIM1J27638
I want to remove first comma only it means my output will be for
1 QCIM1J25836, QCIM1J27637
2 QCIM1J25836, QCIM1J27637, QCIM1J27638
may be in other will not be comma...so please tell me how can I update all data like that...
The following query should work:
It will update all the records in the column.
UPDATE table
SET col2 = CASE WHEN LEFT(col2 ,1) =',' THEN RIGHT(col2,LEN(col2)-1)
ELSE col2 END
UPDATE table
SET col2 = case when charindex(',',col2,0) =1 then right(col2, len(col2)-1) else col2 end
In SQL-Server you can do It in multiple ways.
You use STUFF in following:
SELECT col1,
STUFF(col2,1,1,'') as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%'
or you can use RIGHT
SELECT col1,
RIGHT(col2,LEN(col2)-1) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';
or you can use SUBSTRING
SELECT col1,
SUBSTRING(col2, 2, 255) as [Col Without First Comma]
FROM tbl
WHERE col2 LIKE ',%';
UPDATE
As per your comment you can update in the same ways too:
Using SUBSTRING
UPDATE tbl
SET col2 = SUBSTRING(col2, 2, 255)
WHERE col2 LIKE ',%';
Or using RIGHT
UPDATE tbl
SET col2 = RIGHT(col2,LEN(col2)-1)
WHERE col2 LIKE ',%';
Or using STUFF
UPDATE tbl
SET col2 = STUFF(col2,1,1,'')
WHERE col2 LIKE ',%';
USE [LIB]
CREATE FUNCTION [dbo].[trimChar]
(
#p_string varchar(max),
#p_char varchar(1)
)
RETURNS varchar(max)
AS
BEGIN
declare #l_string varchar(max) = #p_string
-- lets do the front
while SUBSTRING(#l_string,1,1) = #p_char
begin
set #l_string = substring(#l_string,2,len(#l_string))
end
-- lets do the back
set #l_string = reverse(#l_string)
while SUBSTRING(#l_string,1,1) = #p_char
begin
set #l_string = substring(#l_string,2,len(#l_string))
end
set #l_string = reverse(#l_string)
return #l_string
END
GO

Statement blocks in SQL SELECT using IF ELSE

I'm trying to return different data depending on a variable in a SELECT. Something like this:
SELECT
IF #variable = 'YES'
column1, column2
ELSE
column3
FROM TABLE
What is this the proper way to use the IF condition in SQL? Or is there a better alternative to what I'm trying to accomplish?
If you want to return a different number of columns, you'll need to use an IF:
IF #variable = 'YES'
BEGIN
SELECT column1, column2
FROM YourTable
END
ELSE
BEGIN
SELECT column3
FROM YourTable
END
If you want different data on the same column (assuming the same datatype), you could use a CASE:
SELECT CASE WHEN #variable = 'YES' THEN column1 ELSE Column2 AS Data
FROM YourTable
You can use an IF statement, but you'll need to set up multiple queries. You can use a CASE for selecting one column or another, but not to select one or multiple like in your question.
DECLARE #var INT = 1;
DECLARE #test TABLE (
Col1 int,
Col2 int,
Col3 int
)
INSERT INTO #test VALUES (1,2,3)
IF #var = 1
BEGIN
SELECT Col1, Col2
FROM #test
END
ELSE
BEGIN
SELECT Col3
FROM #test
END

tsql: can "returning select" come before "update"?

I want to write a t-sql stored procedure (aka sproc) which selects 3 columns from 'MyTable'. In addition, I want to update the table in the same sproc:
I select the third value from the table.
If it equals 'true', I want to update the relevant record in the table to 'false'
I wasn't sure what syntax should I use. Could you help me out?
ALTER procedure [dbo].[My_PROC]
#ID varchar(10)
AS
BEGIN
declare #Col3 bit;
set #Col3 = select Col3
from dbo.MyTable with (nolock)
where #ID = ID
if #Col3 = 'true'
update dbo.dbo.MyTable set col3 = 'false'
where #ID = ID
select Col1,
Col2,
Col3
from dbo.MyTable table with (nolock) where #ID = ID,
table.Col1,
table.Col2,
#Col3
END
edit: I want to return the original Col3 (not the updated value).
Use:
ALTER procedure [dbo].[My_PROC]
#ID varchar(10)
AS
BEGIN
SELECT t.col1,
t.col2,
t.col3
FROM dbo.MyTable AS t WITH (NOLOCK)
WHERE t.id = #ID
-- No need for an IF statement to determine updating...
UPDATE dbo.dbo.MyTable
SET col3 = 'false'
WHERE id = #ID
AND t.col3 = 'true'
END
I don't know what you're intending for the final SELECT, but I can update it once I understand what you intended.
If you are using SQL Server 2005 or later, you can use an OUTPUT clause to output the original value if it was actually updated by the query. OUTPUT won’t give you the original value if the row is not updated by the query, however.
declare #t table (
ID int,
tf bit
);
insert into #t values
(1,0),
(2,1),
(3,0);
declare #ID int = 2;
select * from #t
update #t set
tf = 0
output deleted.ID, deleted.tf
where ID = #ID;
select * from #t;

how to include the column name in case statement

I need to include a column name in the where clause of SQL query depending on a variable value.
For Example,
1. If I have a variable,say,#test and I assigned the variable with the value 1/0.
2. I have a table ,say, table1 with 2 column names col1,col2.
Now, I am writing a query to fetch some data from the table by including only one column at a time depends on the #test variable(declared earlier) i.e. I need to include col1 in the where clause of the query if the #test is 1 and col2 if #test is 0.
Please let me know the solution asap.
Thanks in advance
Select *
From dbo.MyTable
Where Case When #Test = 1 Then Col1 Else Col2 End > 100
declare #tbl table
(
col1 int,
col2 int
)
insert into #tbl select 10, 20
declare #test int
set #test = 1
select *
from #tbl
Where Case When #test = 1 Then Col1 Else Col2 End = 10
If datatype is different
select *
from #tbl
Where Case when #test = 1 Then Col1 end = 10
OR
Case when #test = 2 Then Col2 end = 'sometext'
A slightly different approach:
Select *
From dbo.MyTable
Where Col1 = Case When #Test = 1 Then 10 Else Col1 End and
Col2 = Case When #Test = 0 Then 'sometext' Else Col2 End
This version of the query may be sargable.