trying to get age from date of birth - sql-server-2005

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?

Related

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())

Dynamic date in T-SQL

I am working on a question where I want to have a dynamic filter for date(I want to see the last 6 months based on today's date). Orderdate is INT which is why I am using nvarchar and then date. When I use the code below I get the following error message "Conversion failed when converting date and/or time from character string.". Does anyone know what I can do to fix this?
cast(cast(OrderDate as nvarchar) as date) > dateadd(month,-6, getdate())
The obvious solution is to use try_convert().
try_convert(date, try_convert(varchar(255), OrderDate)) > dateadd(month, -6, getdate())
That will remove the error, but probably does not fix the underlying problem.
You can find the offending values by doing:
select OrderDate
from t
where OrderDate is not null and
try_convert(date, try_convert(varchar(255), OrderDate)) is null

sql converting date to show

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.

Convert a varchar YYYYMMDD into datetime to compare with GETDATE()

What am I doing wrong here? I've looked through other posts but I'm getting different results than other people.
Trying to convert a varchar YYYYMMDD to datetime, and I keep getting:
Msg 8115, Level 16, State 2, Line 2
Arithmetic overflow error converting expression to data type datetime.
Attempts:
CONVERT(DATETIME, EXPDATE)
CONVERT(DATETIME, EXPDATE, 102)
(CONVERT(DATETIME, CAST(EXPDATE AS CHAR(8)), 112))
CONVERT(DATETIME, CAST(expdate AS VARCHAR(8)))
Am I bungling something obvious here?
You clearly have a values that are not valid dates.
In SQL Server 2012+, I would suggest TRY_CONVERT():
TRY_CONVERT(DATETIME, EXPDATE)
Then, look at the values that are NULL to see where data problems may be.
In earlier versions, you should be able to use isdate():
(CASE WHEN ISDATE(EXPDATE) = 1 THEN CAST(EXPDATE AS DATE) END)
I have a similarly formatted date field (DateKey) in a table I use. I convert it to DATETIME using this syntax:
SELECT CAST(CAST(DateKey AS VARCHAR(10)) AS DATETIME) FROM tblDate
Will this work for you?
Excellent. Gordon and chancrovsky, you helped me flush out the values that were formatted incorrectly. I used a version of what you posted and this worked
(CASE WHEN ISDATE(ExpDate) = 1 THEN CAST(ExpDate AS DATE) ELSE NULL
END) as ExpDate
Thank you so much!

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