Get only the Date portion of a date time column

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Get only the Date portion of a date time column



In the set portion of the below query how would I get it to grab just the date portion of the datetime column in MS SQL 2012?


update db.dbo.Prod

set
db.DBO.prod.code5 = l.datetime

FROM
db.DBO.ali l

INNER JOIN
db.dbo.prod p

ON

l.part = p.KEYCODE and p.code5 = '' and L.PREFIX = 'D' AND l.part like 'ACD%'




1 Answer
1



What is the type of code5? If it is a date, then your code is fine. If not, you can cast the value.


code5



You should also be consistent in your use of aliases:


UPDATE p
SET code5 = CAST(l.datetime as DATE)
FROM db.DBO.ali l INNER JOIN
db.dbo.prod p
ON l.part = p.KEYCODE and p.code5 = '' and L.PREFIX = 'D' AND l.part like 'ACD%'





It is varchar(50) and I can't change it unfortunately. The cast worked perfectly. Thanks!
– Kelly Peterson
Aug 10 at 20:13






If it's a varchar, then you're going to have an implicit cast that converts the date to a string. It's probably better to explicitly use CONVERT, and pass a string format so you can specify the format the date takes... otherwise you could get different strings based on the locale you're running in, which could cause some issues for some applications.
– pmbAustin
Aug 10 at 21:40






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard