How to convert a DATE column to DATETIME in SQL, querying an Access database? - sql

I have a SQL query to where I need to convert a DATE column to DATETIME from an Access database.
The columns to be converted are:
CHECKINOUT.CHECKTIME
CHECKINOUT.DefaultIn
CHECKINOUT.DefaultOut
This is my query:
SELECT
USERINFO.NAME, USERINFO.EmployeeCode, CHECKINOUT.CHECKTYPE,
CHECKINOUT.DefaultIn, CHECKINOUT.DefaultOut, CHECKINOUT.DefaultBreckIn,
CHECKINOUT.DefaultBreakOut, CHECKINOUT.CHECKTIME, USERINFO.TITLE
FROM
(CHECKINOUT
INNER JOIN USERINFO ON CHECKINOUT.USERID = USERINFO.USERID)
WHERE
(CHECKINOUT.CHECKTIME >= ?)
AND (CHECKINOUT.CHECKTIME <= ?)
AND (CHECKINOUT.USERID = ? OR ? = - 1)
AND (CHECKINOUT.DefaultIn <= CHECKINOUT.CHECKTIME OR
CHECKINOUT.DefaultOut >= CHECKINOUT.CHECKTIME )
What's the best way?
Thanks

You have to use the Format function in your query, example:
Format([datetime column],'dd-mm-yyyy # hh:nn:ss AM/PM')
and in your SQL:
SELECT Format([CHECKINOUT.CHECKTIME],'dd-mm-yyyy # hh:nn:ss AM/PM') AS DT_CHECKTIME ...

Related

SQL Issue querying database between two dates

I have the following records in the DB below is the created date for each record.
2013-11-09 12:55:43.000
2013-10-29 19:01:53.000
2013-10-29 04:59:42.000
My SQL query looks like this
Select d.Name as DealerName, Sum(c.CommissionAmount) as CommissionAmount
from Dealer d
Left join Commission c on c.Dealerid = d.DealerId
where c.CreatedDate between isnull(#FromDate, c.CreatedDate) and isnull(#ToDate, c.CreatedDate)
Group by d.Name
Order by CommissionAmount desc
When I enter the following dates in to my search functionality
From date = 29/10/2013
To date = 09/11/2013
It only returns one record, when it should return three, yet if I leave From date as it is and pass in null for To date I get two records back
Can someone tell me what I'm doing wrong here?
Thanks
Try this:
When You are using Dates in where clause Always use Same casting on both sides
Select d.Name as DealerName, Sum(c.CommissionAmount) as CommissionAmount
from Dealer d
Left join Commission c on c.Dealerid = d.DealerId
where CAST(c.CreatedDate as DATE) between
CAST(isnull(#FromDate, c.CreatedDate) as DATE) and
CAST(isnull(#ToDate, c.CreatedDate) as DATE)
Group by d.Name
Order by CommissionAmount desc
You can do this by using tow ways one is
1. CAST
CAST(c.CreatedDate as DATE)
2. CONVERT
CONVERT(varchar(10), c.CreatedDate)
Here is the both the ways that you can achieve.
1. where
CONVERT(varchar(10), c.CreatedDate)
between
isnull(#FromDate, c.CreatedDate)
and
isnull(#ToDate, c.CreatedDate)
2. where
CAST(c.CreatedDate as DATE)
between
isnull(#FromDate, c.CreatedDate)
and
isnull(#ToDate, c.CreatedDate)
Difference between cast & convert is You can Apply any style format you need in the CONVERT function .There are many date time format availabe for CONVErT function Refer this link You will get all the styple format in the SQL.
Syntax for the COnvert is
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
where expression [ , style ] is optional field.
CAST :
Cast is available after the version of SQL 2005. cast also Converts an expression of one data type to another in SQL Server.
Syntax
CAST ( expression AS data_type [ ( length ) ] )
[ ( length ) ] is optional field in Cast
Try This
SELECT d.Name as DealerName, Sum(c.CommissionAmount) as CommissionAmount
FROM Dealer d
LEFT JOIN Commission c on c.Dealerid = d.DealerId
WHERE CONVERT(VARCHAR(10),CAST(c.CreatedDate AS DATE),103)
BETWEEN isnull(#FromDate, c.CreatedDate) and isnull(#ToDate, c.CreatedDate)
Group by d.Name
Order by CommissionAmount desc
The problem is u have defined the variables without time So the #todate will be like '2013-11-09 00:00:00.000'.
But in the table date has time. Between operator will not consider this date '2013-11-09 12:55:43.000' since it is higher than the todate u have mentioned thats y you are getting two rows.
so try this.
CREATE TABLE #temp
(dates DATETIME)
INSERT INTO #temp
VALUES ('2013-11-09 12:55:43.000'),
('2013-10-29 19:01:53.000'),
('2013-10-29 04:59:42.000')
DECLARE #from VARCHAR(50)='29/10/2013',
#to VARCHAR(50) ='09/11/2013'
SELECT *
FROM #temp
WHERE (#from is not null and #to is not null and Cast(dates AS DATE) BETWEEN CONVERT(DATE, #from, 103) AND CONVERT(DATE, #to, 103) ) or 1=1
SQL FIDDLE DEMO

how to join 2 query into one in EXCEL ORACLE CONNECTION

I have 2 query.
I am trying to join them so I just write export from one instead of manually joining them in excel.
(SELECT
b.OUT_NO,
a.ACCNO,
a.BILL_ACCNO,
a.NAME,
a.HOUSE_NO,
a.STREET,
a.HOUSE_NO2,
a.ZIP,
a.ID,
b.TIME_STAMP,
b.REST_DATE,
c.RESTORED_TIME,
b.OUT_CMNT
FROM brook.account a,
brook.problem b,
brook.history c
WHERE c.OUT_NO = b.OUT_NO
AND a.ID = c.ID
AND ( (a.NAME Is Not Null)
AND (a.DISC Is Null)
AND (b.TIME_STAMP>?)
AND (c.RESTORED_TIME<?))
)
and
(SELECT
b.OUT_NO,
a.ACCNO,
a.BILL_ACCNO,
a.NAME,
a.HOUSE_NO,
a.STREET,
a.HOUSE_NO2,
a.ZIP,
a.ID,
b.TIME_STAMP,
b.REST_DATE,
c.RESTORED_TIME,
b.OUT_CMNT
FROM brook.account a,
brook.problem b,
brook.history c
WHERE c.OUTAGE_NO = b.OUTAGE_NO
AND a.ID = c.ID
AND ( (a.NAME Is Not Null)
AND (a.DISC Is Null)
AND (b.TIME_STAMP > ? And b.TIME_STAMP < ?)
AND (c.RESTORED_TIME > ? And c.RESTORED_TIME < ?)
)
)
How can I join these 2? into 1, I tried UNION ALL but I get ora-01847 day of month must be between 1 and last day of month ERROR.
? are the parameter, it is linked to cells on spreadsheet.
format of excel data parameter. 11/04/2013 00:00:00
Thanks
Error is about a date format, not about union.
If you pass cell values as string parameters Oracle tries to convert it to dates to comapre with columns of date or timestamp values in table columns. To do this conversion Oracle uses it's internal default date representation format wich is not mm/dd/yyyy hh24:mi:ss in your case.
There are 2 possibilities to fix a situation:
Pass parameters with date type to query and convert values to dates before passing it to Oracle. Check examples on MSDN and description of CreateParameter and Parameters.Append methods.
Convert values to dates in query with to_date Oracle function.
Change conditions in query from
AND (b.TIME_STAMP>?)
AND (c.RESTORED_TIME<?))
and
AND (b.TIME_STAMP > ? And b.TIME_STAMP < ?)
AND (c.RESTORED_TIME > ? And c.RESTORED_TIME < ?)
to
AND (b.TIME_STAMP > to_date(?,'mm/dd/yyyy hh24:mi:ss') )
AND (c.RESTORED_TIME < to_date(?,'mm/dd/yyyy hh24:mi:ss') ))
and
AND (
b.TIME_STAMP > to_date(?,'mm/dd/yyyy hh24:mi:ss')
And
b.TIME_STAMP < to_date(?,'mm/dd/yyyy hh24:mi:ss')
)
AND (
c.RESTORED_TIME > to_date(?,'mm/dd/yyyy hh24:mi:ss')
And
c.RESTORED_TIME < to_date(?,'mm/dd/yyyy hh24:mi:ss')
)

SQL convert string into datetime in where clause issue

I'm trying to retrieve data from a wordpress database.
I can't find the syntax error in my query, can somebody help me with this?
It's to list the first 10 posts from specific post_type and with its Events_Date field converted into datetime.
Here is my query:
SELECT wp_posts.*, wp_postmeta.*
FROM ritmica_wp.wp_posts, ritmica_wp.wp_postmeta
WHERE convert(datetime, wp_postmeta.meta_value, 120) > GetDate()
IN
(SELECT wp_posts.*, wp_postmeta.* FROM ritmica_wp.wp_posts, ritmica_wp.wp_postmeta
WHERE wp_posts.post_type = 'event'
AND wp_postmeta.meta_key = 'Events_Date' )
ORDER BY wp_postmeta.meta_value ASC LIMIT 10
I don't see the point of the sub-query. Can't you just do:
SELECT TOP 10
wp_posts.*, wp_postmeta.*
FROM
ritmica_wp.wp_posts, ritmica_wp.wp_postmeta
WHERE
convert(datetime, wp_postmeta.meta_value, 120) > GetDate()
AND wp_posts.post_type = 'event'
AND wp_postmeta.meta_key = 'Events_Date'
ORDER BY
wp_postmeta.meta_value ASC
(for SQL Server)

MS Access Query problem?

I am using this query:
SELECT D.Generic, D.Ww, D.Dd, D.Plan, c.TotalScan, D.Plan - c.TotalScan AS Balance
FROM TableA D
LEFT JOIN (
SELECT COUNT(a.Specific) AS TotalScan,
b.Generic, a.Dd,a.Ww
FROM TableB a
INNER JOIN TableC b
ON a.Specific = b.Specific
GROUP
BY b.Generic,a.Dd,a.Ww
WHERE DATEDIFF(DAY, a.TransactionDate, GETDATE()) = 0
) c
ON c.Generic = D.Generic
AND D.Ww = c.Ww
AND c.Dd = D.Dd
WHERE DATEDIFF(DAY, c.TransactionDate, GETDATE()) = 0;
to filter all records that is a insert in my sqlserver database.
Now i am having a hard time how can i do it ms access.
1. DATEDIFF(Day, TransactionDate, GetDate()) = 0 -- Not Work on MS Access(Which Filter all Records inserted in current Date)
2. Cant display TotalScan from subquery
Example Output Date:
TransactionDate
3/21/2011 7:26:24 AM
3/21/2011 7:26:24 AM
3/22/2011 7:26:24 AM --
3/22/2011 7:26:28 AM --
3/22/2011 7:26:30 AM --
3/22/2011 7:26:32 AM --
3/22/2011 7:26:35 AM --
if my date today is 3/22/2011 5 records will be displayed.
Thanks in Regards
GetDate() is SQL Server specific, Access has Now() instead.
The DateDiff() function also exists in Access, but the parameter for the interval is different:
DateDiff("d", TransactionDate, Now())
Equivalent of:
DATEDIFF(DAY, c.TransactionDate, GETDATE()) = 0
DATEDIFF("d", c.TransactionDate, Now()) = 0
Regards

oracle sql query error while comparing date columns

I have a following oracle sql query in which START_DATE is a number column and a_date is DATE type and the input value is also of type DATE. kindly let me know how to compare the date columns with the input date.
select a.id ,a.v ,b.id,b.v
from DATA a ,FDC b where a.START_DATE = to_date('11-DEC-10','YYYYMMDD')
and a.START_DATE = b.a_date and b.code = 'JFK'
select a.id ,a.v ,b.id,b.v
from DATA a ,FDC b where a.START_DATE LIKE TO_DATE('11-DEC-10','DD-MON-YY')
and a.START_DATE = b.a_date and b.code = 'JFK'
If you stored your START_DATE as Number like 'YYYYMMDD':
a.START_DATE=TO_NUMBER(TO_DATE('20101211','YYYYMMDD'))
It depends entirely on what format your number start_date column is stored in.
However, it would probably be easier if you used the predicate on the true date column, and joined using a format mask just once.
For example:
SELECT a.id,
a.v,
b.id,
b.v
FROM data a,
fdc b
WHERE b.a_date = to_date('11-DEC-2010','DD-MON-RRRR')
AND a.start_date = TO_NUMBER(TO_CHAR(b.a_date, 'DDMMRRRR'))
AND b.code = 'JFK'
Please note that the date format matches the format of the date you are comparing - b.a_date = to_date('11-DEC-2010','DD-MON-RRRR'). This query assumes that the a.start_date column is stored in the format DDMMRRRR. You would need to amend this for whichever format your date is stored in e.g. a.start_date = TO_NUMBER(TO_CHAR(b.a_date, 'J')) for a Julian date.
P.s. Why use a number to store a date?
select * from tbltlcrconfighistory where TO_DATE(STARTDATE,'dd-mon-yyyy') = TO_DATE('14-dec-2010','dd-mon-yyyy');
select * from tbltlcrconfighistory where STARTDATE = TO_DATE('14-dec-2010','dd-mon-yyyy');