| Author |
Topic |
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-08-16 : 05:42:58
|
| I have a datetime field in my database, but this statement doesn't workSELECT * FROM vwAllMatrixVehiclesWHERE created > 1/1/2008It returns all records even though nothing has a created date in 2008Have I got the syntax wrong?Thanks |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2007-08-16 : 05:45:08
|
where created > '2008-01-01'Duane. |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2007-08-16 : 05:47:19
|
for the record 1/1/2008 is actually 1900-01-01 as it is perform mathematical division 1 / 1 / 2008 - try this select cast(1/1/2008 as datetime)Duane. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-16 : 05:49:37
|
quote: Originally posted by Mondeo I have a datetime field in my database, but this statement doesn't workSELECT * FROM vwAllMatrixVehiclesWHERE created > 1/1/2008It returns all records even though nothing has a created date in 2008Have I got the syntax wrong?Thanks
Date values should be expressed within single quotesOtherwise 1/1/2008 would become 0 and you will get all dataMadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-08-16 : 05:50:25
|
MadhivananFailing to plan is Planning to fail |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-08-16 : 06:41:12
|
| Okay thanks, its thrown up another problem now.This works fine in a query window in SQL management studio, I get 84 records.SELECT * FROM vwAllMatrixVehicles WHERE created >= '2007/8/9'But when I execute the same command from ASP.NET using the SqlCommand class it returns no records.I've checked and double checked my code, if I remove the where clause it works fine, it also works with a where clause not mentioning a date.I've never had an issue like this? Has anybody come accross it?Thanks |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2007-08-16 : 07:29:54
|
You should rather execute this as a storedproc and not inline sql from your asp code, inline sql can cause many problemsDuane. |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-08-16 : 07:45:25
|
| But why does it work in the query window and not as a command, does the .NET SqlClient class deal with dates in a different way? |
 |
|
|
ditch
Master Smack Fu Yak Hacker
1466 Posts |
Posted - 2007-08-16 : 07:47:39
|
Paste the .net code here - so we can take a look.It should handle it the same.Duane. |
 |
|
|
Mondeo
Constraint Violating Yak Guru
287 Posts |
Posted - 2007-08-16 : 08:07:48
|
Code below, when I test the value of dsResults it is an empty datatable. The same statement is query analyser returns some rows, if I replace the sql statement with something else without a date the datatable gets populated.ThankssqlSel = "SELECT * FROM vwAllMatrixVehicles WHERE created >= '2007/8/9'"Dim db As New SQLDatabaseResponse.Write(SQL)Trydb.openDB()dsResults = db.GetDataSet(sqlSel).Tables(0)Catch ex As ExceptionResponse.Write(ex.Message)Finallydb.closeDB()End TryMy database classPublic Class SQLDatabase Public Enum DatabaseType MatrixDatabase CapDatabase CapCommercialDatabase MembershipDatabase MatrixTESTSERVERDATABASE End Enum Private conn As SqlConnection Public Sub New(Optional ByVal type As DatabaseType = DatabaseType.MatrixDatabase) Select Case type Case DatabaseType.MatrixDatabase conn = New SqlConnection(myConnStr) Case DatabaseType.CapDatabase conn = New SqlConnection(myConnStr) Case DatabaseType.CapCommercialDatabase conn = New SqlConnection(myConnStr) Case DatabaseType.MembershipDatabase conn = New SqlConnection(myConnStr) Case DatabaseType.MatrixTESTSERVERDATABASE conn = New SqlConnection(myConnStr) End Select End Sub Public Sub openDB() If conn.State = ConnectionState.Closed Then conn.Open() End If End Sub Public Sub closeDB() If Not (conn.State = ConnectionState.Closed) Then conn.Close() End If End Sub Public Function GetDataSet(ByVal sql As String) As DataSet Try Dim myCmd As SqlCommand = New SqlCommand(sql, conn) Dim DA As SqlDataAdapter = New SqlDataAdapter DA.MissingSchemaAction = MissingSchemaAction.AddWithKey DA.SelectCommand = myCmd Dim ds As DataSet = New DataSet DA.Fill(ds, "table") Return ds Catch ex As Exception Throw New Exception(ex.Message) End Try End FunctionEnd class |
 |
|
|
|