Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Convert 'Nov 7' to datetime
Message
 
À
09/11/2018 15:48:22
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Versions des environnements
SQL Server:
SQL Server 2016
OS:
Windows 7
Divers
Thread ID:
01663259
Message ID:
01663263
Vues:
58
>Hey guys, I'm back
>
>I'm trying to get something that looks like 'Nov 7 04:30' to convert to the equivalent SQL datetime value.
>Everything I'm trying is bringing back 'Nov 7 2018 4:30 AM. I can get it as far as a string 2018-11-07 04:30, but it will not convert to a datetime
>
>declare @fdate varchar(20), @f2 int, @f3 int, @f4 int, @t varchar(5), @nd varchar(20), @newdate datetime
>set @fdate = 'Nov 07 04:30'
>set @t = right(@fdate, 5)
>print isdate(@fdate)
>set @f2 = datepart(MM,@fdate) --str(@fdate,1,3))
>set @f3 = convert(int, substring(@fdate, 5,2))
>set @f4 = iif((@f2 = 12) and (datepart(MM, getdate()) = 1), year(getdate()) - 1, year(getdate()))
>set @nd = str(@f4,4) + '-' + replace(str(@f2,2), space(1), '0') + '-' + replace(str(@f3,2), space(1), '0') + ' ' + @t
>--set @nd = replace(str(@f2,2), space(1), '0') + replace(str(@f3,2), space(1), '0') + str(@f4,4)-- + @t
>--set @newdate = convert(datetime, @nd)
>set @newdate = cast(@nd as datetime)
>print @f2
>print @f3
>print @f4
>print @nd
>print @newdate

Dorris

I believe you need to make the time portion of your string be something like 'Nov 4 4:30:00 AM', hours, min, sec.

Here is some T-SQL syntax that should get you going in the right direction.

Select convert(datetime,'Nov 4 4:30:00 AM' + str(year(getdate()))) as mdate

Also try and change the AM to PM and note the change.

Weird how the year works, you can just modify your string if you wish.

Bill Sutton
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform