Cast int to SmallDateTime - sql

I am trying to compare L.LoanDueDate (which is a smalldatetime) to the return value of ufnARCUGetLatestProcessDate() (which is an int) to see how many days the loan due date is past the last process date.
I receive an
Arithmetic overflow error converting expression to data type smalldatetime
error and can't seem to figure it out. Any help would be useful. Thank you in advance.
CAST(L.LoanDueDate - CAST(CAST((SELECT ProcessDate FROM ufnARCUGetLatestProcessDate()) AS VARCHAR(10)) AS smalldatetime) AS INT)

You can't compare an int to a smalldatetime. You need to compare two dates and for that you'd have to use datediff.

I was able to figure out the issue. DigCamare's comment on DATEDIFF reminded me that is what I needed to do even though you can cast a int to a smalldatetime so you can compare it.
DATEDIFF(d,L.LoanDueDate,CONVERT(smalldatetime,convert(varchar(10),(SELECT ProcessDate FROM ufnARCUGetLatestProcessDate()))))

You can cast the date column to int. It will give you the aproximate (rounded) date, without the time part, as an int.
Edit:
This will give you the date as a number for you to compare:
SELECT FLOOR(CAST(L.LoanDueDate as float)) -
(SELECT ProcessDate FROM ufnARCUGetLatestProcessDate()) AS myValue
FROM ...
The key is to cast the date to number and not the number to date.

Just wanted to add the following. This is what I am using to do a simple int to smalldatetime conversion. Hope it helps other people.
(SELECT CAST(CAST(ProcessDate AS VARCHAR(10)) AS SMALLDATETIME) FROM ufnARCUGetLatestProcessDate())

Related

Change data type '2019-03-28 23:59:03.000000000' VARCHAR to DATETIME in SQL

I have a table which stores date in '2019-03-28 23:59:03.000000000' this format as VARCHAR in SQL.
I want to convert it from varchar to datetime and remove the last 000000000 values from date.
From '2019-03-28 23:59:03.000000000' to this '2019-03-28 23:59:03'.
Please help me out.
Thanks
If you don't want to show millisecs, try this one. Datetime2 let's you specify how many milliseconds you want displayed, in this case I used 0.
SELECT CONVERT (DATETIME2(0), '2019-03-28 23:59:03.000000000', 121)
Simply
SELECT CAST(REPLACE('2019-03-28 23:59:03.000000000', '.000000000', '') AS DATETIME)
when you select from your table you will
SELECT CAST(REPLACE(<YourColumn>, '.000000000', '') AS DATETIME)
FROM <YourTable>;
The REPLACE() function will remove '.000000000' from your string, and remains just '2019-03-28 23:59:03' which you will cast it as DATETIME

How to convert varchar to datetime in SQL Server

I would like to convert varchar data to datetime format. So, can anyone let me know the way. this is the sample data available in column.
I tried following things
SELECT
CASE WHEN [EntryTime] = ''
THEN NULL
ELSE CONVERT(datetime2, [EntryTime])
END [EntryTime]
FROM
table
I get this result:
But, I don't need additional milliseconds.
So, can anyone let me know how should I convert it to a datetime format?
For all other tries, I am getting error as
Conversion failed when converting date and/or time from character string
So, can anyone please help me? Thanks.
specify the sub-second width as datetime2(7)
select
try_cast(EntryTime as datetime2(7)) by_try_cast
, try_convert(datetime2(7), EntryTime ) by_try_covert
I also suggest you use try_cast() or try_convert() if they are available, if not just remove "try_", The advantage of these is that if a string is an invalid date they return NULL and don't crash the query.
If you really don't want datetime2:
select cast(try_cast(EntryTime as datetime2(7)) as datetime)
see it: http://rextester.com/YWBB31287
LEFT and Concat (SQL2012+)/ or use standard concatenation
then Cast or Convert (for formatting)
declare #mytable as table (
entrytime varchar(100)
)
insert into #mytable
values ('2017-08-31 08:23:23'),
('2017-11-10 08:28:37.75000000'),
('2017-10-30 08:23:37.32000000')
select cast(left(concat(entrytime,'.000'),23) as datetime) from #mytable
Result
2017-08-31 08:23:23.000
2017-11-10 08:28:37.750
2017-10-30 08:23:37.320

Explicit conversion from data type date to bigint is not allowed

This used to work with a column type of DATEIME but now it won't with DATE.
CONVERT(BIGINT,ev.StartDate) * -1
Is there anyway to get a BIGINT value from a DATE column?
You can cast the startdate as datetime for conversion.
CONVERT(BIGINT,CAST(ev.StartDate as DATETIME)) * -1
Yet another option. This will even flip the sign for you
Example
Declare #YourTable table (StartDate date)
Insert Into #YourTable values ('2017-05-30')
Select DateDiff(DAY,StartDate,-1)
From #YourTable
Returns
-42884
First, dates in SQL Server are counted by days from the year 1900. A big int starts to be useful at about 2.1 billion. That corresponds to a year in the range of 5.8 million. Do you really have dates that large?
Of course, casting to an int is not permitted. You can cast datetime values . . . but are there other ways?
One simple way is:
select 1 + datediff(day, 0, datecol)
The "+ 1" is needed so the value matches the actual conversion. (You can use "-1" instead of "0" instead.)
Or, perhaps you want Unix time in seconds or milliseconds. For that:
select datediff_big(ms, '1970-01-01', datecol)
You might require to convert to varchar and then bigint
select Convert(bigint,convert(varchar(10),ev.StartDate,112))*(-1)

T-SQL 2008 Convert Date and time string to datetime

I have two columns in my table, one to capture time and one to capture date. Unfortunately, both are varchar(). I need to take the two fields, concatenate them together, and then convert them to datetime.
I am trying to accomplish that with this:
select CONVERT(datetime,(select txt_returned_date+' '+CONVERT(varchar(20),CONVERT(TIME,txt_time_returned))),126)
from table_name
I am getting this error message:
Conversion failed when converting date and/or time from character string.
The date is being captured as "20130308" as a string. Time is being captures as "4:27 PM" as a string
What I am doing here is converting the string of the time to TIME, then back to varchar. Then I am concatenating them together. This works by itself, but once I introduce the CONVERT(datetime) to the whole query, it is giving me the error.
Any help to try to accomplish this is helpful. Thanks!
You can concatenate the DATE and TIME values together once they have been converted to a DATETIME. Here's a sample to play with that shows concatenating a DATE column and a TIME column that have been stored as VARCHAR:
-- Set up some variables to test with
DECLARE #myTime TIME = GETDATE()
, #myDate DATE = GETDATE()
, #myTimeTxt VARCHAR(16)
, #myDateTxt VARCHAR(10);
-- Initialize your variables
SELECT #myTimeTxt = #myTime
, #myDateTxt = #myDate;
-- Display your separated values
SELECT #myDateTxt, #myTimeTxt;
-- Display your concatenated value
SELECT CAST(#myDateTxt AS DATETIME) + CAST(CAST(#myTimeTxt AS TIME) AS DATETIME);
You can use this option
DECLARE #date date = '20010101',
#time time = '01:01:01'
SELECT CAST(#date AS datetime) + #time
Result:2001-01-01 01:01:01.000
Demo on SQLFiddle
Are you using SQL 2012? If so you may be able to use the datetimedromparts function to achieve this. If not for this specific example, it's always good to know for the future :)
http://technet.microsoft.com/en-gb/library/hh213233.aspx

tsql getdate conversion

I know that when i do the following, it converts getdate to int
select cast (getdate() as int)
Getdate output on my server is "2010-06-11 14:42:20.100" and the int to which the above command is converting to is 40339. What is this integer? Did this int consider minutes and
seconds? i am confused. Please help.
Regards
Manjot
This number is the number of days since the reference date, which is 01/01/1900. You should get the same result from:
SELECT DATEDIFF(day, 0, GETDATE())
Internally, SQL stores datetimes as two 4-byte integers. The first integer (which you are getting in your statement) represents the number of days since the reference date. The second integer represents the number of 1/300 second intervals since midnight. You can find the second integer by converting first to binary, then to int:
SELECT CONVERT(int, CONVERT(binary(4), GETDATE()))