I have Alphabet A to Z in my Key Column and want to update them and set numeric,
ex:A=001 and B=002 and so on.
How to write the logic in sql query.
You could use ASCII T-SQL function, and RIGHT to pad-left it:
UPDATE MyTable
SET [KeyCol] = RIGHT('000' + CONVERT(varchar(3), ASCII([keyCol]) - ASCII('A') + 1), 3)
Related
I have a query that in SQL Server that returns data like this:
1234-A
2345-BB
3456-C
5678-CC
4567-AA
6789-B
01234-A
26857-ZZ
This is what I need it display:
A
B
C
C
A
B
A
Z
I need to get the first letter behind the '-'. How do I get this to display?
Try this:
DECLARE #MyTable TABLE (MyCol VARCHAR(255));
INSERT #MyTable (MyCol)
VALUES ('1234-A'),('2345-BB'),('3456-C'),('5678-CC'),
('4567-AA'),('6789-B'),('01234-A'),('26857-ZZ');
SELECT SUBSTRING(MyCol, CHARINDEX('-', MyCol, 1) + 1, 1)
FROM #MyTable;
CHARINDEX finds where the '-' in the column value is.
SUBSTRING starts at that index + 1 and returns, in this case, 1 character.
You can use substr In conjunction with instr to get the output
I have a database column that is a text string. Some of the values are like
"12345"
and some are as year + sequential number like:
"2016-1, 2016-2, 2016-3, 2017-1, 2017-2, 2017-3" etc.
I want to update the column values to
"2016-001, 2016-002, 2016-003, 2017-001, 2017-002, 2017-003"
for the entire table.
I'm not sure how to do this. Any help would be appreciated. I already updated my stored procedure as such to generate new numbers with zero padding like:
rptnum = cast(year(getdate()) as varchar)
+ '-' + RIGHT('000'+ISNULL(Cast((select count(*)
from dbo.tablename where rptyr = (year(getdate()))) + 1 as varchar),''),3),
Something like this should do:
select left(rptnum, charindex('-', rptnum))+right('000'+substring(rptnum, charindex('-', rptnum)+1, 10), 3)
I want to insert a value into an sql server 2008 table.
The value are like 20.00- , 10.00-
The minus sign is at the end of the value.
How can I achieve this?
How about using something like this?
SELECT REPLACE(CAST(-456.00 AS VARCHAR(10)),'-','') + '-'
Where 456.00 is your value
You could insert raw data into a staging table first. Then you could either convert the values in the staging table and insert them afterwards into the actual table, or insert and convert at the same time.
Here's how you could go about the conversion.
For every input value, you would need to check if it has a minus. If it does, then one character should be deleted at the last position and one character should be inserted at the first position. Otherwise nothing should be deleted or inserted.
So, essentially you can have an indication in the form of 1 or 0 and use that number to determine how many characters to insert or delete.
There are various methods of obtaining that 1/0 result.
For instance, you could take the input value's last character as a substring and use CHARINDEX to search for - within that substring:
CHARINDEX('-', LEFT(InputValue, LEN(InputValue)))
If the last character of InputValue is '-', the CHARINDEX will return 1, otherwise it will return 0.
Another method, also using CHARINDEX, would be to go like this:
CAST(CHARINDEX('-', InputValue) AS bit)
If InputValue has a minus, CHARINDEX will return a positive result and CAST will convert it to 1. If there is no minus, CHARINDEX will give you a 0, which will stay 0 after the cast.
Now you can use the expression's result to determine:
whether to delete the last character or not:
STUFF(InputValue, LEN(InputValue), CAST(CHARINDEX('-', InputValue) AS bit), '')
and
whether to prefix the value with a '-':
REPLICATE('-', CAST(CHARINDEX('-', InputValue) AS bit))
That is, the complete expression is this:
REPLICATE('-', CAST(CHARINDEX('-', InputValue) AS bit)) +
STUFF(InputValue, LEN(InputValue), CAST(CHARINDEX('-', InputValue) AS bit), '')
In the final statement you could also use CROSS APPLY to avoid repetition of the emphasised sub-expression, something aloong the lines of this (assuming you will choose to do insertion + processing):
INSERT INTO dbo.TargetTable (columns)
SELECT
...
REPLICATE('-', x.HasMinus) + STUFF(InputValue, LEN(InputValue), x.HasMinus, ''),
...
FROM dbo.StagingTable
CROSS APPLY (SELECT CAST(CHARINDEX('-', InputValue) AS bit)) AS x (HasMinus)
;
It is long but :
select IIF(ISNUMERIC(IIF(RIGHT(RTRIM('20.55-'), 1) = '-', '-' + REPLACE('20.55-', '-',''), '20.55-')) = 1, CONVERT(decimal(7,3), IIF(RIGHT(RTRIM('20.55-'), 1) = '-', '-' + REPLACE('20.55-', '-',''), '20.55-')), NULL)
Simply :
Select CASE WHEN LEFT(InputValue,1) = '-' THEN '-' ELSE '' END + REPLACE(InputValue,'-','')
You may need to Cast it as money (or numeric or decimal)
Basically, I want to increment a varchar in SQL the has a value of "ABC001".
I have code that adds one to an int, but I don't know how to get it working for a varchar:
SELECT
NXT_NO
FROM
TABLE
UPDATE
TABLE
SET
NXT_NO = NXT_NO + 1
Is there an easy way to increment if NXT_NO is a varchar?
I want:
ABC001
ABC002
ABC003
AND
It also needs to work with:
001, A0001, AB00001
Well, you can do something like this:
update table
set nxt_no = left(next_no, 3) +
right('0000000' + cast(substring(next_no, 4, 100)+1 as varchar(255)), 4)
A bit brute force in my opinion.
By the way, you could use an identity column to autoincrement ids. If you then want to put a fixed prefix in front, you ca use a calculated column. Or take Bohemian's advice and store the prefix and number in different columns.
update
[table]
set [nxt_no] = case when PATINDEX('%[0-9]%', [nxt_no]) > 0 then
left([nxt_no], PATINDEX('%[0-9]%', [nxt_no])-1) -- Text part
+ -- concat
right( REPLICATE('0', LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1) + convert( varchar, convert(int, right([nxt_no], LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1))+1), LEN([nxt_no]) - PATINDEX('%[0-9]%', [nxt_no])+1)
else
[nxt_no] end
I have data in the following format in a sql server database table
[CPOID] [ContractPO] [ContractPOTitle]
1 10-SUP-CN-CNP-0001 Drytech
2 10-SUP-CN-CNP-0002 EC&M
I need to write a stored procedure to generate the following result
[CPOID] [ContractPO] [ContractPOTitle] [ConcatField]
1 10-SUP-CN-CNP-0001 Drytech CNP-0001-Drytech
2 10-SUP-CN-CNP-0002 EC&M CNP-0002-EC&M
where [ConcatField] generate the result using split the last two values of the [ContractPOTitle] column and combine with the [ContractPOTitle]
If the ContractPO field is always the same length, you could just do:
SELECT
CPOID,
ContractPO,
ContractPOTitle,
RIGHT(ContractPO, 8) + '-' + ContractPOTitle as [ConcatField]
FROM MyTable
Assuming that the length of the ContractPO field is not fixed AND we have to rely on stripping out the text after the next to last '-', the following SQL will work. It's a bit ugly, but these types of operations are necessary because there doesn't appear to be a LASTINDEX function available out of the box in SQL Server.
SELECT
CPOID,
ContractPO,
ContractPOTitle,
RIGHT(ContractPO, CHARINDEX('-', REVERSE(ContractPO), CHARINDEX('-', REVERSE(ContractPO)) + 1) - 1) + '-' + ContractPOTitle as [ConcatField]
FROM #myTable