ASP / SQL - Dates being echoed different to what the database - sql

I have a simple sql script in my classic ASP which is as follows
sql = "SELECT date FROM sales WHERE userid=1"
Set RS = Conn.Execute(sql)
date = RS.Fields("date")
Response.Write date
This is a broken down version, it works, but the results are different.
Im looking at the database using SQL Server and it is showing me
2014-10-05 15:06:00
But when the Response.Write runs, it is displaying it as
10/5/2014 3:06:00 PM
How can i use Response.Write to display it exactly as it is in the database?
Cheers,

use
sql = "SELECT CONVERT(VARCHAR(19), date, 120) as date FROM sales WHERE userid=1"

Related

SQL DATEDIFF Function returns wrong values

Im executing following sql statement to get the sum values using date difference as a condition. "t_QueryDemand" is the table name and "DESIRED_SHIP_DATE" is the value I compare with current system date. But, I always get a date difference higher than 44100, even though the actual difference is between 0-60. DESIRED_SHIP_DATE data type is datetime2(7). I'm using MS SQL server database. What could be the issue.
Dim curDate As Date = Date.Today()
Dim strSql7DayDemand As date= "SELECT SUM(ORDER_QTY) AS ORDQTY, (TOTAL_SHIPPED_QTY) AS SHIPQTY, DATEDIFF(day," & curDate & ",DESIRED_SHIP_DATE),DESIRED_SHIP_DATE as DIFF from t_QueryDemand where ID='" & Trim(txtPartID.Text) & "' AND DATEDIFF(DAY," & curDate & ",DESIRED_SHIP_DATE) >" &
Instead of using a injection use the SQL function GETDATE()
DATEDIFF(day,GETDATE(),DESIRED_SHIP_DATE)
This will calculate the date on the server instead of locally like you are currently doing.
As has been pointed out you have a serious security vulnerability -- you need to use parameterized queries and not string concatenation or you are vulnerable to an SQL injection attack.
It will also make your code more robust -- right now you have txtPartID.Text and you are just putting that into your query but what if someone enters an non-number in this field -- you have no way of catching that now. With a parameterized query of a numeric type you would catch this problem when you converted the data entry to a number.

Defining the result of a SQL Statement

I want to get the date from this SQL statement and use that as an ExpirationDate in asp classic so I can use that date in an if/then (conditional) statement. Lost on how to retrieve that. Appreciate the help.
strSQL = "SELECT SUM (Credits)[Amount] from TableName where id = '" & id & "' and date >= '4/1/2019' and date >= dateadd(day,-360,getdate())"
You can add another SQL query with dateadd(day,-360,getdate()) and get the expiration date. However, as you are getting the date based on your current date you can get it using ASP instead of SQL (i.e. DateAdd("d",1,Now())) assuming your ASP server and SQL server are in the same time zone.

Access Runtime Linked Table Query Date Format

I have the following query executed on a Linked table in Access:
Select * From table WHERE DateField = #18-Dec-2016#
This date where clause is created with the following VBA code:
strWHERE = strWHERE & "DateField = #" & VBA.Format(txtDate, "dd-mmm-yyyy") & "#"
When using the full version of Access 2016 it it sent to SQL as:
Select * From table WHERE DateField = {d 18-Dec-2016}
This works! However, when using the Access 2016 runtime, it is sent to SQL as:
Select * From table WHERE DateField = {ts 18-Dec-2016}
and it fails.
I am using SQL Server Profiler to find the exact SQL being sent.
Any ideas?
EDITS:
Tried changing format to yyyy-mm-dd
Tried added "00:00:00" to the VBA code building the where clause, and this does not change what I see sent to SQL through the profiler.
SOLVED:
For some reason, the behaviour of linked table queries is different between the full version of Access and the 2016 Runtime.
In the Runtime version, the data type of the field in the linked table is used to format the query. So because the DateField was DateTime, it passed the where clause as ts. BY creating a view and formatting the date as just a date, this was solved.

Change date format in SQL for ASP

I have the following code:
"SELECT top 1 * FROM CensusFacility_Records WHERE Division_Program = 'Division 1' AND JMS_UpdateDateTime = '" & date & "'"
The date format in column JMS_UpdateDateTime is:
8/22/2013 12:00:07 AM
How can I make sure that my "date" in the query is converted to the correct time format?
My date variable in my SQL query is a real/date time. I would like for it to match the format within the JMS_UpdateDateTime field.
If your database table is small enough, you can cast the value to an actual datetime inside your query, and pass the value in as a datetime parameter, so you're comparing dates instead of comparing strings.
If your table has a decent amount of records, don't do this, because it will be a performance hog.
SELECT top 1 *
FROM CensusFacility_Records
WHERE Division_Program = 'Division 1'
AND cast(JMS_UpdateDateTime as datetime) = #dateParam
I believe SQL Server will be able to read the string that's in your database and automatically cast it properly, assuming your server settings are standard.
But in any case, use parameterized SQL instead of passing in a string like you've got.
The format of your SQL DateTime is actually a bit of a red herring here - it can be displayed in any way the front end (e.g. Management Studio) chooses. The important thing here is that your date variable is in an unambiguous format. With this in mind I'd recommend using the ISO 8601 date/time format, e.g. date.ToString("o") (assuming date is a DateTime).

compare date/time via VBA with date/time in Access DB

How to compare a date/time via VBA with a date/time in an Access DB?
The query I use
adoRS.Open "SELECT * FROM currentpositions WHERE ((currentpositions.
[dateLT])=" & "#" & date_from_message & "#" & ")", adoConn, adOpenStatic,
adLockOptimistic
I only achieve to compare a date.
Anybody an idea?
Regards
Camastanta
adoRS.Open "SELECT * FROM currentpositions WHERE DateValue(currentpositions.
[dateLT]) = DateValue(" & "#" & date_from_message & "#)", adoConn, adOpenStatic,
adLockOptimistic
See, if the above helps.
DateValue extracts the date part from a given date/time. So, it can be used to compare date, ignoring the time part.
IMHO you should never build your SQL statements using string concatenation. Instead use parameterized SQL queries. This will save you from problems like the one you are facing with date/time comparison.
Does your system have dates in either yyyy-mm-dd or dd-mm-yyyy format? If so see Return Dates in US #mm/dd/yyyy# format to convert your dates in mm-dd-yyyy so Access can work with them properly.