sqlsql-server-2012inner-query

select statement to truncate and append column values


i have a table which contains columns

[id],[StartsWith],[Length] ,[MinExt] ,[MaxExt],[isDID] ,[DeleteDigits] ,[AppendDigits]

208 ,     61     ,    5    ,  61000  ,  61999 ,   0    ,       2       ,   22058

209 ,     63     ,    5    ,  63000  ,  63999 ,   0    ,       2       ,  26518

now let suppose if user sends a number say 61205 i have to check that in which row that number(i.e 61205) exist by checking between MinExt and MaxExt

after that i have to truncate x digits from 61205 mentioned in corresponding deleteDigits from the start of the number (in this case 2 digits will be truncated) after truncate i have to append 205 in last of [appenddigits] column. which will make a complete number like 22058205.

i have to do this through select statement as it will be an inner query. or if anyone can suggest something else i will be very thankful.


Solution

  • Try this:

    DECLARE @VAL NVARCHAR(100) = '61205'
    
    SELECT AppendDigits + SUBSTRING(@VAL, 3, LEN(@VAL) - 2)
    FROM   YOUR_TABLE
    WHERE  CAST(@VAL AS int) BETWEEN MinExt AND MaxExt