How to insert value to the new table based on condition
I have a table which has three column
A B
1 9999
2 999
3 99
4 9
Now I need to insert data into the new empty table, the query I am writing for this is below
I want when it insert into the new table it will come like this
A B
1 9999
2 0999
3 0099
4 0009
I am clueless 开发者_运维百科how to do it, please help
INSERT
INTO newtable
VALUES a, RIGHT(REPLICATE('0', 4) + CAST(b AS NVARCHAR(MAX)), 4)
FROM oldtable
INSERT INTO Table2 (A, B)
SELECT A, RIGHT(10000 + B, 4)
FROM Table1
You could do something like this:
SELECT A, REPLACE(SPACE(4-LEN(B)) & B, ' ', '0')
INTO NewTableName
FROM OldTableName
精彩评论