Convert julian date to date in sql server - sql

I want to convert the julian date in a date format. 
Below, please find an example to create my error. All my dates have 5 digits I need to convert.
create table #test (dateR int)
insert into #test (dateR)
values (39596),(39596),(39595),(39595),(39593),(39592),(39592),(39589),(38104),(38104),(37957)
SELECT * from #test
select *
, dateadd (year, dateR/1000 - 1900, dateR %1000 - 1) as Rd
from #test
Getting the error:
Msg 517, Level 16, State 1, Line 2 Adding a value to a 'datetime'
column caused an overflow.
Also tried:
SELECT DATEADD(dd, CONVERT(int, RIGHT(dateR,3)) - 1, CONVERT(datetime,SUBSTRING(dateR,1,2)+'0101', 212))
This returns:
Msg 8116 level 16 state 1 line 1 nullArgument data type int is invalid for argument of substring function

You can just cast the INT value to a DATETIME.
For example:
SELECT CAST(39596 AS DATETIME); -- Returns 2008-05-30 00:00:00.000

Related

Convert string 'yyyy-mm' to date object

In my SQL table, I have a column with just 'yyyy-mm', how can I covert into a Date object and compare if it is older than 2 months from now?
When I try this, CONVERT fails because the [Month] format is just `yyyy-mm' without the day
where DATEDIFF(day,CONVERT(DATE, [Month]),GETDATE()) < 60
I am using SQL Server.
I have tried the answer from the comment
SELECT
COUNT(*) AS aCount
FROM [MyTable]
where [name] != 'abc'
GROUP BY CONVERT(DATE, [Month] + '-01')
But I get error saying
Msg 8152, Level 16, State 2, Line 1
String or binary data would be truncated.

I'm having date conversion problems with the 3rd column in the script below

Select c1.CDS_Date, c2.CDS_Date, Dateadd(day, -1 , c2.CDS_Date)
from cte as c1 left join
cte as c2
on c1.[Local Patient Identifier] = c2.[Local Patient Identifier] and
c1.JoinKey = c2.JoinKey - 1
Can I get come help please?
I got no issues with c1.CDS_Date and c2.CDS_Date.
I receive the following error message as soon as I include the 3rd column:
(24 rows affected)
Msg 242, Level 16, State 3, Line 50
The conversion of a varchar data type to a datetime data type resulted in an --out-of-range value.
That is because your CDS_Date is not of date/datetime type but is a string.
Here is an example:
declare #t table (CDS_Date varchar(100));
insert into #t values ('28/10/2017');
set language British;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
-------
--CDS_Date dt
--28/10/2017 2017-10-27 00:00:00.000
set language us_english;
select CDS_Date, dateadd(day, -1, CDS_Date) as dt
from #t;
--Msg 242, Level 16, State 3, Line 40
--The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
This example shows you that not every string can be converted to date EVEN IF it seems to you that it represents a valid data. This is because server expects a date that you pass as a string to be in the certain format that depends on session language.
To be independent of session language you shoud pass your date string using convert function and specifying the format of your date.
In my example I can fix the issue converting to date with style 103:
select CDS_Date, dateadd(day, -1, convert(date, CDS_Date, 103)) as dt
from #t;
----
--CDS_Date dt
--28/10/2017 2017-10-27
Now it gives me the correct result in both languages just because it knows that my input strings are of 103 format, i.e. dd/mm/yyyy

Convert a sysdate() into int datatype

I have a sysdate() value - 2016.10.18. I need a SQL to convert it to int in MS SQL SERVER.
INPUT - 2016.10.18
OUTPUT reqd. - 20161018
I tried this -
SELECT CONVERT(int, '2016.10.18')
but it throws the following error:
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar value '2016.10.18' to data type int.
Also tried this -
select CONVERT(int,CONVERT(decimal(19,2),'2016.10.18'))
Getting the following error:
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
Could someone help me out!?
Select Convert(int,Convert(varchar,cast('2016.10.18' as date),112))
Returns
20161018
Either of the following will work. If you have a use the replace() method, you will need to strip the time portion off via LEFT(...,10)
Select Convert(int,Convert(varchar,cast('2016.10.18 14:24:49' as date),112))
Select cast(Replace(Left('2016.10.18 14:24:49',10),'.','') as int)
Updated answer.
Select Cast(Convert(varchar(8), Cast(Left('2016.10.18 14:24:49',10) as Date), 112) as int)

Converting 8-length varchar to datetime

I have varchar column as dataset as:
20110712
20141229
20100222
20140408
20131117
20130912
20140702
20110405
That need to be converted datetime datatypes. I have tried: CONVERT(date, column_name) yet it returns the error:
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
How can I ensure that this will work?
If your date is a varchar, then you should be able to use CONVERT(DATE,[date_field], 112)
DECLARE #dates TABLE
(
[date] varchar(10)
)
INSERT INTO #dates([date])
VALUES('20110712'),('20141229'),('20100222'),('20140408'),('20131117'),('20130912'),('20140702'),('20110405')
SELECT CONVERT(date,[date],112)
FROM #dates

Int to Date in SQL SERVER 2000

How to convert an int column ,Birthdate( sample value 20090301) to a date column in the format(01/03/2009)
Sql Server 2000
I was trying to convert some thing like below
select * from tabl
where
cast(dob as datetime)>='01/03/2009'
which gives an error
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
SQL Server doesn't allow you to cast int -> datetime directly, but if you're sure you can cast it to nvarchar first and then cast it to date.
For your question here's an example
declare #test int = 20090301
select CONVERT(datetime, CAST(#test as nvarchar), 112)
112 is the format you mentioned, here's the list of all possible formats for Convert function.
try this !
select * from tabl
where
cast(convert(char(8),dob, 112) as date)>='2009-03-01'