简体   繁体   中英

Using DatePart in line of a larger sql clause

This statement will correctly merge 2 columns ('DATE' and 'TIME') :

update AllBW1 set sUserTime = 
    CAST(
    (
    STR( YEAR( [DATE] ) ) + '/' +
    STR( MONTH( [DATE] ) ) + '/' +
    STR( DAY( [DATE] ) ) + ' ' +
    (select DATENAME(hour, [TIME]))+ ':' +
    (select DATENAME(minute, [TIME])) + ':' +
    (select DATENAME(SECOND, [TIME]))
        ) as DATETIME)

    where sUserTime is null

I'd like to refine the above so as to replace the default timezone with my own (GMT-6).

I've tried a few variations:

    CAST((select DATEADD(hour, -6, DATENAME(hour, [TIME]))) as smalldatetime) + ':' +
and
    (select CAST(DATEADD(hour, -6, DATENAME(hour, [TIME]))) as datetime) + ':' +

and have achieved no joy.

thx

The logfile as parsed into the SQL table by LogParser 2.2 has separate fields for Date and Time but, since both are formatted as datatime fields they end up looking like:

2012-01-04 00:00:00.000 for date (all time fields are zeroed)
2012-01-01 06:04:41.000 for time (all date field = first day of current year)

That's the reason the query is parsing each element the way it is. Thanks to Dems comment I simplified everything down. I've no doubt this can be optimized by for the volumes I'm dealing with this is adaquate:

update myTable set sUserTime = 
    (
    DATENAME(YEAR, [DATE] )  + '/' +
    DATENAME(MONTH, [DATE]  ) + '/' +
    DATENAME(DAY, [DATE] )  + ' ' +
    DATENAME(hour, (dateadd(hh, -6, [time])))+ ':' +
    DATENAME(minute, [TIME]) + ':' +
    DATENAME(SECOND, [TIME])
        ) 

    where sUserTime is null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM