채택완료

하이픈 넣기

2018-06-25 (월) 16:46:53 2,805

휴대폰 번호가 분할필드로 저장된 것을

phpmyadmin에서 replace로 제거했습니다. 그래서

공일공12345678이렇게 저장된 상태입니다.

이거를 디비자체에 하이픈을 넣어 주는 방법이 없을지요?

|

답변 3개 / 댓글 5개

채택된 답변
+20 포인트

update table

set wr_1 = concat(substr(wr_1,1,3),'-',substr(wr_1,4,4,'-',substr(wr_1,8,4))

where wr_1 not like '%-%'

답변에 대한 댓글 3개

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''-',substr(sa_hp,8,4)) where sa_hp not like '%-%'' at line 1 이렇게 애러 뜨는데..다시 좀 봐주십시오,
@앤플
문법오류는 보통 괄호를 하나 빠드릴때 나오는데
제가 작성해드린문장에서 문법오류가 있네요
substr(wr_1,4,4, <-- 괄호를 빠뜨렸네요

set wr_1 = concat(substr(sa_hp,1,3),'-',substr(sa_hp,4,4),'-',substr(sa_hp,8,4))
로 중간을 변경하신후에 다시 해보세요
감사합니다~ 깔끔하게 적용된거 같습니다.
Copy
// 휴대폰번호의 숫자만 취한 후 중간에 하이픈(-)을 넣는다.

 


function hyphen_hp_number($hp)
{
    $hp = preg_replace("/[^0-9]/", "", $hp);
    return preg_replace("/([0-9]{3})([0-9]{3,4})([0-9]{4})$/", "\\1-\\2-\\3", $hp);
}

그누보드 사용 중이라면 /lib/common.lib.php 에 있는 함수입니다.

답변에 대한 댓글 2개

감사합니다. 이거 어떻게 사용해야 되나요?
이렇게 되어 있는데요,
<input type="text" name="wr_203" id="hp" size="15" maxlength="13" value="<?php echo $write['wr_203']?>" required telnumber> 이렇게 되어 있는데
<?php echo hyphen_hp_number ($write['wr_203'])?>

select concat(substring("핸드폰번호", 1, 3),"-",substring("핸드폰번호", 4, 4),"-",substring("핸드폰번호", 8, 4))

substring 로 분할하시고 구분자 넣으셔서 concat 로 합치시면 될꺼에요

답변을 작성하려면 로그인이 필요합니다.