使用多个条件提取列值的数据?

huangapple go评论61阅读模式
英文:

Extract data using multiple conditions for the column value?

问题

从某一列中,我需要以下类型的数据:

  • 值为6位数且为数字
  • "L"后跟5位数字,例如L12345,需要将L更改为0,所以值应为012345
  • "L"后跟6位数字,例如L123456,需要将L更改为0,所以值应为0123456

其余的值应该按原样提取。

我有所有条件的单独查询:
条件1:

SELECT [PartNoAsIs]
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE LEN(PartNoAsIs) = 6 AND IsNumeric(PartNoAsIs) = 1

条件2:

SELECT [PartNoAsIs]
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE [PartNoAsIs] LIKE '[A-Za-z][0-9][0-9][0-9][0-9][0-9]'

条件3与条件2几乎相同。

从条件1和条件2中,需要将"L"更改为0,所以值应为012345和012456。然后,必须提取应用这3个条件后剩下的其他值。

运行Microsoft SQL Server标准版版本9.00.2047.00
感谢任何帮助!

英文:

From a certain column I need the following type of data:

  • Value is 6 characters long and is numerical
  • “L” is any letter followed by 5 numerical characters, for example L12345 the L needs to be turned into a 0 so the value should be 012345
  • “L” is any letter followed by 6 numerical characters, for example L123456 the L needs to be turned into a 0 so the value should be 0123456

The rest of the values are to be extracted as is.

I have all the conditions in sperate queries:
Condition 1:

SELECT [PartNoAsIs]
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
Where  LEN(PartNoAsIs) = 6 AND IsNumeric(PartNoAsIs)=1

Condition 2:

SELECT [PartNoAsIs]
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE [PartNoAsIs] LIKE '[A-Za-z][0-9][0-9][0-9][0-9][0-9]'

Condition 3 is almost same as Condition 2

From Condition 1 and 2 L needs to be turned into a 0 so the value should be 012345 and 012456 respectively. Then rest of the values that are left after applying these 3 conditions have to extracted.

Picture of a sample output is attached使用多个条件提取列值的数据?

Running Microsoft SQL Server Standard Edition Version 9.00.2047.00
Any help is appreciated!

答案1

得分: 1

You may make use of the RIGHT() function here:

SELECT PartNoAsIs, '0' + RIGHT(PartNoAsIs, LEN(PartNoAsIs) - 1) AS PartOut
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE PartNoAsIs LIKE '[A-Za-z][0-9][0-9][0-0][0-0][0-0]' OR
      PartNoAsIs LIKE '[A-Za-z][0-9][0-9][0-0][0-0][0-0][0-0]';

For the rest of the data use:

SELECT *
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE PartNoAsIs NOT LIKE '[A-Za-z][0-9][0-0][0-0][0-0][0-0]' AND
      PartNoAsIs NOT LIKE '[A-Za-z][0-9][0-9][0-0][0-0][0-0][0-0]';
英文:

You may make use of the RIGHT() function here:

<!-- language: sql -->

SELECT PartNoAsIs, &#39;0&#39; + RIGHT(PartNoAsIs, LEN(PartNoAsIs) - 1) AS PartOut
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE PartNoAsIs LIKE &#39;[A-Za-z][0-9][0-9][0-9][0-9][0-9]&#39; OR
      PartNoAsIs LIKE &#39;[A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9]&#39;;

For the rest of the data use:

<!-- language: sql -->

SELECT *
FROM [NIMS_Eng_Test].[dbo].[Madill_Exraction_Test]
WHERE PartNoAsIs NOT LIKE &#39;[A-Za-z][0-9][0-9][0-9][0-9][0-9]&#39; AND
      PartNoAsIs NOT LIKE &#39;[A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9]&#39;;

huangapple
  • 本文由 发表于 2023年6月9日 09:25:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436627.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定