Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SELECT above a certain date

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 work

SELECT * FROM vwAllMatrixVehicles
WHERE created > 1/1/2008

It returns all records even though nothing has a created date in 2008

Have 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.
Go to Top of Page

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.
Go to Top of Page

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 work

SELECT * FROM vwAllMatrixVehicles
WHERE created > 1/1/2008

It returns all records even though nothing has a created date in 2008

Have I got the syntax wrong?

Thanks



Date values should be expressed within single quotes
Otherwise 1/1/2008 would become 0 and you will get all data

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-08-16 : 05:50:25


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

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
Go to Top of Page

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 problems

Duane.
Go to Top of Page

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?
Go to Top of Page

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.
Go to Top of Page

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.

Thanks


sqlSel = "SELECT * FROM vwAllMatrixVehicles WHERE created >= '2007/8/9'"

Dim db As New SQLDatabase
Response.Write(SQL)
Try
db.openDB()
dsResults = db.GetDataSet(sqlSel).Tables(0)
Catch ex As Exception
Response.Write(ex.Message)
Finally
db.closeDB()
End Try

My database class

Public 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 Function
End class
Go to Top of Page
   

- Advertisement -