Date variable in vba using sql query - date

I have a query in VB6:
"select plate_no from INFO where date_time between #07-10-2012 01:13:17# and #10/10/2012 11:30:25#"
My Access database table has the column with datatype Data/Time in general format.
How do I modify the query above to use variables like:
Public t1 As Date
Public t2 As Date

I'm doing it from memory, may have to tweak it to compile:
Public t1 as Date
Public t2 as Date
t1 = #10/07/2012 01:13:17# '7th of October 2012
t2 = #10/10/2012 11:30:25# '10th of October 2012
sql = "select plate_no from INFO where date_time between #" + Format(t1, "YYYY-MM-DD HH:MM:SS") + "# and #" + Format(t2, "YYYY-MM-DD HH:MM:SS") + "#"

Related

#Query for selecting date from datetime

I have a JPA mapped "event" object that includes a timestamp column. I am trying to select all events that occurred on a specific date. To do this I need to ignore the time component of the timestamp. I am not sure that this is possible using non database specific functions? Something like this fails to select:
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :startDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Date startDate);
try the following
#Query("SELECT r FROM BioChemRequest r WHERE r.pasId = :pasid AND r.observationDate BETWEEN :startDate AND :endDate")
public List <BioChemRequest> find(#Param("pasid") String pasid , #Param("startDate") Calendar startDate, #Param("endDate") Calendar endDate);
where startDate is a calendar with hour 00:00:00 000
and endDate is a calendar with hour 23:59:59 999

Casting a datetime column as date 'yyyy-mm-dd' in SQLdf

Running this query in SQLdf and when I go to view or filter based on date I'm noticing the date column is in datetime format instead of 'yyyy-mm-dd'. I tried casting the column as a date with the correct format but didn't work. is there a way to format it in the query or update that column in the data frame after?
BUS_SERV_PROMO_ROLL_FCST <-
sqldf(
"
SELECT
D.DATE
,D.DATA_ROLLS_FORECAST
,VO.VOICE_ROLLS_FORECAST
,VI.VIDEO_ROLLS_FORECAST
,D.DATA_ROLLS_UPPER_FORECAST
,VO.VOICE_ROLLS_UPPER_FORECAST
,VI.VIDEO_ROLLS_UPPER_FORECAST
,D.DATA_ROLLS_LOWER_FORECAST
,VO.VOICE_ROLLS_LOWER_FORECAST
,VI.VIDEO_ROLLS_LOWER_FORECAST
,D.DATA_ROLLS_FORECAST_TREND
,VO.VOICE_ROLLS_FORECAST_TREND
,VI.VIDEO_ROLLS_FORECAST_TREND
,D.DATA_ROLLS_UPPER_FORECAST_TREND
,VO.VOICE_ROLLS_UPPER_FORECAST_TREND
,VI.VIDEO_ROLLS_UPPER_FORECAST_TREND
,D.DATA_ROLLS_LOWER_FORECAST_TREND
,VO.VOICE_ROLLS_LOWER_FORECAST_TREND
,VI.VIDEO_ROLLS_LOWER_FORECAST_TREND
FROM
dataforecast as D
LEFT JOIN
voiceforecast as VO
ON VO.DATE = D.DATE
LEFT JOIN
videoforecast as VI
ON VI.DATE = D.DATE
"
)
enter image description here

how to use utc in postgres timestamp with jdbc PrepareStatement parameter?

I'am using timestamp data type on pg9.4, but there come very strange problem with to_json.
now i am in Shanghai, UTC+08:00 timezone.
see below:
conn.createStatement().execute("set time zone 'UTC'");
String sql = "select to_json(?::timestamp) as a, to_json(current_timestamp::timestamp) as b";
PreparedStatement ps = conn.prepareStatement(sql);
Timestamp timestamp = new Timestamp(new Date().getTime());
ps.setTimestamp(1, timestamp);
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println("a " + rs.getString("a") + ", b " + rs.getString("b"));
}
output:
a "2015-09-24T16:52:42.529", b "2015-09-24T08:53:25.468191"
it's mean when i pass a TIMESTAMP parameter to pg with jdbc, the timezone is still in shanghai, not UTC.
this problem is not due to to_json function, i have make a table with one timestamp column, this problem still exits, the code of above is shortest sample.
how to let's all timestamp work in UTC timezone?
You need to set Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));, Before you create your prepared statement.
UPDATED CODE SNIPPET
conn.createStatement().execute("set time zone 'UTC'");
String sql = "select to_json(?::timestamp) as a, to_json(current_timestamp::timestamp) as b";
Calendar tzCal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
PreparedStatement ps = conn.prepareStatement(sql);
Timestamp timestamp = new Timestamp(new Date().getTime());
ps.setTimestamp(1, timestamp);
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println("a " + rs.getString("a") + ", b " + rs.getString("b"));
}
This way you will be able to set timezone to UTC in your JDBC call.
If you want to run the whole application/JVM in UTC, set -Duser.timezone=UTC flag while starting JVM.
HTH.

Change date format from 20140511 to DD-MM-YYYY SQL 2008

I have 2000+ rows returned with the query below but the date needs to be changed so it's more readable. Is there a way to convert the date from being returned as 20140511 to DD-MM-YYYY in the query below?
Select Jobname AS OptimiseJobs, RunDate AS Date
from tablename
where jobname like '%Optimise'
thanks!
IF you are using SQL server then try below query:
SELECT Jobname AS OptimiseJobs,
CONVERT(varchar(20),CONVERT(date, CONVERT(varchar(8), RunDate), 112),105) AS Date
FROM tablename
WHERE jobname LIKE '%Optimise'
In sql-server
Select Jobname AS OptimiseJobs, cast(RunDate as date) AS Date
from tablename
where jobname like '%Optimise'
Demo

PostgreSQL, getting min and max date from text column

I have such situation in table:
1 01.02.2011
2 05.01.2011
3 06.03.2012
4 07.08.2011
5 04.03.2013
6 06.08.2011
7
8 02.02.2013
9 04.06.2010
10 10.10.2012
11 04.04.2012
where first column is id (INT) and second column is TEXT in which may be written date in format 'dd.mm.yyyy'.
I would like to get:
1) lowest entered date in whole table and highest entered date in whole table.
2) lowest entered date in year 2012 and highest entered date in year 2012.
Lowest and highest date in year may be a same (like for year 2010) or field may be empty (like in row 7).
I am tying to use TO_TIMESTAMP but unsuccessfully.
Example:
SELECT (TO_TIMESTAMP(mydatetxt, 'DD.MM.YYYY'))
FROM " & myTable & "
ORDER BY (TO_TIMESTAMP(mydatetxt, 'DD.MM.YYYY')) ASC LIMIT 1
Also with BETWEEN I don't get wanted result.
How to write those two queries?
SOLUTION:
Thanks for all suggestions.
Igor's solution is most suitable and simple enough for me.
Dim sqlText As String = "SELECT min(to_date(nullif(mydate,''), 'DD.MM.YYYY')) FROM " & mytable
cmd1 = New NpgsqlCommand(sqlText, conn)
min = CDate(cmd1.ExecuteScalar())
If Not IsDate(min) Then
min = CType(CDate("01.01." & myyear) & " 00:00:00", Date)
End If
fromdate = CType(CDate(min), Date)
sqlText = "SELECT max(to_date(mydate, 'DD.MM.YYYY')) FROM " & mytable
cmd1 = New NpgsqlCommand(sqlText, conn)
max = CDate(cmd1.ExecuteScalar())
If Not IsDate(max) Then
max = CType(CDate("31.12." & myyear) & " 23:59:59.9999", Date)
End If
todate = CType(CDate(max), Date)
Try something like:
SELECT max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')),
min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY'))
FROM table_name;
SELECT max(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')),
min(to_date(nullif(mydatetxt,''), 'DD.MM.YYYY'))
FROM table_name
WHERE date_part('year',to_date(nullif(mydatetxt,''), 'DD.MM.YYYY')) = 2012;