将记录提取到表的问题
问题描述:
朋友们,我有一张桌子
喜欢
hi friends i have one table
like
id name
1 AAA
2 BBB
3 CCC
我想记录这个
and i want to record lik this
id
0001
0002
0003
感谢提前
Thanks to advance
答
您需要转换号码ID吗?
您可以这样做。
Do you need to convert number ID?
You can do this.
SELECT RIGHT('0000' + CONVERT(varchar, id), 4), name FROM table;
USE [cpqaAnswers]
GO
CREATE TABLE [cpqa].[tbl_RP_zeropadSolution005]
(
P_Id int,
amount varchar(50),
name nvarchar(10)
)
INSERT INTO [cpqa].[tbl_RP_zeropadSolution005]
VALUES(1, '9', 'Tintin'),
(2, '361','TakTak'),
(3, '542','Toucan'),
(4, '2010','Tallidego')
SELECT * FROM [cpqa].[tbl_RP_zeropadSolution005]
SELECT * FROM [cpqa].[tbl_RP_zeropadSolution005] ORDER BY [amount]
将前两个SELECT比较为:
Compare previous two SELECT to this:
SELECT [P_Id], LEFT(REPLICATE('0', 6), 6 - LEN([amount])) + [amount] AS [zeropadded_amount], [name] FROM [cpqa].[tbl_RP_zeropadSolution005]
SELECT [P_Id], LEFT(REPLICATE('0', 6), 6 - LEN([amount])) + [amount] AS [zeropadded_amount], [name] FROM [cpqa].[tbl_RP_zeropadSolution005] ORDER BY [zeropadded_amount] DESC
将上面的内容复制并粘贴到New Query中,点击F5查看。
Copy and paste the above into New Query and hit F5 to see.
这样的东西:
Something like this:
SELECT RIGHT('000'+ CONVERT(VARCHAR, Id), 6) AS Result FROM MyTable;