Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting date format

Status
Not open for further replies.

wizard1923

Technical User
Dec 29, 2010
4
US
just need help converting date from yyyymmdd to yyyy-mm-dd so i can compare two fields with different date formats
 
Take a look at CAST and CONVERT:
Code:
SELECT [Date1] = CONVERT(DATE, '17760704', 101)
SELECT [Date1] = CAST('17760704' AS DATE);

--------------------------------------------------
“Crash programs fail because they are based on the theory that, with nine women pregnant, you can get a baby a month.” --Wernher von Braun
--------------------------------------------------
 
I think this may be what you are looking for.

Code:
Declare @MyTable Table (
	PK int,
	COLUMN1 Varchar(10),
	COLUMN2 Varchar(10),
	DATE_COLUMN1 VarChar(10),
	DATE_COLUMN2 VarChar(10) )

Insert INTO @MyTable (PK, COLUMN1, COLUMN2,DATE_COLUMN1, DATE_COLUMN2 )
Values (1,'John','Smith','01-01-2011','20110115')

Insert INTO @MyTable (PK, COLUMN1, COLUMN2,DATE_COLUMN1, DATE_COLUMN2 )
Values (2,'Terry','Kriten','02-01-2011','20110210')

Insert INTO @MyTable (PK, COLUMN1, COLUMN2,DATE_COLUMN1, DATE_COLUMN2 )
Values (3,'Merry','Xmas','05-17-2011','20110520')

SELECT
	PK,
	COLUMN1,
	COLUMN2,
	CAST(DATE_COLUMN1 AS DATETIME),
	CAST(DATE_COLUMN2 AS DATETIME),
	DATEDIFF(DAY,DATE_COLUMN1,DATE_COLUMN2) As 'Difference in Days'
FROM @MyTable
WHERE DATEDIFF(DAY,DATE_COLUMN1,DATE_COLUMN2) > 10

Thanks

John Fuhrman
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top