sql converting date to show - sql

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.

Related

Error converting data type varchar to numeric when using datediff

I am trying to calculate a date using two table. Here the query
select DATEDIFF(DAY, convert(varchar,D.BUSS_DATE, 23), convert(varchar,A.BUSS_DATE,23))
FROM dbo.TrxA1 A WITH (NOLOCK)
LEFT JOIN SOURCE_TBL_MASTER_EXCHANGE_RATE_HISTORY D WITH (NOLOCK)
ON A.CCY = D.SPOT_RATE
But I get the error
Error converting data type varchar to numeric.
I have been converting those date columns to varchar but the error is still same.
Please help. Thank you.
You should be converting to dates not strings. No conversion may be needed at all if your data is using the correct types:
select datediff(day, D.BUSS_DATE, A.BUSS_DATE)
from dbo.TrxA1 A left join
SOURCE_TBL_MASTER_EXCHANGE_RATE_HISTORY D
on A.CCY = D.SPOT_RATE;
If your values are inappropriate stored as strings, you can convert using:
select datediff(day, convert(date, D.BUSS_DATE, 23), convert(date, A.BUSS_DATE, 23))
If you still have a problem, you can fix it using try_convert():
select datediff(day, try_convert(date, D.BUSS_DATE, 23), try_convert(date, A.BUSS_DATE, 23))
But that just masks the problem. You should find the offensive values:
select buss_date
from dbo.TrxA1 A
where try_convert(date, buss_date, 23) is null and buss_date is not null;
Then fix the values and change the data type to an appropriate date/time type.
Finally, do not use NOLOCK unless you know what it is doing. And you probably don't. NOLOCK is a license that says: "You can get me data from the table that is not 100% accurate."

show data with shipmentdate bigger than today

i am trying to pulling data for all shipments with shipmentdate greater than todays date. However, I cant figure out an easy conversion of the format of the nvarchar, i get a out of range value error when trying to run this:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where SHIPMENTDATE > GETDATE()
Msg 242, Level 16, State 3, Line 1 The conversion of a nvarchar data
type to a datetime data type resulted in an out-of-range value.
Try:
select *
from dbo.BAS_CT_RAW_ARCHIVE_TBL
where convert(date, SHIPMENTDATE, 103) > GETDATE()
To see how to use convert with non-standard dates see this.
Further consideration: use proper datatypes for columns, i.e. don't store dates as strings, but as date datatype - it will prevent you from having such problems.
according to your data format that got from comment below should work
select * from dbo.BAS_CT_RAW_ARCHIVE_TBL
where CONVERT(date, SHIPMENTDATE, 103) > convert(date, GETDATE())

Arithmetic overflow error converting numeric to data type numeric. AVG Select Statment

I am trying to average the values for a given month using the AVG function but it's throwing the Arithmetic overflow error converting numeric to data type numeric. After some Googling, it looks like the common cause for this is trying to store the output of the AVG function into a variable/column that has a datatype not suitable for the incoming value. I am receiving this error just trying to SELECT the values.
--Hours (DECIMAL(18,4)), Starts (DECIMAL(18,4))
SELECT ID, Avg(Hours), Avg(Starts)
FROM MonthlyReadings
WHERE ReadingDate < '2016-03-01'
AND ReadingDate > DATEADD(MM, -36, '2016-03-01')
AND MONTH(ReadingDate) = 1
GROUP BY ID
Any idea why I'm getting this error?
Resulting datatype is inherited from the field you're aggregating. You need a datatype large enough to hold the SUM() in order to calculate the AVG(), you should be fine with FLOAT or expand your DECIMAL:
SELECT ID, Avg(CAST(Hours AS FLOAT)), Avg(CAST(Starts AS FLOAT))
FROM MonthlyReadings
WHERE ReadingDate < '2016-03-01'
AND ReadingDate > DATEADD(MM, -36, '2016-03-01')
AND MONTH(ReadingDate) = 1
GROUP BY ID

trying to get age from date of birth

SELECT [ID_KUNDNR]
,CASE
WHEN DA_FODELSEAR IS NULL THEN 0
WHEN dateadd(year, datediff (year, DA_FODELSEAR, getdate()), DA_FODELSEAR) > getdate() THEN datediff (year, DA_FODELSEAR, getdate()) - 1
ELSE datediff (year, DA_FODELSEAR, getdate())
END As Age
,[Catalog]
FROM Table
I get
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting
expression to data type datetime.
Any solutions?
I'm just wondering why you are using case. Try the below and see whether it works.
SELECT [ID_KUNDNR] ,
datediff (year, 0, (getdate()-DA_FODELSEAR)) as Age,
Catalog
From Table
But again are you sure all values of DA_FODELSEAR is convertable to datetime?
If you have trouble try this.
SELECT [ID_KUNDNR] ,
Case When ISDATE(DA_FODELSEAR) = 0 Then 0 Else
datediff (year, 0, (getdate()-DA_FODELSEAR))
End as Age,
Catalog
From Table
Incase you want to know, which rows are causing this conversion problem select the table with 'where IsDATE(DA_FODELSEAR) = 0'
You have to make sure that DA_FODELSEAR is in correct datetime format. You can do it by using CAST(DA_FODELSEAR as DATETIME) and check.
I think that your problem lies here dateadd(year, datediff(year, DA_FODELSEAR, getdate()), DA_FODELSEAR). You have a value in your DA_FODELSEAR that is not in a DATETIME format. I would run a SELECT statement to find the offending value. You are checking for NULL but what about a blank space?

Please tell me what is error in my date comparison sql query

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