2015-06-17 118 views
2

我有一個查詢如下SQL查詢來修剪字符串

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.我如何可以修剪嗎?

+0

PLZ指定什麼'Batch'的數據類型,什麼都在此列可能值的集合? – Deepshikha

+0

@Deepshikha數據類型將是'int'。他加了'+ 1'吧? – Jesuraja

回答

0
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; 
+0

謝謝你..這對我解決:) –

+0

不客氣:) – DNac

4

Calc的餘數爲:

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 
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 
2

試試這個:

RIGHT(CAST(Batch + 1 AS VARCHAR(10), 2) AS batch1 
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給出

1

你爲什麼不能嘗試使用

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 

這可能會給你同樣的結果

+0

我試過使用子字符串,但它沒有工作,因爲批+ 1不是表中的實際字段它是1添加到批 –

+0

要麼這些可以幫助你。您需要將子字符串設爲**選擇SUBSTRING('2013'+ 1,3,4)**或 **選擇SUBSTRING('2013',3,4)+ 1 ** –