remove first comma in string using sql - 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

Related

How to condense multiple update statement - SQL

I have an update statement for 10 columns which is replacing values in each of the column with a where condition to that specific column.
Here's the code:
UPDATE Table1
SET Col1 = REPLACE(Col1, '#DIV/0', NULL)
WHERE Col1 = '#DIV/0';
UPDATE Table1
SET Col2 = REPLACE(Col2, '#DIV/0', NULL)
WHERE Col2 = '#DIV/0';
UPDATE Table1
SET Col3 = REPLACE(Col3, '#DIV/0', NULL)
WHERE Col3 = '#DIV/0';
Like this I have it for 10 columns, it does the work but it doesn't look clear or professional.
I wanted to condense this code and make it look like a professional code.
Any suggestions is much appreciated.
Thanks!
I suggest you leave it as it is, with one change because statements such as REPLACE(Col1, '#DIV/0', NULL) do not make sense: calling REPLACE in sql server where any parameter is NULL always produces NULL, regardless if the text is found or not.
So best is this:
UPDATE Table1 SET Col1 = NULL WHERE Col1 = '#DIV/0';
UPDATE Table1 SET Col2 = NULL WHERE Col2 = '#DIV/0';
UPDATE Table1 SET Col3 = NULL WHERE Col3 = '#DIV/0';
Use case expressions keep the old value if it's not equal to '#DIV/0', the default else null will replace the '#DIV/0' values with NULL.
UPDATE Table1
SET
Col1 = case when Col1 <> '#DIV/0' then Col1 end,
Col2 = case when Col2 <> '#DIV/0' then Col2 end,
...
Note that all rows will be updated, i.e. one huge transaction. If that's a problem, either go back to your original solution with several updates, or add this at the end:
where '#DIV/0' in (Col1, Col2, ...)
If this is a good or bad idea depends on the data, how many percent of the rows/columns need to be updated. Indexes etc.
Assuming its T-SQL, why not just use NULLIF function
DECLARE ValToNull AS VARCHAR(10) = '#DIV/0'
UPDATE Table1
SET Col1 = NULLIF(Col1, ValToNull),
Col2 = NULLIF(Col2, ValToNull),
Col3 = NULLIF(Col3, ValToNull)

Select where in list or else select all most elegantly

For a select statement I set a part of the where condition externally in a configuration file as parameter, like this (COL2 is filled with string values):
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 = $external_parameter
Now I have a more complex case, where I need the external parameter to be a list. Whenever a value of COL2 is in the list, it should select, however if no list is provided or some kind of "empty" tag, every value should be selected again. I came up with an idea, but it does not work that way:
SELECT COL1
,COL2
FROM TABLE1
WHERE $external_list = ('') or COL2 IN $external_list
Like this, the statement would be true for all elements of COL2 when an empty list is provided, and true for every element in the list that matches with the list if a filled list is provided.
However, if I provide a filled list it will not work on the comparison in the first half of the where statement:
('entry_a', 'entry_b') = ('')
Is there any query that would make it work - best in a one line where statement?
Try something like this,
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 IN ($EXTERNAL_PARAMETER)
OR 1 = (
CASE
WHEN $EXTERNAL_PARAMETER = ''
THEN 1
ELSE 0
END
)
Try this way
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 IN (
CASE
WHEN $EXTERNAL_PARAMETER = ''
THEN COL2
ELSE $EXTERNAL_PARAMETER
END)
Try this
IF $EXTERNAL_PARAMETER = ''
SET $EXTERNAL_PARAMETER = NULL
SELECT COL1
,COL2
FROM TABLE1
WHERE COL2 = ISNULL($EXTERNAL_PARAMETER, COL2 )

Stored procedure setting a value if row is empty

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

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

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.