I have a date stored within a text field with other text. Why didn't they just put this in a date field? I have no idea, but I do not have the power to change it now. Elsewhere in the code, I am doing this to get the records where this date is in a certain range. It works fine.
For Each i As InventoryItem In inventoryList
index = i.Notes.IndexOf("on {") + 4
scanned_dt = i.Notes.Substring(index, i.Notes.Length - index - 1)
If Date.Parse(scanned_dt) >= startDate And Date.Parse(scanned_dt) <= endDate Then
...
I am now trying to get a total of items for a certain date range.
This sql statement works to get the total for all dates. How can I update the Where clause to only count the items where i.Notes contains a date between startDate and endDate
Dim sql As String = "Select COUNT(inv_PartNum) from lester.inventory i join lester.vendor v on v.vendor_ID = i.vendor_ID Where v.vendor_Name = '" + vendorName + "' AND i.inv_Desc LIKE '%" + size + "%'"
*EDITED*
I came up with this sql select statement:
SELECT COUNT(i.inv_PartNum) FROM cdms.lester.inventory AS I
join CDMS.lester.vendor AS v on v.vendor_ID = i.vendor_ID Where v.vendor_Name = 'JVE-285'
AND CONVERT(DATETIME,
SUBSTRING(i.inv_Notes, CharIndex('on {', i.inv_Notes)+4, len(i.inv_Notes)-(CharIndex('on {', i.inv_Notes) + 4)
),101) BETWEEN '01-01-2011' AND '04-04-2011'
But I am getting this error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
In trying to figure it out I created the following sql select statement:
SELECT * FROM (
SELECT i.inv_Notes,
CONVERT(DATETIME,SUBSTRING(i.inv_Notes, CharIndex('on {',i.inv_Notes)+4,LEN(i.inv_Notes)-(CharIndex('on {', i.inv_Notes) + 4)),101) AS d
FROM cdms.lester.inventory AS i
join CDMS.lester.vendor AS v ON v.vendor_ID = i.vendor_ID WHERE v.vendor_Name = 'JVE-285') AS s
My inv_Notes column contains a string like "Assigned to Tool Trailer {JVE-285} on {4/8/2011}"
When I run the query as shown above, I get my inv_Notes column along with the date column. The dates all show in this format "2011-04-08 00:00:00.000" and no errors are thrown.
However as soon as I add a WHERE clause, I get the error: The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
I've tried formatting the date every which way, but always get the error...
WHERE s.d > CONVERT(DATETIME, '2011-1-1', 101)
WHERE s.d < GetDate()
WHERE s.d > '20110101'
WHERE s.d >= '2011-01-01'
WHERE s.d >= '01-01-2011'
WHERE s.d > '01/01/2011'
EDIT
I've also tried
WHERE s.d IS NOT NULL
and get the same error. This obviously isn't working the way I think it's working b/c at the point of the WHERE, the conversion should have already successfully happened.
SOLUTION
Got this working
SELECT * FROM (
SELECT i.inv_Notes,
SUBSTRING(i.inv_Notes, CharIndex('} on {',i.inv_Notes)+6,LEN(i.inv_Notes)-(CharIndex('} on {', i.inv_Notes) + 6))
AS d
FROM cdms.lester.inventory AS i
join CDMS.lester.vendor AS v ON v.vendor_ID = i.vendor_ID WHERE v.vendor_Name = 'JVE-285') AS s
WHERE PARSENAME(REPLACE(s.d, '/', '.'), 1)+
RIGHT('00'+PARSENAME(REPLACE(s.d, '/', '.'), 3),2)+
RIGHT('00'+PARSENAME(REPLACE(s.d, '/', '.'),2),2) BETWEEN '20110407' AND '20110409'
I tried to cast that to int and do an integer comparison but then I get an error. Figured out that the problem was that some of the inv_Notes fields contain data like "Item Added {3/11/2011}" None of those meet the conditions for the inner select, so in my mind if it's not selected in the inner select, it shouldn't be a problem for the condition of the outer select. However, it was trying to cast 2011{311 to int and throwing an error. I'm sure it was trying to cast that to date and that's why I had the previous problems.
Try adding something similar to this WHERE clause into your statement:
WHERE CAST(SUBSTRING(Notes, CHARINDEX(Notes, 'on {') + 4, Length?) AS Date) BETWEEN StartDate And EndDate
The idea been that you extract the relevant part of the string by combining the SubString and CharIndex methods, and then convert this expression to a date format so that it can be used with the Between Operator.
Best of Luck
Part 2 Update:
As you have been able to select the date but not use it in the where clause, I suggest using it as a select statement and then wrapping this in another statement e.g:
SELECT *
FROM
(SELECT CAST(SUBSTRING(Notes, CHARINDEX(Notes, 'on {') + 4, Length?) AS Date) AS dtmNotes)
WHERE dtmNotes BETWEEN Start And End
This is just to illustrate, but include the whole of your first select statement in the wrapping.
Try this...
assuming that the table with the embedded date is called InventoryList and the column is called inv_Notes
SELECT *
FROM InventoryList i
WHERE
CAST(
substring(
i.inv_Notes,
patindex('%on {%',i.inv_Notes) +4,
patindex('%[0-9][0-9][0-9][0-9]}%',i.inv_Notes)-patindex('%on {%',i.inv_Notes)
) as Datetime)
BETWEEN '12/1/2011' AND '12/23/2011
EDIT: More restrictive looking for a "9/9999}" pattern
SELECT *
FROM InventoryList i
WHERE
CAST(
substring(
i.inv_Notes,
patindex('%on {%',i.inv_Notes) +4,
patindex('%[0-9]/[0-9][0-9][0-9][0-9]}%',i.inv_Notes)-patindex('%on {%',i.inv_Notes)
) as Datetime)
BETWEEN '12/1/2011' AND '12/23/2011
Related
I need to print manipulate month and string which I fetch from a table and display in the format like '12/20', '11/20', 09/20'
For this I need to trim the last 2 digits from year and also a leading zero to months which are less than 10.
SELECT
CAST(MONTH(O.AddDate) AS VARCHAR(2)) + '/' + CAST(YEAR(O.AddDate) AS VARCHAR(4)) AS TimeStamp
FROM
[Order] O
WHERE
O.CountryCode = 9009
GROUP BY
CAST(MONTH(O.AddDate) AS VARCHAR(2)) + '/' + CAST(YEAR(O.AddDate) AS VARCHAR(4))
This provided output in the format of '10/2020', '8/2020' but I require it to be like '08/20', '10/20'
You can use format():
select format(O.AddDate, 'MM/yy') as timestamp
from [Order] o
group by format(O.AddDate, 'MM/yy')
Obviously that's not your entire query; otherwise, if you have no aggregation function in the select clause, you can use select distinct instead of group by.
IMHO formatting dates is a presentation layer concern. SQL Server should provide the data and presentation layer should show the data to user as required. So I would write the query like this:
SELECT DISTINCT
YEAR(O.AddDate) AS AddDateYear, MONTH(O.AddDate) AS AddDateMonth
FROM
[Order] O
WHERE
O.CountryCode = 9009
Or like this:
SELECT
YEAR(O.AddDate) AS AddDateYear, MONTH(O.AddDate) AS AddDateMonth
FROM
[Order] O
WHERE
O.CountryCode = 9009
GROUP BY
YEAR(O.AddDate), MONTH(O.AddDate)
However, if you insist in formatting the date like you requested, then here is the query:
SELECT
RIGHT('0' + CAST(T.AddDateMonth AS varchar(2)), 2) + '/'
+ RIGHT(CAST(T.AddDateYear) AS varchar(4), 2) AS Y2kVulnerableTimestamp
FROM
(
SELECT DISTINCT
YEAR(O.AddDate) AS AddDateYear, MONTH(O.AddDate) AS AddDateMonth
FROM
[Order] O
WHERE
O.CountryCode = 9009
) T
When I try changing the order of SQL where condition, I am facing error.
for example:
select *
from dbo.abc
where measure_date <> 'null' and stage = 'xyz'
and measure_place = 'xxx' and fact_code = '123' and std_sis like '%180%'
and cast(measure_date as date) >= '2020/09/01' and cast(measure_date as date) <= '2020/10/13'
In the above query if I keep std_sis after fact_code I am getting output result whereas if I keep std_sis at the end of the query like:
select *
from dbo.abc
where measure_date <> 'null' and stage = 'xyz'
and measure_place = 'xxx' and fact_code = '123'
and cast(measure_date as date) >= '2020/09/01' and cast(measure_date as date) <= '2020/10/13'
and std_sis like '%180%'
I am getting error like:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
The behavior you are seeing is due to Short-circuiting by optimizer. but, it is not guaranteed. We can never predict that LEFT expression will be evaluated first and RIGHT expression will evaluated second.
To avoid this problem, you can do few things like below:
Fix data issues in the measure_date. Only keep the date values
You can change expression like below.
TRY_CAST(measure_date, DATE) >= '2020/09/01' AND TRY_CAST(measure_date, DATE) <= '2020/10/13'
Get the records which are date values and they apply filter
select * INTO #abc
from dbo.abc
where measure_date <> 'null' and stage = 'xyz'
and measure_place = 'xxx' and fact_code = '123' and std_sis like '%180%'
and isdate(measure_date) = 1
SELECT * FROM #abc
WHERE cast(measure_date as date) >= '2020/09/01' and cast(measure_date as date) <= '2020/10/13'
Read more on short-circuiting
As #DaleK has pointed out in the comments "the issue" is that you are casting a date stored as a string and the cast is invalid.
As pointed out in the comments the best solution is to change the table column type to date and fix all the bad data.
If you cannot not do this, then another option is to use TRY_CAST (introduced in SQL 2012) function. This function returns NULL if a cast operation is invalid and does not throw an error.
I'm using this query to get the Sum of SaleAmount for each type (SOType) of Sale Invoices.
I am getting the result but the result is not grouped by SOType. Have tried to use Group by Outside the query after where condition but getting an error as
"Column 'SaleInvoices.InvoiceID' is invalid because it is not
contained in either aggregate or group by function".
DECLARE #fromDate Datetime = '2019/05/23'
DECLARE #toDate Datetime = '2019/10/25'
DECLARE #isKpi int = '1'
SELECT (
(Select Sum((Isnull(I.Quantity,0)*Isnull(I.SalePrice,0))+((Isnull(I.Quantity,0)*Isnull(I.SalePrice,0) - I.Discount) *(I.TAX/100)))
from ItemsSold as I
where I.InvoiceId= S.InvoiceID and I.InvoiceType='Sale Invoice'
) -
(Select isnull(Sum((Isnull(I.Quantity,0)*Isnull(I.SalePrice,0))+((Isnull(I.Quantity,0)*Isnull(I.SalePrice,0) - I.Discount)*(I.TAX/100))),0)
from ItemsSold as I
where I.InvoiceId= S.InvoiceID and I.InvoiceType='Sale Return'
)) as Total
,S.SOType as SOType
FROM SaleInvoices AS S
where S.OrderDate>=Convert(VARCHAR,#fromDate,111) and S.OrderDate<=Convert(varchar,#toDate,111)
You want conditional aggregation. The logic should look something like this:
select s.SOType,
sum(case when i.invoicetype = 'Sale Invoice'
then (I.Quantity * I.SalePrice) * (1 - i.discount) * i.tax / 100.0
when i.invoicetype = 'Sale Return'
then - (I.Quantity * I.SalePrice) * (1 - i.discount) * i.tax / 100.0
end) as Total
from SaleInvoices s join
ItemsSold i
on i.InvoiceId= s.InvoiceID
where s.OrderDate >= #fromDate and
s.OrderDate <= #toDate
group by s.SOType ;
I'm not sure I got the arithmetic correct.
Notes:
The group by clause defines the rows being returned by the query. If you want one row per SOType then you want to GROUP BY SOType.
Use date comparisons and functions for dates. It is absurd to convert a date to a string to compare to a date.
You probably don't need COALESCE() or ISNULL() to handle NULL values. These are generally ignored by aggregation functions.
How to select year-1?
This is my code:
select
a.*
from
(select
met_men, kli_kod, pre_kod, galutinis, savik_group, marza,
KLR_KOD, KLI_POZ1, KLI_POZ2, KLI_POZ3, KLG_KOD, PRE_RUS,
PRE_POZ1, PRE_POZ2, PRE_POZ3, PRE_POZ4, PRE_POZ5, PRE_POZ6,
did_dat, savi_suproc, marza_suproc, pre_ska dbo.SVF_View_10) AS a
left outer join
(select
pre_kod, kli_kod, met_men, did_dat
from
dbo.SVF_View_10_sum
where
dateadd(year, -1, 'did_dat')) as b on a.kli_kod = b.kli_kod
and a.pre_kod = b.pre_kod
and a.did_dat = b.did_dat
This error occurs on the line where DATEADD(year, -1, 'did_dat')) as b:
Msg 4145, Level 15, State 1, Line 6
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Please help me
the required data: order date, id, quantity, orderdate-1 year, quantity. It is necessary to compare the quantity sold for this year and for the last year
You give column name in single quote dateadd(year, -1, 'did_dat') which is no need here change into dateadd(year, -1, did_dat) and when you use where clause need to give comparison operation in where clause.
so changed into
where did_dat <= dateadd(year, -1, did_dat)
but it doesn't make any sense because query should be execute on previous year against current date like
where did_dat <= dateadd(year, -1, GETDATE())
I suspect what you want is:
select . . .
from dbo.SVF_View_10 v left outer join
dbo.SVF_View_10_sum vs
on v.kli_kod = vs.kli_kod and a.pre_kod = b.pre_kod and
v.did_dat = dateadd(year, -1, vs.did_dat);
Notes:
Subqueries are not necessary for this query. I think they just make the query harder to write and to read.
Use table aliases that are abbreviations for the table names.
You have a date calculation in the where clause rather than a boolean expression. That is causing your error.
The date expression has a string for the third argument. The would be your next error. Strings do not refer to column names.
I am speculating that you want the tables joined on that date expression.
Try This :-
SELECT a.*
FROM
(
SELECT met_men,kli_kod,pre_kod,galutinis,savik_group,marza,KLR_KOD,LI_POZ1,
KLI_POZ2,KLI_POZ3,KLG_KOD,PRE_RUS,PRE_POZ1,PRE_POZ2,PRE_POZ3,PRE_POZ4,
PRE_POZ5,PRE_POZ6,did_dat,savi_suproc,marza_suproc,pre_ska
FROM SVF_View_10_sum
) AS a
LEFT OUTER JOIN
(
SELECT pre_kod,kli_kod,met_men,did_dat
FROM dbo.SVF_View_10_sum
WHERE did_dat = DATEADD(YEAR, -1, did_dat)
) AS b
ON a.kli_kod = b.kli_kod AND a.pre_kod = b.pre_kod AND a.did_dat = b.did_dat;
Here is my query:
SELECT
*
FROM
(SELECT
A.Name, AP.PropertyName, APV.Value AS [PropertyValue],
CONVERT(DATETIME, APV.VALUE, 101) AS [DateValue]
FROM dbo.Account AS A
JOIN dbo.AccountProperty AS AP ON AP.AccountTypeId = A.AccountTypeId
JOIN dbo.AccountPropertyValue AS APV ON APV.AccountPropertyId = APV.AccountPropertyId
AND APV.AccountId = A.AccountId
WHERE
A.AccountTypeId = '19602AEF-27B2-46E6-A068-7E8C18B0DD75' --VENDOR
AND AP.PropertyName LIKE '%DATE%'
AND ISDATE(APV.Value) = 1
AND LEN(SUBSTRING( REVERSE(APV.Value), 0 , CHARINDEX( '/', REVERSE(APV.Value)))) = 4 --ENSURE 4 digit year
) AS APV
WHERE
APV.DateValue < GETDATE()
It results in the following error:
Conversion failed when converting date and/or time from character string.
If you comment out the WHERE APV.DateValue < GETDATE() clause then there is no error and I get the 300+ rows. When I enable the WHERE clause I get the error.
So you are going to tell me my data is jacked up right? Well that's what I thought, so I tried to figure out where the problem in the data was, so I started using TOP() to isolate the location. Problem was once I use the TOP() function the error went away, I only have 2000 rows of data to begin with. So I put a ridiculous TOP(99999999) on the inner SELECT and now the entire query works.
The inner SELECT returns the same number of rows with or without the TOP().
WHY???
FYI, this is SQL that works:
SELECT
*
FROM
(SELECT TOP(99999999)
A.Name, AP.PropertyName, APV.Value AS [PropertyValue],
CONVERT(DATETIME, APV.VALUE, 101) AS [DateValue]
FROM dbo.Account AS A
JOIN dbo.AccountProperty AS AP ON AP.AccountTypeId = A.AccountTypeId
JOIN dbo.AccountPropertyValue AS APV ON APV.AccountPropertyId = APV.AccountPropertyId
AND APV.AccountId = A.AccountId
WHERE
A.AccountTypeId = '19602AEF-27B2-46E6-A068-7E8C18B0DD75' --VENDOR
AND AP.PropertyName LIKE '%DATE%'
AND ISDATE(APV.Value) = 1
AND LEN(SUBSTRING(REVERSE(APV.Value), 0 , CHARINDEX( '/', REVERSE(APV.Value)))) = 4
) AS APV
WHERE
APV.DateValue < GETDATE()
The problem that you are facing is that SQL Server can evaluate the expressions at any time during the query processing -- even before the WHERE clause gets evaluated. This can be a big benefit for performance. But, the consequence is that errors can be generated by rows not in the final result set. (This is true of divide-by-zero as well as conversion errors.)
Fortunately, SQL Server has a work-around for the conversion problem. Use try_convert():
TRY_CONVERT( DATETIME, APV.VALUE, 101) AS [DateValue]
This returns NULL rather than an error if there is a problem.
The reason why some versions work and others don't is because of the order of execution. There really isn't a way to predict what does and does not work -- and it could change if the execution plan for the query changes for other reasons (such as table statistics). Hence, use try_convert().
My guess is that your date is such that APV.VALUE contains also data that cannot be converted into a date, and should be filtered out using the other criteria?
Since SQL Server can decide to limit the data first using the criteria you have given:
APV.DateValue < CONVERT( DATETIME, GETDATE(),101)
And if there is data that cannot be converted into the date, then you will get the error.
To make it more clear, this is what is being filtered:
CONVERT( DATETIME, APV.VALUE, 101) AS [DateValue]
And if there is any data that cannot be converted into a date using 101 format, the filter using getdate() will fail, even if the row would not be included in the final result set for example because AP.PropertyName does not contain DATE.
Since you're using SQL Server 2012, using try_convert instead of convert should fix your problem
And why it works with top? In that case SQL Server cannot use the criteria from the outer query, because then the result might change, because it might affect the number of rows returned by top
Because number of records in the table < 999..99. And regarding the error it seems like SQL engine evaluates the WHERE clause after converting to date so you can try this:
SELECT *
FROM (
SELECT A.Name
, AP.PropertyName
, APV.Value AS [PropertyValue]
,
CASE
WHEN SDATE(APV.Value) = 1
THEN CONVERT( DATETIME, APV.VALUE, 101)
ELSE NULL
END AS [DateValue]
FROM dbo.Account AS A
JOIN dbo.AccountProperty AS AP
ON AP.AccountTypeId = A.AccountTypeId
JOIN dbo.AccountPropertyValue AS APV
ON APV.AccountPropertyId = APV.AccountPropertyId
AND APV.AccountId = A.AccountId
WHERE A.AccountTypeId = '19602AEF-27B2-46E6-A068-7E8C18B0DD75' --VENDOR
AND AP.PropertyName LIKE '%DATE%'
AND LEN( SUBSTRING( REVERSE(APV.Value), 0 , CHARINDEX( '/', REVERSE(APV.Value)))) = 4 --ENSURE 4 digit year
) AS APV
WHERE APV.DateValue IS NOT NULL AND APV.DateValue < GETDATE()