Sql-server – How to drop the TIME portion from DATEADD

datedate formatdatetimesql serverupdate

I am trying to create some test data where each Employee has a random birthdate. I accomplished the randomizing part but it did not return result in the format that I want.. NOTE: the DateOfBirth column uses the VARCHAR data type.

/****** Generate Random Date For Each Row (Between 1900 - 2015) ******/
UPDATE CCCINC.dbo.Employees
SET DateOfBirth = DATEADD(DAY, ABS(CHECKSUM(NEWID()) % 41975), '1900-01-01');

Result:

Feb 28 1930 12:00AM

How do I remove the TIME portion from the result? Also, if I wanted to change the DATE format to YYYYMMDD how would I accomplish that?

Best Answer

Well, assuming that someone is holding a gun to your head, forcing you to not change the data type to something more sensible and logical:

UPDATE CCCINC.dbo.Employees
SET DateOfBirth = CONVERT(CHAR(8), DATEADD(DAY, ABS(CHECKSUM(NEWID()) 
  % 41975), '1900-01-01'), 112);

Strong, strongly, strongly suggest you change the data type of that column to use the right data type (DATE). Then you can just use your original query and not worry about the "format" at all. Format is what you worry about when you present the data, not when you store it.