SQL查询修剪字符串

时间:2015-06-17 11:28:52

标签: sql sql-server trim

我的查询如下

select 
    First_Name+' '+Middle_Name+' '+Last_Name as studentname,
    Batch,
    Batch+1 as batch1,
    Admtd_Semester,
    Program,
    Title,
    @His_Her as His_Her,
    Fathers_Name,
    Branch_Name,
    Student_Mobile_Number,
    Fathers_Mobilenumber,
    CONVERT(VARCHAR(10),GETDATE(),110) as date 
from 
    STUDENT_Admission_1212341

在上面的查询中,批处理+ 1将输出显示为2014,其中我只需要14.如何修剪它?

6 个答案:

答案 0 :(得分:4)

计算余数:

SELECT 2014 % 100

即:

SELECT  First_Name + ' ' + Middle_Name + ' ' + Last_Name AS studentname ,
        Batch ,
        (Batch + 1 ) % 100 AS batch1 ,
        Admtd_Semester ,
        Program ,
        Title ,
        @His_Her AS His_Her ,
        Fathers_Name ,
        Branch_Name ,
        Student_Mobile_Number ,
        Fathers_Mobilenumber ,
        CONVERT(VARCHAR(10), GETDATE(), 110) AS date
FROM    STUDENT_Admission_1212341

答案 1 :(得分:2)

试试这个:

RIGHT(CAST(Batch + 1 AS VARCHAR(10), 2) AS batch1

答案 2 :(得分:2)

select 
    First_Name+' '+Middle_Name+' '+Last_Name as studentname,
    Batch,
    RIGHT(Batch + 1, 2) as batch1,
    Admtd_Semester,
    Program,
    Title,
    @His_Her as His_Her,
    Fathers_Name,
    Branch_Name,
    Student_Mobile_Number,
    Fathers_Mobilenumber,
    CONVERT(VARCHAR(10),GETDATE(),110) as date 
from 
    STUDENT_Admission_1212341

<强>测试

2099给出99

2009年给出09

答案 3 :(得分:1)

为什么不能尝试使用 SubString

select 
    First_Name+' '+Middle_Name+' '+Last_Name as studentname,
    Batch,
  substring(Batch+1,3,4) as batch1,
    Admtd_Semester,
    Program,
    Title,
    @His_Her as His_Her,
    Fathers_Name,
    Branch_Name,
    Student_Mobile_Number,
    Fathers_Mobilenumber,
    CONVERT(VARCHAR(10),GETDATE(),110) as date 
from 
    STUDENT_Admission_1212341

这可能会给你相同的结果

答案 4 :(得分:0)

val1 = val1.OrderBy(Function (c) c ).ToArray

答案 5 :(得分:0)

使用RIGHT功能。

SELECT First_Name+' '+Middle_Name+' '+Last_Name AS studentname, 
Batch, 
RIGHT(Batch,2) + 1 AS batch1, 
Admtd_Semester, 
Program,Title, 
@His_Her AS His_Her, 
Fathers_Name, 
Branch_Name, 
Student_Mobile_Number, 
Fathers_Mobilenumber, CONVERT(VARCHAR(10),GETDATE(),110) AS date 
FROM STUDENT_Admission_1212341