สอบถามการแยกคอลัมน์


0 สมาชิก และ 1 บุคคลทั่วไป กำลังดูหัวข้อนี้

20 มี.ค. 61 , 14:36:21
อ่าน 1308 ครั้ง

Earth

สอบถามการแยกคอลัมน์
« เมื่อ: 20 มี.ค. 61 , 14:36:21 »
จากรูปด้านล่างมีข้อมูลฟิลด์ Locationอยู่ค่ะ 
อยากทำQuery ให้แยกเป็น 2ฟิลด์ โดยมีเครื่องหมาย - เป็นตัวแยก
และเติม0ด้านหน้าแต่ละหลักในวรรค - ให้ครบ 9หลัก
ส่วนชนิดที่ไม่มี- ก็แยกตามรูปเลยค่ะ


รบกวนขอแนวทางด้วยค่ะ
« แก้ไขครั้งสุดท้าย: 20 มี.ค. 61 , 14:38:18 โดย Earth »

 

20 มี.ค. 61 , 15:18:16
ตอบกลับ #1

pizza_p

> สอบถามการแยกคอลัมน์
« ตอบกลับ #1 เมื่อ: 20 มี.ค. 61 , 15:18:16 »
สร้าง Module ขึ้นมาแล้วเอาฟังก์ชั่นนี้ไปใส่
โค๊ด: [Select]
Public Function FormatLocation(stLocation As String, zone_or_id As Byte) As String
Dim splitText() As String, nPart As Integer, stResult As String

splitText = Split(stLocation, "-")
nPart = UBound(splitText)
stResult = ""
If zone_or_id = 1 Then
    stResult = splitText(0)
Else
    If nPart > 0 Then
        For i = 1 To nPart
            stResult = stResult & Format(splitText(i), "000")
        Next i
    Else
        stResult = "00000000"
    End If
End If
FormatLocation = stResult

End Function

เสร็จแล้วก็สามารถใช้ Query แบบนี้ได้
SELECT Location, FormatLocation([Location],1) AS Zone, FormatLocation([Location],2) AS ID FROM tblName;

« แก้ไขครั้งสุดท้าย: 20 มี.ค. 61 , 15:23:43 โดย pizza_p »

 

20 มี.ค. 61 , 15:33:48
ตอบกลับ #2

Earth

> สอบถามการแยกคอลัมน์
« ตอบกลับ #2 เมื่อ: 20 มี.ค. 61 , 15:33:48 »
ทำตามตัวอย่างขึ้นError แบบนี้ค่ะ


 

20 มี.ค. 61 , 23:48:25
ตอบกลับ #3

pizza_p

> สอบถามการแยกคอลัมน์
« ตอบกลับ #3 เมื่อ: 20 มี.ค. 61 , 23:48:25 »
เพิ่ม i As Integer เข้าไปบรรทัดนี้ครับ

Dim splitText() As String, nPart As Integer, stResult As String, i As Integer

 
โพสต์นี้ได้รับคำขอบคุณจาก: Earth

21 มี.ค. 61 , 09:05:10
ตอบกลับ #4

Earth

> สอบถามการแยกคอลัมน์
« ตอบกลับ #4 เมื่อ: 21 มี.ค. 61 , 09:05:10 »
รบกวนอีกรอบนะคะ

คือถ้าต้องการเปลี่ยน เช่น
CC-4-1-1 เป็น  zone= CC,   ID= 00400101 (3หลัก 3หลัก 2หลัก) จากเดิมที่เป็น 004001001
ต้องเปลี่ยนจุดไหนบ้างคะ

ขอบคุณค่ะ

 

21 มี.ค. 61 , 10:46:37
ตอบกลับ #5

pizza_p

> สอบถามการแยกคอลัมน์
« ตอบกลับ #5 เมื่อ: 21 มี.ค. 61 , 10:46:37 »
มันเป็นแบบนี้ทุกตัว หรือเฉพาะ CC

 

21 มี.ค. 61 , 10:55:36
ตอบกลับ #6

Earth

> สอบถามการแยกคอลัมน์
« ตอบกลับ #6 เมื่อ: 21 มี.ค. 61 , 10:55:36 »
เป็นทุกตัวค่ะ

 

21 มี.ค. 61 , 11:25:55
ตอบกลับ #7

pizza_p

> สอบถามการแยกคอลัมน์
« ตอบกลับ #7 เมื่อ: 21 มี.ค. 61 , 11:25:55 »
โค๊ด: [Select]
Public Function FormatLocation(stLocation As String, zone_or_id As Byte) As String
Dim splitText() As String, nPart As Integer, stResult As String, i As Integer

splitText = Split(stLocation, "-")
nPart = UBound(splitText)
stResult = ""
If zone_or_id = 1 Then
    stResult = splitText(0)
Else
    If nPart > 0 Then
        For i = 1 To nPart
            If i = nPart then
                stResult = stResult & Format(splitText(i), "00")
            Else
                stResult = stResult & Format(splitText(i), "000")
            End If
        Next i
    Else
        stResult = "00000000"
    End If
End If
FormatLocation = stResult

End Function

 
โพสต์นี้ได้รับคำขอบคุณจาก: Earth


บอร์ดเรียนรู้ Access สำหรับคนไทย


 

Sitemap 1 2 3 4 5