Solved

Date format in SQL query string

Posted on 2014-12-14
10
246 Views
Last Modified: 2014-12-27
I have a slight problem with date formating.

I want to lookup a vb,net date in an SQL table and i keep getting errors, the first part of teh code (SET) is fine, the issue is the StartDate and CycleStart which are both dates.  

SQL.RunQuery("UPDATE temptbl_Stops SET DowntimeCode = '" & dc & "' WHERE StartDate =  & CycleStart &  ")

Any help most welcome.
0
Comment
Question by:SweetingA
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40498640
Pass the date as a string, formatted as yyyymmdd (a language-neutral literal).

SQL.RunQuery("UPDATE temptbl_Stops SET DowntimeCode = '" & dc & "' WHERE StartDate = '"  & format(CycleStart,"yyyymmdd") &  "'")
0
 

Author Comment

by:SweetingA
ID: 40498665
Does not error now but still yields no result

Datetime shown Vb is #1/26/2014 3:02:50 PM# and teh date i need to compare to in SQL is 2014-09-13 15:04:50.000.

Thanks for the help so far.
0
 
LVL 18

Expert Comment

by:Simon
ID: 40498765
Ah, you're comparing datetimes rather than dates. The two datetimes you want to compare are not identical. Was that deliberate or a typo?

If you want to get a datetime within a one day range...
SQL.RunQuery("UPDATE temptbl_Stops SET DowntimeCode = '" & dc & "' WHERE StartDate between '"  & format(CycleStart,"yyyymmdd") &  "' AND  Dateadd(dd,1,'" &  format(CycleStart,"yyyymmdd") & "')")

if you want the exact comparison, I think you want this
SQL.RunQuery("UPDATE temptbl_Stops SET DowntimeCode = '" & dc & "' WHERE StartDate = '"  & format(CycleStart,"yyyymmdd hh:mm:ss") &  ":000'")
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 11

Expert Comment

by:LordWabbit
ID: 40498766
I agree with SimonAdept, in that using an ISO standard date of yyyy-mm-dd should resolve all date issues, the fact that you are not getting any results probably means that the date is not being stored as a datatype of datetime in the database, but is probably being stored as a varchar.  

Also never use inline SQL, EVER!  It is one of the most common forms of attack on the internet (SQL Injection)

Even if it is an internal intranet site it's a bad habit and should never be used, use parameters instead and if you absolutely have to continue using inline SQL at least sanitize your inputs.

If the datetime is stored as varchar casting it to a datetime could resolve your problems
SQL.RunQuery("UPDATE temptbl_Stops SET DowntimeCode = '" & dc & "' WHERE CAST(StartDate AS DATETIME) = '"  & format(CycleStart,"yyyymmdd") &  "'")

Open in new window


This will not work if all of the StartDate's are not valid datetime values.
0
 

Author Comment

by:SweetingA
ID: 40498873
Datestime is stored as datetime not varchar
0
 

Author Comment

by:SweetingA
ID: 40498884
I have tried several different formats including adding - between year/m/d but makes no difference, no errors but does nothing
0
 
LVL 11

Expert Comment

by:LordWabbit
ID: 40499045
Well to test perhaps run the statement directly against the database.  Is there a rollback in place?  Are you seeing a "records affected count" more than once, which is signs of a trigger changing your update?
0
 

Author Comment

by:SweetingA
ID: 40499141
Looks like you can only reformat a datetime to a string and not to a date time, once this is done no direct comparison can be made.

An sql count statement works only if i type in the dtae, if i type in the variable it returns nothing.

I am very much a novice so you will have to be patient.
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40499262
A sql select count(*) query is a good way to test... as you have done. Yes, the format() function returns a string, but if it's recognisable as a date, SQL Server will treat it as one, though not always the date you expect.
If you're getting no results from your count(*), try the > operator instead of the = operator and return a list of matching values rather than just a count. This might illustrate the reason for the unexpected result.
Do you have access to the design of the table you're querying? Can you check which datatype the column is?
Note that there are six possible date/time datatypes in MSSQL 2008:

Even yyyy-mm-dd is not foolproof for the datetime and smalldatetime datatypes. See this blog post among others. On my own system it gets interpreted as yyyy-dd-mm, unless I use the full ISO spec:
YYYY-MM-DDThh:nn[:ss[:mmm]]
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40499663
Does not error now but still yields no result

Datetime shown Vb is #1/26/2014 3:02:50 PM# and teh date i need to compare to in SQL is 2014-09-13 15:04:50.000.

#1/26/2014 3:02:50 PM# can never equal 2014-09-13 15:04:50.000 no matter what formats you use; they just are not equal.

1/26/2014 is: 2014-01-26 ; and  2014-01-26 <> 2014-09-13 ; plus the times don't match either.

Please provide some sample data and the expected result. Make sure datetime values contain as much precision as possible.

Remember the precision of datetime is something like 3.5 milliseconds (from memory) so getting 2 datetime values  that are equal can be like trying to split an arrow that hit the bullseye.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net Report Printing Issue 3 33
Addition to SQL for dynamic fields 6 38
HTML - Color not displaying correctly in EMAIL. 6 37
Help with exporting to excel 4 27
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question