วันอาทิตย์ที่ 14 ธันวาคม พ.ศ. 2557

คำสั่ง SQL REPLACE

SQL REPLACE 
เป็นคำสั่งที่ใช้สำหรับการระบุเงื่อนไขการเลือกข้อมูลในตาราง (Table) โดยทำการแทนที่ข้อความที่พบในตำแหน่งที่ต้องการ 

Database : MySQL,Microsoft Access,SQL Server,Oracle

Syntax

SELECT REPLACE(Field,Search,Replace) AS [New-Field] FROM [Table-Name]


Table : customer
CustomerID
Name
Email
CountryCode
Budget
Used
C001
Win Weerachaiwin.weerachai@thaicreate.com
TH
1000000600000
C002
John Smithjohn.smith@thaicreate.com
EN
2000000800000
C003
Jame Bornjame.born@thaicreate.com
US
3000000600000
C004
Chalee Angelchalee.angel@thaicreate.com
US
4000000100000


Sample1 การเลือกข้อมูลโดยทำการแทนที่ค่า Win ด้วย Mr.Win

SELECT REPLACE(Name, 'Win', 'Mr.Win') AS MyName FROM customer

Output 

MyName
Mr.Win Weerachai
John Smith
Jame Born
Chalee Angel


นอกนากนี้ยังสามารถทำ REPLACE ไปแทนที่ในส่วนของ WHERE อีกด้วย

ที่มา : http://www.thaicreate.com/tutorial/sql-replace.html

ไม่มีความคิดเห็น:

แสดงความคิดเห็น