Sql between query trying to get DATETIME column and not the condition - sql

I'm trying to filter between two DateTimes allowing null values too
I'm using OdbcDataAdapter
objCon = new OdbcConnection(connStr05);
objDA = new OdbcDataAdapter(query, objCon);
The connection is working fine because I use it for another select queries.
And I'm filling a datatable with that info
DataTable objDTfinal = new DataTable();
objDA.Fill(objDTfinal);
I've tested different queries...
This one works (don't crash when start filling) but it crashes showing this error:
Year, Month, and Day parameters describe an un-representable DateTime Exception
"SELECT * FROM DBName.PUB.gvehicu
where (( \"GVVE-fecmat\" BETWEEN '1753-01-01'
AND '2500-12-31' or ( \"GVVE-fecmat\" IS NULL ))
and (( \"GVVE-finiga\" BETWEEN '1753-01-01'
AND '2500-12-31') OR ( \"GVVE-finiga\" IS NULL )))"
The same query without null condition get half of the results but its working and filling it all fine.
My boss told me that when he works with dates, he uses Convert(DATETIME, value) so I tried this way too
But this query automatically throws an error that cannot find the column DATETIME
"SELECT * FROM DBname.PUB.gvehicu
where (( \"GVVE-fecmat\" BETWEEN CONVERT(DATETIME,'1753-01-01')
AND CONVERT(DATETIME,'2500-12-31')) or ( \"GVVE-fecmat\" IS NULL ))
and (( \"GVVE-finiga\" BETWEEN CONVERT(DATETIME,'1753-01-01')
AND CONVERT(DATETIME,'2500-12-31')) OR ( \"GVVE-finiga\" IS NULL ))"
System.Data.Odbc.OdbcException: 'ERROR [42S22] [DataDirect][ODBC Progress OpenEdge Wire Protocol driver][OPENEDGE]Column "DATETIME" cannot be found or is not specified for query. (13865)'
I'm not sure if it should work with convert and I'm doing something wrong, or there is another way to get this.
I'm trying to get automatically all table values with one operation to fill a DataTable object that I will throw to another table with SqlBulkCopy, that's why I'm using this way to work.
The source database is a Progress that I'm accessing with odbc connection and the ending database is SQL

I've finally found the solution.
With progress database you have to pass the datatimes in different format.
Passing datetimes without convert and in this format worked for me: 'YYYY-mm-dd', but if you have the year, month or day into a variable you have to pass like '#YEAR#' + '-' + '#MONTH#' + '-' + '01'
Because there is a lot of information and it still crashes because timeout, I've done different ranges of datetimes to solve it.
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" < '1900-01-01'
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" < '2000' + '-' + '01' + '-' + '01' AND "RHMC-fecmov" > '1900' + '-' + '01' + '-' + '01'
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" < '2010' + '-' + '01' + '-' + '01' AND "RHMC-fecmov" > '2000' + '-' + '01' + '-' + '01'
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" < '2015' + '-' + '01' + '-' + '01' AND "RHMC-fecmov" > '2010' + '-' + '01' + '-' + '01'
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" < '2020' + '-' + '01' + '-' + '01' AND "RHMC-fecmov" > '2015' + '-' + '01' + '-' + '01'
SELECT * FROM AUTO05AS.PUB.rhiscab where "RHMC-fecmov" > '2020-01-01'

From the exception it is clear that query builder is looking for "DATETIME" as column in table, which means first parameter must be value. change the query as below and try.
"SELECT * FROM DBname.PUB.gvehicu
where (( \"GVVE-fecmat\" BETWEEN CONVERT('1753-01-01', DATETIME)
AND CONVERT('2500-12-31', DATETIME)) or ( \"GVVE-fecmat\" IS NULL ))
and (( \"GVVE-finiga\" BETWEEN CONVERT('1753-01-01' , DATETIME)
AND CONVERT('2500-12-31', DATETIME)) OR ( \"GVVE-finiga\" IS NULL ))"

Related

Construct a date from parts in SQL Server 2008

I am using SQL Server 2008 and trying to form a query that will use a CASEstatement with a date calculation to return one of two dates depending on what day of the month the calculation results in.
The goal is if the calculation (in query below) CURRENT_TIMESTAMP + LEAD_TIME results in a day between the 1st and 15th, then assign the 15th of that month. Otherwise, return the last day of that month.
For example, if CURRENT_TIMESTAMP + LEAD_TIME = 07/12/19, return 07/15/19. If that calculation = 07/16/19, return 07/31/19.
This seems like it would be possible using DATEFROMPARTS but I believe since I am using SQL Server 2008 that function is not defined (that is the error I am returning). Any ideas on a work around?
SQL:
SELECT I.po_number,
I.po_item_number AS 'po_item',
S.orderentry_date,
I.po_req_ship_date,
I.ex_factory_date,
I.del_indicator,
H.po_type,
H.vendor_no,
CASE WHEN DATEPART(dd,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)) BETWEEN 1 AND 15
THEN DATEFROMPARTS(DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),15)
ELSE DATEFROMPARTS(DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)+1,0))
END AS 'LT_CALC',
H.po_created_by,
I.comment
FROM rbk_sap_user..vw_po_header H
JOIN rbk_sap_user..vw_po_item I ON H.po_number = I.po_number
JOIN rbk_sap_user..vw_mm_material MM ON I.material = MM.material
JOIN (SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder1
UNION ALL
SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder2
UNION ALL
SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder3
) S ON S.order_no = H.ahag_number
WHERE S.orderentry_date BETWEEN '01/31/2019' AND '02/13/2019'
AND I.del_indicator <> 'L'
AND H.po_type NOT IN ('02','06','10','UB')
AND MM.business_segment_code NOT IN ('420','421','422','424')
You can convert all the parts of the date to VARCHAR and string them together to yyyy-mm-dd format, and then CONVERT toDATETIME.
SELECT
...
CASE WHEN DATEPART(dd,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)) BETWEEN 1 AND 15
THEN CONVERT(DATETIME, CONVERT(varchar, DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)))+ '-' + CONVERT(VARCHAR, DATEPART (mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME))) + '-' + CONVERT(VARCHAR, 15))
ELSE DATEADD(DAY, -1, CONVERT(DATETIME, CONVERT(VARCHAR, DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)))+ '-' + CONVERT(VARCHAR, DATEPART(mm,CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)+ 1) + '-' + CONVERT(VARCHAR, 1)))
END AS 'LT_CALC'
...
Note that in the ELSE statement, the logic to get the last day of the month is to go the first day of the following month, and then use the DATEADD function to subtract one day. The SQL Server function EOMONTH , introduced in SQL Server 2012, eliminates the need for that logic.
Try this
SELECT CAST(LEFT(CONVERT(varchar, CURRENT_TIMESTAMP + LEAD_TIME, 103 ),2) as int)
then
CASE WHEN CAST(LEFT(CONVERT(varchar, CURRENT_TIMESTAMP + LEAD_TIME, 103 ),2) as int) < 15 THEN
....
ELSE
....
END

Two dates having same values failing when joined in SQL Server 2014 using CONVERT and DATEPART functions

I am facing a very weird problem in joining on two dates in SQL Server 2014. I have a condition like below in my where clause :-
SELECT
a.*, w.*
FROM
TABLE1 a, TABLE2 w
WHERE
CONVERT(varchar(8), ISNULL(a.Col1,999), 112) + ' ' + CONVERT(VARCHAR, DATEPART(hh, ISNULL(a.Col1,999))) + ':' + RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, ISNULL(a.Col1,999))), 2)
= CONVERT(varchar(8),ISNULL(w.Col2,999),112) + ' ' + CONVERT(VARCHAR, DATEPART(hh, ISNULL(w.Col2,999))) + ':' + RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, ISNULL(w.Col2,999))), 2)
Here Col1 in Table1 and Col2 in Table2 are of type VARCHAR(18).
When selected separately both using the CONVERT and DATEPART function, both return the same value but when used in a join condition it fails with below error:
ERROR encountered - 241Conversion failed when converting date and/or time from character string
Example of Col1 in Table1 data:- "20190227 17:20:40"
Any help is truly appreciated!!

Query to compare cast in select statement

I have a query where I am pulling in three fields, and converting them to a date. I'm hoping to then run a comparison within the same select statement, but I get an invalid column name on the field I'm attempting to compare. The order by is working correctly though. /scratching_head
SQL Server 2012
SELECT
UTCSID,
UTLCID,
/* put utonmm, utondd, utonyy together as a date called uton */
CAST(
CAST(UTONMM as varchar) + '/' +
CAST(UTONDD as varchar) + '/' +
CASE WHEN UTONCV = '1'
THEN RIGHT('20' + CONVERT(varchar(4), RIGHT('00' + CONVERT(varchar(4), UTONYY),2)),4)
ELSE RIGHT('19' + CONVERT(varchar(4), UTONYY),4)
END
AS DATETIME) AS UTON,
/* put utofmm, utofdd, utofyy together as a date called utoff */
CAST(
CASE WHEN UTOFMM > '0'
THEN
CAST(UTOFMM as varchar) + '/' +
CAST(UTOFDD as varchar) + '/' +
CASE WHEN UTOFCV = '1'
THEN RIGHT('20' + CONVERT(varchar(4), RIGHT('00' + CONVERT(varchar(4), UTOFYY),2)),4)
ELSE RIGHT('19' + CONVERT(varchar(4), UTOFYY),4)
END
END
AS DATETIME) AS UTOFF,
UTCBAL,
UTDBAL,
UTUNPS
FROM [HTEDTA].[THOR].[HTEDTA].UT210AP
WHERE UTLCID = '885570' AND UTOFF > GETDATE() ORDER BY UTON DESC
This statement returns:
Invalid column name: 'UTOFF'
The problem is that the alias utoff isn't known at the time when the where clause is parsed.
One way to get around it is to wrap the query in a common table expression and apply the where clause to that:
WITH CTE AS (
SELECT UTCSID, UTLCID,
/* put utonmm, utondd, utonyy together as a date called uton */
CAST(
CAST(UTONMM as varchar) + '/' +
CAST(UTONDD as varchar) + '/' +
CASE WHEN UTONCV = '1'
THEN RIGHT('20' + CONVERT(varchar(4), RIGHT('00' + CONVERT(varchar(4), UTONYY),2)),4)
ELSE RIGHT('19' + CONVERT(varchar(4), UTONYY),4)
END
AS DATETIME) AS UTON,
/* put utofmm, utofdd, utofyy together as a date called utoff */
CAST(
CASE WHEN UTOFMM > '0'
THEN
CAST(UTOFMM as varchar) + '/' +
CAST(UTOFDD as varchar) + '/' +
CASE WHEN UTOFCV = '1'
THEN RIGHT('20' + CONVERT(varchar(4), RIGHT('00' + CONVERT(varchar(4), UTOFYY),2)),4)
ELSE RIGHT('19' + CONVERT(varchar(4), UTOFYY),4)
END
END
AS DATETIME) AS UTOFF,
UTCBAL, UTDBAL, UTUNPS
FROM [HTEDTA].[THOR].[HTEDTA].UT210AP
)
SELECT *
FROM CTE
WHERE UTLCID = '885570' AND UTOFF > GETDATE()

How possible is it to perform date ranges with date values of string data type?

I am forced to work with a date value stored as strings.
For instance,
Fieldname: claimsDate
data Type: nvarchar
The values are stored in the format of mmddyy.
E.G; 022413.
I have managed to convert this to say 02/24/2013 using the following query.
select (left(claimsdate,2) + '/' + substring(claimsdate,3,2) + '/' + case when cast(right(claimsdate,2) as int) >= 70 then '19' else '20' end + right(claimsdate,2)) claimsdate
from tblTable
The problem now is that we cannot perform a date range check with that code.
I have tried using it like this:
select (left(claimsdate,2) + '/' + substring(claimsdate,3,2) + '/' + case when cast(right(claimsdate,2) as int) >= 70 then '19' else '20' end + right(claimsdate,2)) claimsdate
from tblTable
where (left(claimsdate,2) + '/' + substring(claimsdate,3,2) + '/' + case when cast(right(claimsdate,2) as int) >= 70 then '19' else '20' end + right(claimsdate,2))
between '03/01/2007' and '12/31/2007'
This didn't give me the range I was looking for. It just displayed dates arbitrarily.
Is it possible to manipuate the code to be used for date range checks?
Thanks alot in advance
Use datetime conversion in sql. This link show you full example http://www.sqlusa.com/bestpractices/datetimeconversion/
select (left(claimsdate,2) + '/' + substring(claimsdate,3,2) + '/' + case when cast(right(claimsdate,2) as int) >= 70 then '19' else '20' end + right(claimsdate,2)) claimsdate
from tblTable
where cast(((case when cast(right(claimsdate,2) as int) >= 70 then '19' else '20' end + right(claimsdate,2) + '-' + left(claimsdate,2) + '-' + substring(claimsdate,3,2))) as datetime)
between #fromDate and #endDate
With #fromDate and #endDate is datetime variable
I hope this will satisfy your need

Using DatePart in line of a larger sql clause

This statement will correctly merge 2 columns ('DATE' and 'TIME'):
update AllBW1 set sUserTime =
CAST(
(
STR( YEAR( [DATE] ) ) + '/' +
STR( MONTH( [DATE] ) ) + '/' +
STR( DAY( [DATE] ) ) + ' ' +
(select DATENAME(hour, [TIME]))+ ':' +
(select DATENAME(minute, [TIME])) + ':' +
(select DATENAME(SECOND, [TIME]))
) as DATETIME)
where sUserTime is null
I'd like to refine the above so as to replace the default timezone with my own (GMT-6).
I've tried a few variations:
CAST((select DATEADD(hour, -6, DATENAME(hour, [TIME]))) as smalldatetime) + ':' +
and
(select CAST(DATEADD(hour, -6, DATENAME(hour, [TIME]))) as datetime) + ':' +
and have achieved no joy.
thx
The logfile as parsed into the SQL table by LogParser 2.2 has separate fields for Date and Time but, since both are formatted as datatime fields they end up looking like:
2012-01-04 00:00:00.000 for date (all time fields are zeroed)
2012-01-01 06:04:41.000 for time (all date field = first day of current year)
That's the reason the query is parsing each element the way it is. Thanks to Dems comment I simplified everything down. I've no doubt this can be optimized by for the volumes I'm dealing with this is adaquate:
update myTable set sUserTime =
(
DATENAME(YEAR, [DATE] ) + '/' +
DATENAME(MONTH, [DATE] ) + '/' +
DATENAME(DAY, [DATE] ) + ' ' +
DATENAME(hour, (dateadd(hh, -6, [time])))+ ':' +
DATENAME(minute, [TIME]) + ':' +
DATENAME(SECOND, [TIME])
)
where sUserTime is null