Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > Database and Reporting > String to date


Reply
 
Thread Tools Display Modes
  #1  
Old 05-03-2012, 08:16 AM
AlexanderJames AlexanderJames is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 381
Default String to date


Hi All

Ive got the horrble task of interfacing to an application that stores it's date and time as a string with the database fields set to char.I have written the below query and i dont get any errors but i also dont get any data back.

Code:
Dim DateToday As Date
    DateToday = Format(Now, "dd/mm/yyyy")
    
    gstrSQL = "SELECT CONVERT(datetime,C_Date,103) as NewDate, C_Time, L_UID, C_Name FROM tEnter WHERE C_Date = '" & DateToday & "'"
This is new territory for me so any assistance would be greatly appreciated.

Many Thanks
AJ
__________________
www.necsys-software.com
Reply With Quote
  #2  
Old 05-03-2012, 09:25 AM
TheRealTinTin's Avatar
TheRealTinTin TheRealTinTin is offline
Junior Contributor
 
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 328
Default

Try the following instead
Code:
gstrSQL = "SELECT CONVERT(datetime,C_Date,103) as NewDate, C_Time, L_UID, C_Name FROM tEnter WHERE C_Date = #" & DateToday & "#;"
__________________
Artificial Intelligence is no match for natural stupidity
Reply With Quote
  #3  
Old 05-03-2012, 09:52 AM
AlexanderJames AlexanderJames is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 381
Default

Hi TherealTinTin

Thanks for your reply.
I tried your plan but i get no data back either, i have no idea why this company stores it's date as a string, makes no sence and makes it difficult.
__________________
www.necsys-software.com
Reply With Quote
  #4  
Old 05-03-2012, 12:32 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

Which database/version are you using? It looks like SQL server (from your code, Alexander,) but, which version?

You may want to apply the convert in the where clause as well, rather than convert the current date to a string for the comparison.

i.e.
Code:
"... WHERE DATEPART(CONVERT(datetime,C_Date,103)) = DATEPART(GETDATE())"
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown

Last edited by loquin; 05-03-2012 at 12:41 PM.
Reply With Quote
  #5  
Old 05-04-2012, 09:39 AM
AlexanderJames AlexanderJames is offline
Junior Contributor
 
Join Date: Sep 2005
Posts: 381
Default

Hi Loquin

Thanks for your reply, I am using sql express 2008. I managed to get that problem solved by declaring my date as a string then formatting it "yyyymmdd" as this is how it appears in the database. These guys also store the time in a field called C_Time also as a string and i simply cannot get this time out. I have the following code.

Code:
Dim DateToday As String
    DateToday = Format(Date, "yyyymmdd")
    ActiveReadUnis "SELECT CONVERT(datetime,C_Date,103) as NewDate, CONVERT(Char,C_Time,108) as NewTime , L_UID, C_Name FROM tEnter WHERE C_Date = '" & DateToday & "' and C_Name <> '" & " " & "'"
C_Time always comes out as 00:00:00 no matter what i do, the above convert statement should do the trick but no luck. Any ideas ?

Thanks
AJ
__________________
www.necsys-software.com
Reply With Quote
  #6  
Old 05-10-2012, 02:53 PM
loquin's Avatar
loquin loquin is offline
Google Hound

Retired Moderator
* Guru *
 
Join Date: Nov 2001
Location: Arizona, USA
Posts: 12,378
Default

Could you copy/paste a few examples of the time data? (include times with hours in the range of 0 through 9, hours in the range of 10-12, minutes in the range of 0-9 , and minutes in the range of 10-59. And, if seconds are included, with seconds in the range 0-9 and seconds in the range of 10-59.)
__________________
Lou
"I have my standards. They may be low, but I have them!" ~ Bette Middler
"It's a book about a Spanish guy called Manual. You should read it." ~ Dilbert
"To understand recursion, you must first understand recursion." ~ unknown
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->