开发者

Storing SQL Server 2005 datetime

I have a php script inserting values in to SQL Server 2005 table via SQL

$date = '15 Jun 2011 00:00';
$sql = "INSERT INTO shows ( DateStart ) VALUES ( '$date')"; 

I have 2 issues:

1) how best to convert this date into something SQL Server understands? The default format is mm/dd/yyyy on my system but I want to present the dates 开发者_如何学Cas readable to the user.

2) When inserting an empty string the DateStart value gets set to 1/1/1900. How best to convert an "invalid" date into a NULL (the column already accepts NULLs) so that if $date = "" then DateStart IS NULL

Thanks!


  • Point 1: Use ISO date format YYYY-MM-DDTHH:MM:SS
    Note: For date only use YYYYMMDD becuase of anomalies in how SQL Server parse dates
  • Point 2: NULLIF(value, '')


  1. I think the datetime format while inserting the record in datetime column should always "YYYY-MM-DD HH:MM:SS" Default Format

  2. gbn already told.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜