PERVASIVE - SQL Double - Date REquests - pervasive

in my Table column DeliveryDate datatype is "DOUBLE" not Date
How can I select the following example
SELECT .... FROM .... WHERE DeliveryDate>='01.01.2021'
Can I Convert CURRENTDATE() to a DOUBLE Value?
thx

Short answer is that no, you can't just CONVERT a DATE to DOUBLE. If you execute:
select convert(CURDATE(), sql_double)
You will get an error. This is because CURDATE returns a date in the form MM\DD\YYYY. Because of the slashes, the conversion to Double fails.
You can do something like this:
select convert(
concat(
DatePart(year,curdate())
, concat(right(concat('00', DatePart(month, curdate())),2)
, right(concat('00', DatePart(day, curdate())),2))), sql_double)
But that assumes the DOUBLE is just a YYYYMMDD concatenation of the DATE value.
You need to determine what format your DOUBLE field is and adjust the algorithm to fit your needs.

Related

Dividing a date value in T-SQL

I am working on migrating an Access program into SQL Server.
The following is my SQL code taken directly from access.
(([Promise Date])-([Date Recieved]))/100
As you can see, I am attempting to do a division on a datetime value.
This is the error message i receive:
Msg 257, Level 16, State 3, Line 1
Implicit conversion from data type datetime to int is not allowed. Use the
CONVERT function to run this query.
Both fields are type Datetime. Any ideas what I am missing?
I think if I am right you are looking for something like this
SELECT DATEDIFF(day, [Promise Date], [Date Recieved])/100;
If right you can get more details here
since dates in access are effectively stored as DOUBLE, I would recommend converting to the SQL float type to handle any part days
(cast([Promise Date] as float) - cast([Date Recieved] as float))/100
check out the following example, the different ideas here give quite different answers
declare #x as datetime = '19960420 15:05:48';
declare #y as datetime = '19960423 18:09:23';
select (CAST(#y as float) - CAST(#x as float)) / 100
select (CAST(#y as int) - CAST(#x as int)) / 100
select datediff(day,#x,#y) / 100
select cast(datediff(day,#x,#y) as float) / 100
Access should treat the dates as floats, with the fractional part representing time, and the integer part representing days
Declare #pd Date = '2019-01-15'
Declare #dr Date = '2019-01-12'
Select Cast(DATEDIFF(d,#dr,#pd) As Float) /100
Result
0.03
UPDATE: As per Cato's comment to allow for DateTime
Declare #pd DateTime = '2001-01-01 19:00:00'
Declare #dr DateTime = '2001-01-05 13:00:00'
Select (Cast(DATEDIFF(hh,#dr,#pd) As Float)/24) / 100
Result:
-0.0375

How to convert int year into valid date format in my stored procedure

My stored proc has the following select statement:
select Name,Holiday from tblNames where ID = #ID and DATENAME(YEAR, GETDATE()) = #Year
When executing the statement I have an error
"Error converting data type int to nvarchar."
How can I convert #Year parameter to a correct year?
The return type of the datename function is nvarchar, you want the datepart function that returns an integer value, so change to DATEPART(YEAR, GETDATE()) instead.
datename is what you would use to get the name of a month or weekday.
Or you could use the year(getdate()) function instead as Gordon L mentioned in a comment.
I'd recommend to switch back from fixing specific error message to the original question. You have a date in your table and need to filter it by range. So just provide to server the range bounds. This will also avoid any conversions of stored data. To avoid tail time issue you may append '23:59:59.997' to the upper bound of range or (my advice) provide next date and compare by < instead of <=
set #startdate = ...
set #enddate = dateadd(dd, 1, ...)
select *
from mytable
where t.date >= #startdate and t.date < #enddate
Complicated conversions can make it impossible to use appropriate index. If you can convert your arguments and provide prepared values to server - strive to do so.

How to change date-time format?

I have a RegDate column of nvarchar(max) type in my table in which dates are stored in mm/dd/yyyy (5/22/2015 11:09:39 PM) and dd-mm-yyyy (19-05-2015 22:55:05) format. I want to get all these entries in one format i.e. dd/mm/yyyy. I tried to convert it by using
Convert(varchar(10),cast(vr.RegDate as DATETIME),105) as RegistrationDate
but it gives following error:
The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.
Please help me regarding this problem.
You need to determine the format of the value you are converting before you can convert it. If it's simply between those two formats, you can simply search for - to determine it's format.
I would also suggest storing the value in a datetime column as opposed to a varchar, and if you can't do that for whatever reason, you should definitely store it in an ISO format: YYYY-MM-DD HH:MM:SS.
Here's a sample that uses a case statement to provide optional formatting of your two date formats, using the presence of the - character:
CREATE TABLE #temp ( RegDate VARCHAR(50) )
INSERT INTO #temp
( RegDate )
VALUES ( '5/22/2015 11:09:39 PM' ),
( '19-05-2015 22:55:05' )
SELECT CASE WHEN CHARINDEX('-', RegDate) != 0
THEN CONVERT(DATETIME, RegDate, 105)
ELSE CONVERT(DATETIME, RegDate, 101)
END AS FormattedToDate
FROM #temp
DROP TABLE #temp
Produces:
FormattedToDate
2015-05-22 23:09:39.000
2015-05-19 22:55:05.000

Error when converting float to datetime

I am trying to convert a float in the format yyyymmdd to datetime. According to this the correct style code for that format is 112.
Code:
select
convert(datetime,cast(myDate as numeric),112)
from MyTable
Error:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type datetime.
I get the same error without the cast as numeric part. I've been looking around for a couple hours, but I haven't been able to find anything that fixes this. If you know a better way to convert the float to a datetime I would be open to that idea.
Thank you for your help.
EDIT
Here is the working code:
SELECT
case when isdate(CAST(CAST(myDate AS INT) AS VARCHAR(8))) = 1
then CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
end
from MyTable
I wrapped it in the isdate because there were a few invalid dates in there. Thanks to Matt for the help.
EDIT2
Better version:
SELECT
TRY_CAST(CAST(CAST(myDate AS INT) AS VARCHAR(8)) AS DATETIME)
FROM MyTable
First you must convert the FLOAT to a VARCHAR. And since FLOAT has a number of decimal points, it must first be converted to an INT.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(#myDate AS INT) AS VARCHAR(8))
--20140721
Then you can convert the VARCHAR to DATE or DATETIME format.
DECLARE #myDate FLOAT
SET #myDate = 20140721
SELECT CAST(CAST(CAST(#myDate AS INT) AS VARCHAR(8)) AS DATE)
--2014-07-21

How to convert this varchar values to a datetime

I want to convert a varchar value that iI set up to a date time value. I want to load rows of a database into another database, but conversion is not going well.
This is my query:
select Krant
, cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datum
, Inhoud as artikel
, LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords
, 'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
The cast to Datum has to be a datetime value, but i am not getting it to work properly. When i tried to cast to datetime it gave me an out of range exception.
These are the results of this query:
alt text http://94.100.115.48/837450001-837500000/837478801-837478900/837478868_5_dE_7.jpeg
I want the "Datum" field to be a Datetime field with the same values but in Datetime format. Could anyone help me please :).
Thanks,
Younes
Use this:
select Krant, cast(cast(jaar as varchar(4))+'-'
+RIGHT('0'+cast(maand as varchar(2)),2)+'-'
+RIGHT('0'+cast(dag as varchar(2)),2) as datetime) as datum,
Inhoud as artikel,
LEN(Inhoud)-LEN(Replace(Inhoud,' ','')) as numwords,
'Goirle' as vestiging
from [Sitecore_Bibliotheekmb_Krantenknipsel].[dbo].[KRANGOI]
You are not casting the Varchars to a Datetime
CAST and CONVERT on MSDN.
Can you try this:
SELECT CAST(('2010' + '-' + '01' + '-' + '06') AS DATETIME)
If any of your dates are earlier than 1753, a simple datetime type will not be sufficient. For this, you'd need to use the datetime2 datatype introduced in SQL Server 2008.
Also check that all values of maand and dag are valid days and months.

Resources