Select statement with unique format in SQL Server - sql-server

I have a table with a column 'Number' which contain values like INV-00100,INV-00101,INV-00102,INV-00103 etc.
I want to get highest value +1 in a format like "INV-00103". How it possible?
Any suggestions??
I have tried this query
SELECT
ISNULL(MAX(CAST(SUBSTRING(Number, 5, LEN(Number) - 4) AS INT)), 0) AS [MaxNo]
FROM
Table
But this statement returns like "103" as result. I want to get this "103" as "00103" (total of 5 digits).

Try like follwoing.
DECLARE #TABLE TABLE(Code VARCHAR(100))
INSERT INTO #TABLE
VALUES ('INV-00100'),('INV-00101'),('INV-00102')
SELECT RIGHT('00000'+
CAST(MAX(TRY_PARSE(SUBSTRING(Code,CHARINDEX('-',Code)+1,
LEN(Code)-CHARINDEX('-',Code)) AS INT) + 1) AS VARCHAR(10)),5)
FROM #TABLE
Output
00103
Edit:
This is working fine.. But what will happen if the Number exceeds
99999?? Will this give a value like INV-100000 – Vahid
DECLARE #TABLE TABLE(Code VARCHAR(100))
INSERT INTO #TABLE
VALUES ('INV-00100'),('INV-00101'),('INV-00102')
SELECT
(
CASE WHEN [Output] > 99999 THEN CAST([Output] AS VARCHAR(100))
--MORE CONDITION
ELSE RIGHT('00000'+CAST([Output] AS VARCHAR(100)),5)
END
) AS [Output]
FROM
(
SELECT
MAX(TRY_PARSE(SUBSTRING(Code,CHARINDEX('-',Code)+1,
LEN(Code)-CHARINDEX('-',Code)) AS INT) + 1) AS [Output]
FROM #TABLE
) T

What about something like this?
select left('INV-00100',6) + cast(max(cast(right('INV-00100', 5) as int) + 1)as VARCHAR)

Related

CHARINDEX function to fetch string of digits until there is '.'

How to fetch string of letters up to a digit before . using charindex, for example:
ID
1022786.12
1203384
1226757.23
22343445
23434533
and I want to fetch only those which do not have . using charindex function.
we can do this using like condition:
where ID not like '%.%'
But I want to use charindex function
After using charindex, the result should be:
Result
1203384
22343445
23434533
How do I do this?
use charindex('.', number) < 1
declare #t table(number varchar(20))
insert #t select '1022786.12'
insert #t select '1203384'
insert #t select '1226757.23'
insert #t select '22343445'
insert #t select '23434533'
select *
from #t
where charindex('.', number) < 1

SQL Server - How to select record where my value in split column

This is result that I want:
I wrote a stored procedure:
ALTER PROCEDURE [dbo].[test_Split]
#item INT
AS
CREATE TABLE #temp
(
Id INT,
Name NVARCHAR(50),
List NVARCHAR(100)
)
INSERT INTO #temp
VALUES (1, 'John', '1,2,4,6,7'), (2, 'Fox', '7,23,42,10,17'),
(3, 'Rose', '51,72,14,16,37'), (4, 'Alex', '1,22,84,56,47')
SELECT *
FROM #temp
When I execute test_Split, I get the results shown in the screenshot.
When I pass #item = 1 (any number), I want to get result like the right part of the screenshot (List column include my value).
Is there any way to do this without a function?
Just add
where List like + '%' + #item + '%'
So...
select *
from #temp
where List like + '%' + #item + '%'
Notice this will find all rows where 1 is present. The only hack to distinguish between 1 and numbers like 15 is to search for a comma.
select *
from #temp
where
List like + '%,' + #item + ',%'
or List like + #item + ',%'
or List like + '%,' + #item
It's much wiser to use a split function and INNER JOIN.
I'd check out Jeff's article.

Get the right number of a value in SQL Server using substring

I have this data in my SQL Server:
1/2
1/4
2/23
12/13
1/10
...
I need to change these to 002,004,023,013,010,..
I just need to select the end (RIGHT) part number of my value. I got the LEFT part using this code before:
RIGHT('000' + LEFT(SheetNumber, CHARINDEX('/', SheetNumber) - 1), 3)
Try this,
SELECT Right('000' + RIGHT(SheetNumber,LEN(SheetNumber) - CHARINDEX('/',SheetNumber) ), 3)
OR
SELECT RIGHT(REPLACE(#SheetNumber,'/','/000'),3)
Your were almost there
Declare #YourTable table (SheetNumber varchar(50))
Insert Into #YourTable values
('1/2'),
('1/4'),
('2/23'),
('12/13'),
('1/10')
Select right('000'+substring(SheetNumber,CHARINDEX('/',SheetNumber) + 1,10),3)
From #YourTable
Returns
(No column name)
002
004
023
013
010
First you need to get the right part like this :
declare #table table (SheetNumber varchar(10))
insert into #table values ('1/2')
insert into #table values ('2/23')
select Right(SheetNumber, len(SheetNumber) - CHARINDEX('/',SheetNumber)) from #table
This will give you this :
2
23
Now build on this to pad 0 in front of it
declare #table table (SheetNumber varchar(10))
insert into #table values ('1/2')
insert into #table values ('2/23')
select Right('000' + Right(SheetNumber, len(SheetNumber) - CHARINDEX('/',SheetNumber)), 3) from #table
and that will give you this :
002
023

SQL - Move last 2 character to become first 2

I need move last 2 characters of string to become first 2, for example, "ABC PT" become "PT ABC".
Thanks for help.
DECLARE #String VARCHAR(100) = 'ABC PT'
SELECT RIGHT(#String, 2) + ' ' + LEFT(#String, LEN(#String) -2)
RESULT : PT ABC
You can use substring function.
for example :
select substring('ABC PT',len('ABC PT')-1,2)+' '+stuff('ABC PT',len('ABC PT')-1,2,'')
Query:
DECLARE #Str as nvarchar(10);
SET #Str = 'ABC PT';
SELECT RTRIM(RIGHT(#Str,2)+' '+SUBSTRING(#Str, 1 , LEN(#Str)-2))
Result:
PT ABC
below is the select statement, use it to update if you want to
c1 is the column name in the table test15.
If you have a variable then replace c1 with the variable name and remove the from clause.
select RIGHT(c1,2)+SUBSTRING(c1,1,len(c1)-2) from test15
CREATE TABLE #TEMP
(
ID INT IDENTITY(1,1) ,
NAME VARCHAR(50)
)
INSERT INTO #TEMP VALUES('PC1AB')
INSERT INTO #TEMP VALUES('PC2XY')
INSERT INTO #TEMP VALUES('PC3NA')
INSERT INTO #TEMP VALUES('PC3NAXBBNTEYE12')
SELECT SUBSTRING(NAME,LEN(NAME)-1,2)+LTRIM(LEFT(NAME,LEN(NAME)-2)) FROM #TEMP

Converting an integer to a 0-padded string

In SQL Server 2008, I want to represent an integer as a 3-character string - so:
3 becomes '003'
5 becomes '005'
107 becomes '107'
How can I do this?
/* Method 1 Using RIGHT function */
SELECT RIGHT('000' + CAST(NumericColumn AS VARCHAR(3)), 3) PaddedCnumericColumn
FROM MyTable
/* Method 2 Using RIGHT AND REPLICATE function */
SELECT RIGHT(REPLICATE('0', 3) + CAST(NumericColumn AS VARCHAR(3)), 3) PaddedCnumericColumn
FROM MyTable
You can try this
DECLARE #Table TABLE(
Val INT
)
INSERT INTO #Table (Val) SELECT 1
INSERT INTO #Table (Val) SELECT 10
INSERT INTO #Table (Val) SELECT 100
SELECT REPLICATE('0',3 - LEN(CAST(Val AS VARCHAR(3)))) + CAST(Val AS VARCHAR(3))
FROM #Table
WHERE ABS(Val) < 1000

Resources