May 1, 2023 at 12:00 am
Comments posted to this topic are about the item Convert Dates for XML
May 1, 2023 at 1:34 am
I know what you were looking for but there's actually no correct answer because of the VARCHAR(20) thing. The correct format of the correct answer is 23 characters long and that's what the VARCHAR needs to be if you want to include the milliseconds or only 19 if you do not. Using 20 will truncate the format and leave the decimal point as the last character.
As a bit of a sidebar, people tend to go a bit sideways when I tell them I use T-SQL for generate the HTML for my automatic emailed and nicely formatted "Morning Jobs Report" but they never seem to even raise a finger when it comes to formatting stuff for XML. 🙁
--Jeff Moden
Change is inevitable... Change for the better is not.
May 1, 2023 at 2:35 pm
This entirely depends on what the system consuming the XML is expecting.
May 1, 2023 at 4:24 pm
Corrected to varchar(30), and the target system always depends, but there is a format designed to convert cleanly to XML. Not all formats do this.
May 1, 2023 at 5:26 pm
Corrected to varchar(30), and the target system always depends, but there is a format designed to convert cleanly to XML. Not all formats do this.
I find it interesting that people often use a VARCHAR sized larger than it ever needs to be to avoid truncation issues, instead of defining a CHAR at the exact length needed.
Why is VARCHAR(30) a better choice than CHAR(23)? The date format returned from CONVERT will always be 23 characters - it will never be less than 23 nor will it be more than 23 for the format specified.
Even when you want to truncate the milliseconds - it will always be 19 characters and will never vary from that length. So why use VARCHAR(19) vs CHAR(19)?
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 1, 2023 at 5:41 pm
Steve Jones - SSC Editor wrote:Corrected to varchar(30), and the target system always depends, but there is a format designed to convert cleanly to XML. Not all formats do this.
I find it interesting that people often use a VARCHAR sized larger than it ever needs to be to avoid truncation issues, instead of defining a CHAR at the exact length needed.
Why is VARCHAR(30) a better choice than CHAR(23)? The date format returned from CONVERT will always be 23 characters - it will never be less than 23 nor will it be more than 23 for the format specified.
Even when you want to truncate the milliseconds - it will always be 19 characters and will never vary from that length. So why use VARCHAR(19) vs CHAR(19)?
Unless you're actually storing the result in a database field is there any performance difference between varchar or char? If you are just for example returning the result set or using it to generate an XML result?
May 2, 2023 at 6:32 pm
Unless you're actually storing the result in a database field is there any performance difference between varchar or char? If you are just for example returning the result set or using it to generate an XML result?
Why would you code it differently because you are not storing the data? Is it somehow easier to use VARCHAR(23) instead of CHAR(23) because you are outputting the data to XML? What is the benefit of using varchar vs char when you know the data has a fixed length and will always have that fixed length?
Jeffrey Williams
“We are all faced with a series of great opportunities brilliantly disguised as impossible situations.”
― Charles R. Swindoll
How to post questions to get better answers faster
Managing Transaction Logs
May 2, 2023 at 8:46 pm
ZZartin wrote:Unless you're actually storing the result in a database field is there any performance difference between varchar or char? If you are just for example returning the result set or using it to generate an XML result?
Why would you code it differently because you are not storing the data? Is it somehow easier to use VARCHAR(23) instead of CHAR(23) because you are outputting the data to XML? What is the benefit of using varchar vs char when you know the data has a fixed length and will always have that fixed length?
Right that was precisely my point why would I code it as char(23) when normally I would just do varchar(30), like what the answer was actually changed to. Now sure if there's an explicit reason to use char(23) that's fine but that doesn't seem to be the case here. Which is why I asked if there was there was any value in using char(23) if you weren't storing it in the DB and it made sense to do that(like why are you just storing it as a date time).
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply