I am looking for the date difference between dates in the 'Dates' column. The query is also converting the Dates column to yyyy-mm-dd format and Filedate column to only display date. the query is being executed in a staging table. I am getting this error and it seems minor but I dont know where and how to correct it: "Parse error at line: 2, column: 19: Incorrect syntax near '('."
SELECT
CONVERT([DATE], date) AS Dates,
CAST(filedate AS DATE) AS Filedates,
filesize,
DATEDIFF(DAY, a1.(CONVERT([DATE], date)), a2.(CONVERT([DATE], date))) AS difference
FROM
stage.petsmart a1
INNER JOIN
stage.petsmart a2 ON a2.id = a1.id + 1
WHERE
filesize < 6000
Related
I have 6 digit value in one column that I need to convert to date-time. I tried two different formula given below.
(DATEADD(day, CONVERT(int, COLUMNNAME)-((1000*(CONVERT(int, COLUMNNAME)/100)))-1, DATEADD(year, CONVERT(int, COLUMNNAME/1000), '1 Jan 1900'))) as Order_date
But this is giving following error message:-
Adding a value to a 'datetime' column caused an overflow. [SQLSTATE=22007, SQLERRORCODE=517]
convert(datetime, (convert (int, COLUMNNAME)), 6) as Order_date
And this is giving incorrect value for date. There is one particular value 118150 that should result into 2018-05-30 :00:00:00, but my statement is returning 2223-06-27 00:00:00
Can anybody please help what is causing error with first statement and how can I modify it to run on entire table.
If I understand the date format correctly, this will work:
select dateadd(day, x % 1000 - 1, datefromparts(1900 + x / 1000, 1, 1))
from (values (118150)) v(x)
I want to get the total sum from a the table from the previous day. I get an error
My code:
select
date_paid, sum(paid_amount) as amount
from
till1
where
date_paid = dateadd(day, datediff(day, 0, getdate()), 0)
group by
date_paid
I want to get a result something like this:
I get the following error:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value
Presumably, this is SQL Server. You can fix the syntax error by using try_convert():
select date_paid, sum(paid_amount) as amount
from till1
where try_convert(date, date_paid) = cast(getdate() as date)
group by date_paid;
However, the real solution is to fix the data. Dates should not be stored as strings.
What is wrong with this sql query. I am getting syntax error near convert
"SELECT * FROM collections c where c.submittedBy='679' AND CONVERT(varchar, c.eventTime, 103) ='2017-06-21'";
The eventTime is "eventTime": "2017-06-21T12:20:03.9366135+05:30"
The error is
{"Message: {\"errors\":[{\"severity\":\"Error\",\"location\":{\"start\":64,\"end\":71},\"code\":\"SC1001\",\"message\":\"Syntax error, incorrect syntax near 'CONVERT'.\"}]}\r\nActivityId: a0c51c53-fca0-4c81-8f10-65348749e3d2"}
The best way to write this query (across databases) is:
SELECT c.*
FROM collections c
WHERE c.submittedBy='679' AND
c.eventTime >= '2017-06-21' AND
c.eventTime < '2017-06-22';
This allows the database to use an appropriate index for filtering. Note that if submittedBy is a number, then don't use single quotes.
convert to date before to convert to varchar
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(varchar(10), CONVERT(date, c.eventTime, 103)) = '2017-06-21'
if eventTime is already a datetime field should work also this way..
SELECT *
FROM collections c
where c.submittedBy='679'
AND CONVERT(date, c.eventTime) = '2017-06-21'
Greetings StackWarriors....
I am in need of some help.
I have this SQL Select statement:
SELECT GEOID, cast(LEFT(PP.PurchaseDate,4) + RIGHT(Left(PP.PurchaseDate,6),2) as integer) AS Month
FROM PropertyParametersNew PP
join PropertyTracts PT on PP.ParcelID = PT.PARCELID
WHERE
PP.PurchaseDate >0
and convert(datetime, PP.PurchaseDate, 112)>= DATEADD(MONTH,-1,getdate())
The intent in this query is trying to get GEOID, and the year/month associated in the PurchaseDate Column.
and I'm getting this error:
Msg 242, Level 16, State 3, Line 1 The conversion of a varchar data
type to a datetime data type resulted in an out-of-range value.
I realized that inside that column, I have yyyymmdd formatted dates that have 00's in the mm and dd. Examples include 20130000 and 20120300.
I am trying to rephrase the sql query to handle those entries.
My owners say that 0000 should reflect January 1, and 00 should reflect the 1st of the month.
How can I restructure this SQL statement to reflect this value, so I can get Year/Month combo without the conversion failure?
Thanks.
You can use REPLACE to fix values in PurchaseDate, then simply use fixed field in its place:
(edit made after correct remark by #t-clausen)
SELECT GEOID,
cast(LEFT(x.PurchaseDate,4) + RIGHT(Left(x.PurchaseDate,6),2) as integer) AS Month
FROM PropertyParametersNew PP
CROSS APPLY (SELECT CASE WHEN RIGHT(PP.PurchaseDate, 4) = '0000'
THEN LEFT(PP.PurchaseDate, 4) + '0101'
WHEN RIGHT(PP.PurchaseDate, 2) = '00'
THEN LEFT(PP.PurchaseDate, 6) + '01'
ELSE PurchaseDate
END) x(PurchaseDate)
JOIN PropertyTracts PT on PP.ParcelID = PT.PARCELID
WHERE
PP.PurchaseDate >0
and convert(datetime, x.PurchaseDate, 112)>= DATEADD(MONTH,-1,getdate())
If, for example, PP.PurchaseDate is equal to 20130000, then the above query sets x.PurchaseDate equal to 20130101 and uses this value inside CONVERT as well as in SELECT clause.
For all this to work PP.PurchaseDate must be of fixed length (8 characters).
Best senario would be fixing the data to date second best would be changing your dates to be valid dates as char(8).
But your question is how to write your select.
You should not convert the purchase date to datetime, you should go the other way and convert the getdate() to char(8). This will give you a better performance. Then there is the issue of the varchar being 20130000. You can compensate by subtracting 1 from the date and ask for a greater value instead of greater equal.
The answer is quite simple and improves performance because you don't have a conversion on your column:
WHERE
PP.PurchaseDate >0
and PP.PurchaseDate >
CONVERT(char(8),DATEADD(MONTH,-1,getdate())-1, 112)
Please help me to find out error in my SQL query. I have created this query to compare dates
select * from Joinplans jp
where cast(convert(varchar,GETDATE(),103) AS datetime) BETWEEN
CASE(convert(varchar,jp.planstartDate,103) AS datetime) AND
CASE(convert(varchar,DATEADD(DAY,jp.planDays,jp.planstartDate),103) AS DATETIME)
It's giving me the error:
incorrect near 'AS'
I am using SQL Server 2005.
You wrote case instead of cast in two instances.
If planStartDate is actually a date, then there is no need to cast it to a character column:
Select ...
From Joinplans jp
where GetDate() Between planStartDate And DateAdd(day, jp.planDays, jp.planStartDate)
Now, if planStartDate is storing both date and time data, then you might want to use something like:
Select ...
From Joinplans jp
Where planStartDate <= GetDate()
And GetDate() < DateAdd(day, jp.planDays + 1, jp.planStartDate)
This ensures that all times on the last date calculated via the DateAdd function are included