How to convert this varchar values to a datetime - sql-server

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.

Related

SQL Server : DATETIME to INT

The date I have is 10.01.2022, the value I want is 10012022 - how can I do that? Thanks
If you have a DateTime value and want to convert it to an int value with that format, you could cast it to varchar first using a format that gets you close, and then get rid of any unwanted symbols, and then convert to int
declare #d date = '20220110'
select convert(int, replace(convert(varchar, #d, 103), '/', ''))
I would suggest something like this...
Select Day(YourColumn) * 1000000 + Month(YourColumn) * 10000 + Year(YourColumn)
From YourTable
It's not clear from your question if you want month-day-year or day-month-year. Regardless, with this code it should be fairly easy to adjust it to meet your situation.
PS. This will be faster than converting to varchar and then to int.

PERVASIVE - SQL Double - Date REquests

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.

Adding two time fields together

I have two columns within my table they are set as nvarchar fields but contain time values.
one column is a time field one is the duration field
eg.
Time 1 = 15:05:22 (time field)
Time 2 = 00:02:00 (duration field)
I want to output Time 1 + Time 2 = 15:07:22
I have tried CAST(time1 as datetime)+CAST(time2 as datetime)
but I get 1900-01-01 15:07:22.000, and I don't want the date part. I can't use cast as time as I get an error I presume this is because the fields are set as nvarchar and not date/time?
Just cast the result to time to get rid of the date portion:
DECLARE #time_txt varchar(8);
DECLARE #duration_txt varchar(8);
SET #time_txt = '15:05:22';
SET #duration_txt = '00:02:00';
SELECT CAST(CAST(#time_txt as datetime) + CAST(#duration_txt as datetime) as time);
-- yields the time value 15:07:22.0000000
If you need this as a string (for example, in hh:mm:ss format), you can use CONVERT with the appropriate format option:
...
SELECT CONVERT(varchar(8), CAST(#time_txt as datetime) + CAST(#duration_txt as datetime), 108);
-- yields the string 15:07:22
PS: In general, you should use time columns for time values instead of varchar columns. Unfortunately, SQL Server does not have a really good data type for durations (time spans).
select dateadd(second,datediff(second,0,time1),time2) as Time3
from your_table

SQL Server Convert int to Datetime

I'm trying to convert some data that has datatype int. However when I tried to convert 200 into year, it throws an error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
Code:
declare #MonthStart datetime,
#Day int = 1,
#Month int = 5,
#Year int = 200 /*gives me an error if I input this but when I tried 2016 the code works fine*/
set #MonthStart = Cast(#Month as varchar(10)) +'-'+ Cast(#Day as varchar(10))+'-'+Cast(#Year as varchar(10))
select #MonthStart
I don't know what's the problem on this. Maybe the datetime won't accept this kind of format 200-05-01 as a date format.
As the error pointed out, you have an out-of-range value. The minimum value for DATETIME is:
1753-01-01 00:00:00.000
What you're tring to convert is below the minimum, thus the error.
As commented by marc_s:
And if you're on SQL Server 2008 or newer, you can get around this
problem by using DATETIME2(3) instead of DATETIME. This new datatype
doesn't have those "artificial" range limitations as the old one did.
You can use datetime2 instead of datetime. It supports dates of 1-1-0001 or later. https://msdn.microsoft.com/en-us/library/bb677335.aspx

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

Resources