Date conversion in a XML column in SQL Server - sql-server

How do I convert the below mentioned XML code date format.
<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>

If you're accessing your XML content with the built-in XQuery functionality, you can just use the .value() method and define the output datatype to be a DATETIME2(3) type - no special treatment necessary:
DECLARE #InputTbl TABLE (ID INT NOT NULL, XmlContent XML)
INSERT INTO #InputTbl (ID, XmlContent)
VALUES (1, '<Root>
<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>
</Root>');
SELECT
StartDate = XC.value('(StartDate)[1]', 'datetime2(3)'),
EndDate = XC.value('(EndDate)[1]', 'datetime2(3)')
FROM
#InputTbl
CROSS APPLY
XmlContent.nodes('/Root') AS XT(XC)
This returns this output:

There are several correct answers, but I've got the feeling, that these answers don't hit your actual issue:
In my table there is a xml column, and that xml column contains somuch data including date, And i want to update those dates to date format like '2/28/2017' now the date format is like '2012-04-26T00:00:00'
If I got you correctly you want to change the stored dates within your XML to another format, correct?
Simple answer: Don't!
ISO8601 is the standard format for date/time values within XML. The format you would like more 2/28/2017 is culture related and could lead to errors, or even worse!, to wrong values, if day and month both are below 13: 04/05/2017 can be taken as 4th of May or as 5th of April. You should never rely on culture settings!
XML is not meant to be human readable. Or in better words: It is meant to be human readable for technical people only... It is a standardizes string representation of structured, complex documents. The format of values should not bother you! Use an appropriate editor for the presentation.

This might be work
declare #date ='2015-12-24T00:00:00 2015-12-29T15:39:20'
SELECT CONVERT(date, Left(#date,10)) as NewDate

This may help you
DECLARE #X XML ='<StartDate>2015-12-24T00:00:00</StartDate>
<EndDate>2015-12-29T15:39:20</EndDate>'
SELECT #X.value('/StartDate[1]','DATETIME') AS START_DTE
,#X.value('/EndDate[1]','DATETIME') AS END_DTE
+-------------------------+-------------------------+
| START_DTE | END_DTE |
+-------------------------+-------------------------+
| 2015-12-24 00:00:00.000 | 2015-12-29 15:39:20.000 |
+-------------------------+-------------------------+
Update: From comments
The Datatime format in XML follows the ISO8601 standards. And you are thinking to format it native SQL format, which is not correct that you are treating XML like normal text data. The data present in XML format is correct. And If you want you can convert it to native SQL as above mentioned.
There is a good info at Wikipedia ISO 8601(Combined date and time representations)
on how the XML hold date time data.
A single point in time can be represented by concatenating a complete
date expression, the letter T as a delimiter, and a valid time
expression. For example, "2007-04-05T14:30".

Related

SQL Server date separator dot not working while inserting data

When I insert a date like this '01.03.2020 21:35:12' it changes into '2020-01-03 21:35:12.000'.
I want to insert the date with DOT as the Date separator.
NOTE: I'm not using a stored procedure, just insert query.
This is an inferior choice in format, because nobody reading that code can be certain whether you meant January 3rd or March 1st. You can get there this way, but it is ugly, unintuitive, and equally non-self-documenting:
DECLARE #d varchar(30) = '01.03.2020 21:35:12';
SELECT CONVERT(datetime, #d, 104);
Much better to use a standard, unambiguous date format for literals. These are the only two formats not subject to misinterpretation by language, dateformat, or regional settings, and therefore don't need to be accompanied by cryptic style numbers:
DECLARE #d1 varchar(30) = '20200301 21:35:12',
#d2 varchar(30) = '2020-03-01T21:35:12';
SELECT CONVERT(datetime, #d1), CONVERT(datetime, #d2);
Background:
Recommended SQL Server Date Formats
Bad Habits to Kick : Mis-handling date / range queries
Dating Responsibly
I don't think you can change the display in SSMS from the YYYY-MM-DD TIME format. If you want to change the way you get the date back when selected, you can use the CONVERT or FORMAT functions.
CONVERT function: https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
FORMAT function: https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql
You may need to select your datetime twice with the CONVERT. Once for the date and once for the time in order to get the combination of formats you want.
-John

Check data type format of every value of a column in SQL Server

I have a table_1 that has column dob which has datatype nvarchar(max). Now I want to check every date whether it is in datetime format or not.
The dates which are in datetime format should be copied to table_2 that has column with a datatype of datetime.
Any help how can I do this?
If you are using SQL Server 2012 or later, then TRY_CONVERT can help here:
SELECT *
FROM yourTable
WHERE TRY_CONVERT(datetime, dob) IS NULL;
This would return every record from your table where the dob text field is in some format which SQL Server cannot natively convert to a datetime.
This will insert all the strings can be converted into Table_2
INSERT INTO Table_2 (DateTimeColumn)
SELECT dob
FROM Table_1
WHERE TRY_CAST(dob AS DATETIME) IS NOT NULL;
It is a very bad habit to store datetime values in a string. The reason why: Date-Time formats differ around the world and depend on your system's culture settings. Even worse, some format depend on a given language. Try this out:
SET LANGUAGE ENGLISH; --try with GERMAN to see the effect on "Decemeber"
SET DATEFORMAT ymd; --try with "ydm" or "dmy"
DECLARE #tbl TABLE(dob NVARCHAR(MAX));
INSERT INTO #tbl VALUES('blah') --just a wrong value
,('20201231') --ISO, "unseparated YMD-format (see CONVERT with 112)
,('2020-12-31') --ISO8601
,('2020-31-12') --European with leading year
,('12-31-2020') --USA (see CONVERT with 110)
,('31-12-2020') --European (see CONVERT with 113)
,('31 December 2020') --language dependant (see CONVERT with 113), try with German "Dezember"
,('2020-02-30'); --Invalid, there's no 30th of February
SELECT t.dob --Your value
,[cast] = TRY_CAST(t.dob AS DATETIME) --CAST relies on the system's settings (might work on your machine but can break on a customer's machine
,[convert] = TRY_CONVERT(DATETIME, t.dob, 112) --CONVERT allows to use the style paramter, better than CAST, but more strict
,[parse] = TRY_PARSE(t.dob AS DATETIME USING 'en-US') --Parsing allows to mention the culture. You do not need to specify the language as a general setting
,[xmlCast] = CAST(t.dob AS XML).value('. cast as xs:date?','datetime') --Works below v2012, but can deal with ISO8601 only.
FROM #tbl t;
Play with the settings
You can use GERMAN instead of ENGLISH. The entry with December will not work any more. You can change the general date format to any combination of ymd and find, that some formats stop to work while others start to work.
TRY_CAST, TRY_CONVERT and TRY_PARSE will need a version of v2012 or higher.
If you are running an older system you should upgrade ( :-) ). The only chance with an older system is a XML hack, but this is very tightly bound to ISO8601.
Finally: For your next question, please try to add more information. State some of your input values, mention your RDBMS with version and try to set up a mockup with DDL and INSERT (look what I've done above).
Hint: Very dangerous...
Try to insert a value like 2020-05-06 and you will find, that some styles read this as the 5th of June, while others return the 6th of May. Returning wrong values is worse than returning NULL or throwing an error...
StayAtHome
StayHealthy

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

I'm using SQL Server 2014. I have a date stored as varchar(MAX) in the format of:
2019-02-18
However, I want it in the British format dd/mm/yyyy (103).
This is my SQL:
SELECT CONVERT(DATE, DateField, 103) AS "JobStartDate"
FROM tblTest
However, I keep getting this error:
Conversion failed when converting date and/or time from character string.
What am I missing?
Update: The date is initially stored as varchar max as it is coming from a 3rd party system. I have no control over this and I completly understand this is the wrong format, but this is what I have been given.
I have a date stored as varchar(MAX)
There's your problem right there.
Not only you are using the wrong data type to store dates, you are also using max which is a known performance killer.
The solution to the problem is to alter the table and store dates in a Date data type - but first, you must look up all the objects that depends on that column and make sure they will not break or change them as well.
Assuming this can't be done, or as a temporary workaround, you must first convert the data you have to Date, and then convert it back to a string representation of that date using the 103 style to get dd/mm/yyyy.
Since yyyy-mm-dd string format is not culture dependent with the date data type, you can simply do this:
SELECT CONVERT(char(10), TRY_CAST(DateField As Date), 103) As [JobStartDate]
FROM tblTest
Note I've used try_cast and not cast since the database can't stop you from storing values that can't be converted to dates in that column.
You want to format the DateField column and not convert it to date.
So first convert it to DATE and then apply the format:
SELECT FORMAT(CONVERT(DATE, DateField, 21), 'dd/MM/yyyy') AS JobStartDate
See the demo.

Convert varchar to datefield in SQL Server

I am aware that this is a very common and have tried using the solutions suggested on similar questions. However, I cant seem to make it work.
I have a column which contains dates, but it is of varchar datatype.
The data looks like this:
startdate
-----------
15/01/2007
29/06/1998
07/12/2001
and so on..
I tried the following command
try_convert(datetime, startdate) as 'startdate'
But it returns a lot of NULLs even when there is a date populated in the original column.
Can someone please help?
The try_convert method accepts three arguments: Data type, Expression, and Style.
You need to use the Style argument:
try_convert(datetime, startdate, 103) as 'startdate' -- 103 is dd/MM/yyyy
(a list of supported styles can be found here)
Also, You should store date values as Date, not as varchar.
For more information, read Aaron Bertrand's Bad habits to kick : choosing the wrong data type

SSIS Convert m/dd/yyyy to yyyymmdd with inconsistencies

I'm loading many files into a SQL SERVER database. I have one flat file that has a Date Column coming in as string[DT_STR].
I have TWO "date fields" in my database. One is varchar, one is datetime.
Converting the datetime column is no issue, I just use Data Conversion/Derived Column if necessary. However, this varchar column is giving me trouble. Our database values for this column should be in yyyymmdd format. However, on this single file the format of the dates change.
Normally I'd do a SUBSTRING(...) expression here, but the difficulty is that the format of these dates change. some examples of values could be
08/16/2017
8/16/2017
08/6/2017
08/06/2017
10/6/2017
10/06/2017
This makes the challenge harder. I tried LEN([DATE]) == NUM_HERE ? do_THING : OTHER_CALC, but this approach failed because the length of 10/6/2017 is the same as 8/06/2017 which will give me the wrong result. Does anyone have a good workaround for this?
Perhaps a simple convert to date and then into the final format. If 2012+, use try_convert() to trap any bogus dates.
Example
Declare #YourTable Table ([SomeCol] varchar(50))
Insert Into #YourTable Values
('08/16/2017')
,('8/16/2017')
,('08/6/2017')
,('08/06/2017')
,('10/6/2017')
,('10/06/2017')
Select *
,Formatted = convert(varchar(8),convert(Date,SomeCol),112)
from #YourTable
Returns
SomeCol Formatted
08/16/2017 20170816
8/16/2017 20170816
08/6/2017 20170806
08/06/2017 20170806
10/6/2017 20171006
10/06/2017 20171006
Convert the varchar data to datetime and convert that to a formatted string
SELECT CONVERT(varchar,(CONVERT(datetime, '8/6/2017')),112)

Resources