8

SQL date-time conversion with nested converts experiencing random conversion err...

 2 years ago
source link: https://www.codesd.com/item/sql-date-time-conversion-with-nested-converts-experiencing-random-conversion-errors.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

SQL date-time conversion with nested converts experiencing random conversion errors

advertisements

Looking for assistance with a strange issue if anyone has ideas:

I have a SQL that statement works most of the time in a T-SQL script but crashes occasionally. I have identified the data that a crash occurs on and cannot identify any difference between data rows that work.

The goal of this code is to add the time to an already existing datetime value that has 00:00:00 as the time from the second time column (as outlined below). My goal is to combine both columns into YYYY-MM-DD HH:MM:SS format, but I had to convert them to char first to trim off the orignal 00:00:00.

Columns

LogDate - contains date only in DateTime format (YYYY-MM-DD HH:MM:SS)

LogTime - contains the time of the action and is in varchar format (HH:MM)

SQL Conversion

SELECT CONVERT(DATETIME, CONVERT(CHAR(8), LogDate, 112) + ' ' + CONVERT(CHAR(8), LogTime, 108))
                FROM  TestTable
                WHERE EventSerial = '100001'

However, if I change the EventSerial in the above statement to a different row, such as '100002', the statement works.

The data for each row is below:

EventSerial 100001's values: LogDate: 2015-04-02 00:00:00.000 LogTime: 10:04

EventSerial 100002's values: LogDate: 2015-04-02 00:00:00.000 LogTime: 10:48

Running with data set 1 fails, running with data set 2 produces output. Also, running the code without the final datetime conversion works, or if I run the code with the string manually it works (as outlined below:)

SELECT CONVERT(CHAR(8), LogDate, 112) + ' ' + CONVERT(CHAR(8), LogTime, 108)
                FROM  TestTable
                WHERE EventSerial = '100001'

SELECT  CONVERT(DATETIME, '20150402 10:48')
SELECT  CONVERT(DATETIME, '20150402 10:04')

Any suggestions, I'm sure its something silly that I'm missing (and I probably took the long way around the issue anyway. The desired output would be 2015-04-02 10:04:00


First, datetime has no format. (why?)

Second, you don't need to convert the datetime value to char to add hours and minutes, just use DateAdd:

SELECT DATEADD(Minute,
               CAST(RIGHT(LogTime, 2) as int),
               DATEADD(Hour,
                       CAST(LEFT(LogTime, 2) as int),
                       LogDate
                   )
              )
FROM  TestTable
WHERE EventSerial = '100001'

Also, note that convert does not hold a style for yyyymmdd hh:mm

Note: code was written directly here, there might be some mistakes.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK