Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a table with 4 fields called expirytable:Name Expiryday ExpiryMonth ExpiryyearThe fields contain the format:Name: WhateverExpiryday: 20Expirymonth: AUGExpriryyear: 2010I need to SELECT Name FROM expirytableWHERE whole expiry date ie: AUG 20 2010is greater than or equal to the date two days ago.Can someone help please?
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2010-09-03 : 05:26:49
[code]DECLARE @Sample TABLE ( Name VARCHAR(20) NOT NULL, ExpiryDay TINYINT NOT NULL, ExpiryMonth CHAR(3) NOT NULL, ExpiryYear SMALLINT NOT NULL )INSERT @SampleSELECT 'Whatever', 20, 'AUG', 2010 UNION ALLSELECT 'Peso', 1, 'SEP', 2010-- Solution hereSELECT Name, ExpiryDay, ExpiryMonth, ExpiryYearFROM @SampleWHERE CONVERT(DATETIME, CAST(ExpiryDay AS VARCHAR(4)) + ' ' + ExpiryMonth + ' ' + CAST(ExpiryYear AS VARCHAR(4)), 106) >= DATEADD(DAY, DATEDIFF(DAY, 2, GETDATE()), 0)[/code]N 56°04'39.26"E 12°55'05.63"
colgie
Starting Member
18 Posts
Posted - 2010-09-03 : 06:07:35
Thanks a million - looks like just what I need.I'll work on it and post more thanks when I get it to work.Thanks again
SwePeso
Patron Saint of Lost Yaks
30421 Posts
Posted - 2010-09-03 : 07:42:49
You can also make a calculated column like this
ALTER TABLE ExpiryTableADD ExpiryDate AS (CONVERT(DATETIME, CAST(ExpiryDay AS VARCHAR(4)) + ' ' + ExpiryMonth + ' ' + CAST(ExpiryYear AS VARCHAR(4)), 106))
Does the calculated column calculate as new entries are entered or do you have to do the recalculation every time you access?By the way - the code you already gave me is working - thanks again