0

I have an SQL table:

date
************************
|  id   |  date format |
************************
|   1   | 2013-02-10   |
|   2   | 02-02-2013   |
|   3   | 20130202     |
************************

I want to store the diff (date format) in the date table, checking with the following pattern against the table date:

/^\d{2}-\d{2}-\d{4}$/

How can I check this pattern against the date format field in the date table?

2
  • you can use REGEXP in mysql Commented Aug 19, 2013 at 8:11
  • Firstly, don't go calling your tables things closely resembling reserved words like "date". Commented Aug 20, 2013 at 10:36

4 Answers 4

1

I'm not sure why you stored date like that.

I'm assuming your database is MySQL, if it is you could use str_to_date function as follows:

select `date format` from mytable where str_to_date(`date format`, '%d-%m-%Y') is not null;

Note that this query is not going to be efficient as it has to scan every row and apply the str_to_date function.

Sign up to request clarification or add additional context in comments.

4 Comments

i don't know why you didn't use REGEXP
i want to check regex pattern its not fix only date check like time date-time string etc...
@user2496644, Please see the linked documentation. This function returns DATETIME. However, if you specifically want to use regex then DevZer0's answer should do it for you.
0

Try this code

Through this syntax you can check its regex pattern

  int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

Comments

0

you can pass your regexp with slight modification to a query it self.

SELECT * FROM table WHERE `date format` REGEXP '^[0-9]{2}-[0-9]{2}-[0-9]{4}$'

5 Comments

its work but my pattern is not valid on work /^\d{2}-\d{2}-\d{4}$/
your pattern includes \d qualifier which is not recognized in mysql
but i have lost of pattern this type so what i can do... ? for solve
replace \d with [0-9]
0

i fix it... (y)

<?php
$pattern = '/^\d{4}$/';
$link = mysql_connect('localhost', 'root', '');

if (!$link) {
    die('Could not connect: ' . mysql_error());
}

if (!mysql_select_db('regex_field')) {
    die('Could not select database: ' . mysql_error());
}

$result = mysql_query('SELECT date_format FROM date');

$date = array();
$newdate = array();

while ($date_row = mysql_fetch_assoc($result)) {
    $date[] = $date_row;
}

foreach($date AS $key => $value) {
    $newdate[$key] = $value['date_format'];
}

foreach($newdate as $key => $value) {
    $testexample = @preg_match($pattern, $value, $matches);

    if($testexample == true) {
        echo "Example & Pattern Match";

        exit;
    }
}

echo "Example & Pattern MisMatch";

mysql_close($link);
?>

2 Comments

don't use the @ sign. read this
In addition to DevZer0's comment, mysql_ functions are deprecated, please start looking into mysqli or PDO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.