PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 1.00 average.

Convert String to Datetime in SQL

 
 
lhtan123
Guest
Posts: n/a
 
      12th Jan 2009
I have a form with two combo boxes and one textbox which allows user to enter
a date. If this criteria is used to filter records from a query by an sql
string, it runs into error.

I guess it's becos' Date is a string value while the table design has it in
a Date/Time format.

I've tried to look through some examples online but couldn't find a suitable
one to solve my problem. My SQL statement is something like below:

If Not IsNull(Me.txtDate) Then
strWhere = strWhere & "([Date] = " & CDate(Me.txtDate) & ") AND "
End If

 
Reply With Quote
 
 
 
 
Tom van Stiphout
Guest
Posts: n/a
 
      12th Jan 2009
On Mon, 12 Jan 2009 00:48:01 -0800, lhtan123
<(E-Mail Removed)> wrote:

Since you posted in this NG I am assuming your Back-End is SQL Server.
It requires date values (and text values) to be wrapped in
single-quotes.

-Tom.
Microsoft Access MVP


>I have a form with two combo boxes and one textbox which allows user to enter
>a date. If this criteria is used to filter records from a query by an sql
>string, it runs into error.
>
>I guess it's becos' Date is a string value while the table design has it in
>a Date/Time format.
>
>I've tried to look through some examples online but couldn't find a suitable
>one to solve my problem. My SQL statement is something like below:
>
>If Not IsNull(Me.txtDate) Then
> strWhere = strWhere & "([Date] = " & CDate(Me.txtDate) & ") AND "
>End If

 
Reply With Quote
 
Paul Shapiro
Guest
Posts: n/a
 
      13th Jan 2009
CDate is an Access function, and doesn't exist in SQL Server. You want to
build a string. The query you want might be:
Where [TheDateField] = '2008.01.12'

SQL Server always interprets dates correctly if given yyyy.mm.dd and other
than that I think it usually accepts American date format, mm/dd/yyyy, even
if the language is set to something other than US. You can find details in
SQL Books Online. I just make a habit of using the yyyy.mm.dd format to
avoid any language/region issues.

If you're using Date as a field name, you should change it now if you can
since it's a reserved word in most SQL dialects.


"Tom van Stiphout" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Mon, 12 Jan 2009 00:48:01 -0800, lhtan123
> <(E-Mail Removed)> wrote:
>
> Since you posted in this NG I am assuming your Back-End is SQL Server.
> It requires date values (and text values) to be wrapped in
> single-quotes.
>
> -Tom.
> Microsoft Access MVP
>
>
>>I have a form with two combo boxes and one textbox which allows user to
>>enter
>>a date. If this criteria is used to filter records from a query by an sql
>>string, it runs into error.
>>
>>I guess it's becos' Date is a string value while the table design has it
>>in
>>a Date/Time format.
>>
>>I've tried to look through some examples online but couldn't find a
>>suitable
>>one to solve my problem. My SQL statement is something like below:
>>
>>If Not IsNull(Me.txtDate) Then
>> strWhere = strWhere & "([Date] = " & CDate(Me.txtDate) & ") AND "
>>End If


 
Reply With Quote
 
lhtan123
Guest
Posts: n/a
 
      13th Jan 2009
It's not connected to SQL Server but a shared drive. Sorry to have posted my
question here. I'm using VBA to construct the SQL string.

So should post it to "DAO VBA" ?

"Paul Shapiro" wrote:

> CDate is an Access function, and doesn't exist in SQL Server. You want to
> build a string. The query you want might be:
> Where [TheDateField] = '2008.01.12'
>
> SQL Server always interprets dates correctly if given yyyy.mm.dd and other
> than that I think it usually accepts American date format, mm/dd/yyyy, even
> if the language is set to something other than US. You can find details in
> SQL Books Online. I just make a habit of using the yyyy.mm.dd format to
> avoid any language/region issues.
>
> If you're using Date as a field name, you should change it now if you can
> since it's a reserved word in most SQL dialects.
>
>
> "Tom van Stiphout" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > On Mon, 12 Jan 2009 00:48:01 -0800, lhtan123
> > <(E-Mail Removed)> wrote:
> >
> > Since you posted in this NG I am assuming your Back-End is SQL Server.
> > It requires date values (and text values) to be wrapped in
> > single-quotes.
> >
> > -Tom.
> > Microsoft Access MVP
> >
> >
> >>I have a form with two combo boxes and one textbox which allows user to
> >>enter
> >>a date. If this criteria is used to filter records from a query by an sql
> >>string, it runs into error.
> >>
> >>I guess it's becos' Date is a string value while the table design has it
> >>in
> >>a Date/Time format.
> >>
> >>I've tried to look through some examples online but couldn't find a
> >>suitable
> >>one to solve my problem. My SQL statement is something like below:
> >>
> >>If Not IsNull(Me.txtDate) Then
> >> strWhere = strWhere & "([Date] = " & CDate(Me.txtDate) & ") AND "
> >>End If

>
>

 
Reply With Quote
 
Tom van Stiphout
Guest
Posts: n/a
 
      13th Jan 2009
On Mon, 12 Jan 2009 18:00:04 -0800, lhtan123
<(E-Mail Removed)> wrote:

That would be better, but I think between Paul and myself you should
already have the solution.

-Tom.
Microsoft Access MVP


>It's not connected to SQL Server but a shared drive. Sorry to have posted my
>question here. I'm using VBA to construct the SQL string.
>
>So should post it to "DAO VBA" ?
>
>"Paul Shapiro" wrote:
>
>> CDate is an Access function, and doesn't exist in SQL Server. You want to
>> build a string. The query you want might be:
>> Where [TheDateField] = '2008.01.12'
>>
>> SQL Server always interprets dates correctly if given yyyy.mm.dd and other
>> than that I think it usually accepts American date format, mm/dd/yyyy, even
>> if the language is set to something other than US. You can find details in
>> SQL Books Online. I just make a habit of using the yyyy.mm.dd format to
>> avoid any language/region issues.
>>
>> If you're using Date as a field name, you should change it now if you can
>> since it's a reserved word in most SQL dialects.
>>
>>
>> "Tom van Stiphout" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > On Mon, 12 Jan 2009 00:48:01 -0800, lhtan123
>> > <(E-Mail Removed)> wrote:
>> >
>> > Since you posted in this NG I am assuming your Back-End is SQL Server.
>> > It requires date values (and text values) to be wrapped in
>> > single-quotes.
>> >
>> > -Tom.
>> > Microsoft Access MVP
>> >
>> >
>> >>I have a form with two combo boxes and one textbox which allows user to
>> >>enter
>> >>a date. If this criteria is used to filter records from a query by an sql
>> >>string, it runs into error.
>> >>
>> >>I guess it's becos' Date is a string value while the table design has it
>> >>in
>> >>a Date/Time format.
>> >>
>> >>I've tried to look through some examples online but couldn't find a
>> >>suitable
>> >>one to solve my problem. My SQL statement is something like below:
>> >>
>> >>If Not IsNull(Me.txtDate) Then
>> >> strWhere = strWhere & "([Date] = " & CDate(Me.txtDate) & ") AND "
>> >>End If

>>
>>

 
Reply With Quote
 
raibeart
Guest
Posts: n/a
 
      16th Jan 2009
since you are using an ADP, you should be using a stored procedure
that will take all the parameters you are developing and then
displaying the data based on it. There is no DAO.QueryDef in an ADP
project. All of the queries are SQL Server views.

If you are saving the entire WHERE clause in this table for use later,
then you could do a stored procedure something like this:

CREATE PROCEDURE dbo.pQueryName
@WhereClause VARCHAR(1000),
@OrderByClause VARCHAR(500)
AS
BEGIN
DECLARE @Sql NVARCHAR(4000)
SET @Sql = 'SELECT * FROM tblNames ' + @WhereClause + @OrderByClause

EXEC sp_executesql @Sql
END

If you implement this, you should also be checking for a ; in either
the where clause or the order by clause and default them to an empty
string if either one is found so you do not get an injection attack.

Robert

On Fri, Jan 16, 2009 at 2:17 AM, microsoft.public.access.adp.sqlserver
group <(E-Mail Removed)> wrote:

== 1 of 2 ==
Date: Thurs, Jan 15 2009 10:08 am
From: Guy Kerr


This sounded like something that wouldn't be too challenging but for
some
reason I can't figure out the best way to do this.

I have an unbound form that contains Listboxes, radio buttons, unbound
controls, checkboxes etc. that correspond to fields in my database.
This
form is designed to build a query - or at least the WHERE CLAUSE.
Once the
user checks the boxes, fills in the unbound controls they click a
button to
generate a query. For example if the user fills in Kerr* in LastName
field,
checks off the >18 checkbox, select 'CA' from the State listbox then
clicks
the GENERATE QUERY button a record is created in a table that is
essentially
the Select Statement based on what the user selected on the form. So
the
text generated would be "Lastname like '%Kerr' and Age > 18 and State
= 'CA'".

Then when a user opens a form it does a DLookup based on the user name
to
search the table for the latest query to correspond to that user. It
then
uses this to apply the ServerFilter to the form which limits the
dataset to
what the defined query is. This is not relevant to what I'm trying to
accomplish, I just wanted to explain the function of the form and the
table
storing the queries.

What I WANT to do is export data to TAB Delim or CSV from the same
table
based on any of the queries stored in this table. I was looking at
DoCmd.TransferText but it looked to me that you had to refer to a
named
query. I was also considering using a report then export from that.
This
way the user can see the data before choosing to Export.

If anyone has any ideas on how best to do this I would be very much
appreciative.

Guy




== 2 of 2 ==
Date: Thurs, Jan 15 2009 10:33 am
From: "Paul Shapiro"


You can use a named query and just update its sql before exporting.
Open a
DAO.QueryDef, and then I believe the sql is one of the properties. Set
it to
your new sql string, save the querydef, and then export it.

"Guy Kerr" <(E-Mail Removed)> wrote in message
news:396D5238-2109-4CFD-ACEC-(E-Mail Removed)...
> This sounded like something that wouldn't be too challenging but for some
> reason I can't figure out the best way to do this.
>
> I have an unbound form that contains Listboxes, radio buttons, unbound
> controls, checkboxes etc. that correspond to fields in my database. This
> form is designed to build a query - or at least the WHERE CLAUSE. Once
> the
> user checks the boxes, fills in the unbound controls they click a button
> to
> generate a query. For example if the user fills in Kerr* in LastName
> field,
> checks off the >18 checkbox, select 'CA' from the State listbox then
> clicks
> the GENERATE QUERY button a record is created in a table that is
> essentially
> the Select Statement based on what the user selected on the form. So the
> text generated would be "Lastname like '%Kerr' and Age > 18 and State =
> 'CA'".
>
> Then when a user opens a form it does a DLookup based on the user name to
> search the table for the latest query to correspond to that user. It then
> uses this to apply the ServerFilter to the form which limits the dataset
> to
> what the defined query is. This is not relevant to what I'm trying to
> accomplish, I just wanted to explain the function of the form and the
> table
> storing the queries.
>
> What I WANT to do is export data to TAB Delim or CSV from the same table
> based on any of the queries stored in this table. I was looking at
> DoCmd.TransferText but it looked to me that you had to refer to a named
> query. I was also considering using a report then export from that. This
> way the user can see the data before choosing to Export.
>
> If anyone has any ideas on how best to do this I would be very much
> appreciative.
>
> Guy


 
Reply With Quote
 
raibeart
Guest
Posts: n/a
 
      16th Jan 2009
Sorry guys I posted this to the wrong thread.

On Jan 16, 7:34*am, raibeart <raibe...@gmail.com> wrote:
> since you are using an ADP, you should be using a stored procedure
> that will take all the parameters you are developing and then
> displaying the data based on it. There is no DAO.QueryDef in an ADP
> project. All of the queries are SQL Server views.
>
> If you are saving the entire WHERE clause in this table for use later,
> then you could do a stored procedure something like this:
>
> CREATE PROCEDURE dbo.pQueryName
> * * *@WhereClause VARCHAR(1000),
> * * *@OrderByClause VARCHAR(500)
> AS
> * * *BEGIN
> DECLARE @Sql NVARCHAR(4000)
> SET @Sql = 'SELECT * FROM tblNames ' + @WhereClause + @OrderByClause
>
> EXEC sp_executesql @Sql
> * * *END
>
> If you implement this, you should also be checking for a ; in either
> the where clause or the order by clause and default them to an empty
> string if either one is found so you do not get an injection attack.
>
> Robert


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert the String to Datetime Kylin Microsoft ASP .NET 2 14th May 2005 02:35 AM
How do I convert a string to DateTime? Tom Microsoft C# .NET 1 12th May 2004 11:39 PM
Re: Best Way To Convert String to DateTime Tom Porterfield Microsoft Dot NET 1 16th Apr 2004 09:46 PM
Convert DateTime to string? Tony Microsoft C# .NET 3 25th Mar 2004 05:49 AM
How to convert string to DateTime using C# David Microsoft Dot NET Compact Framework 2 6th Oct 2003 04:45 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:16 AM.