检查字段的最后X个字符是否为数字

时间:2012-04-17 09:16:23

标签: sql sql-server-2008 tsql

实际上有2个问题,2个查询:

  • 我需要找出一个以'541'开头18 chars为长的数字 事实上,在SO:Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when....'541'....15RandomNumbers in (0,1,2,3,4,5,6,7,8,9).....an unknown printer took a galley of type and scrambled it to make a type specimen book.
  • 之后就有一个空格
  • 第二步:检查字符串的最后18个字符是否为数字(可能在修剪右后)

就是这样,

2 个答案:

答案 0 :(得分:1)

一些数据让你开始

declare @tmp table (value nvarchar(30))
insert @tmp values ('ABC123456789123456789'),('ABC12345678912345678A'),('ABC123456789123456D78')

查看RIGHTISNUMERIC

SELECT t.value, ISNUMERIC(RIGHT(t.value,18)+'.0e0') as [IsNumeric]
FROM @tmp t

给出:

value                          IsNumeric
------------------------------ -----------
ABC123456789123456789          1
ABC12345678912345678A          0
ABC123456789123456D78          0

编辑 我根据Damien_The_Unbeliever的反馈修改了我的答案,并解决了他提出的问题

答案 1 :(得分:1)

“找出以'541'开头的数字是18个字符长”

( text_col NOT LIKE '541%' OR LEN(text_col) = 18 )

“检查字符串的最后18个字符是否为数字(可能在修剪右后)”

   ( RTRIM(text_col) LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )