SQL Server Performance

Need Help in SQL Query

Discussion in 'SQL Server 2005 General Developer Questions' started by abi.chapagai@gmail.com, Nov 12, 2009.

  1. Hi All:
    I am new SQL Programming and i need help in doing following task.
    I want to write a script that does following thing:
    --If the date is 11/12/2009 or 11/12/2020 or greater than today's date than display 'D'
    -- If the date is valid date other than 'D' than disply 'W',
    -- If the date is blank display '' ( blank'.
    Please help me in this query. Thanks all.
  2. atulmar New Member

    CASE suits your solution better, I think you have typo in question, 1st case is about BETWEEN the specified dates.CREATE
    TABLE DateTable(DateCol VARCHAR(50)
    )INSERT
    INTO DateTable VALUES (GETDATE())INSERT
    INTO DateTable VALUES (GETDATE()+4000)INSERT
    INTO DateTable VALUES (GETDATE()+40000)INSERT
    INTO DateTable VALUES (NULL)INSERT
    INTO DateTable VALUES ('')INSERT INTO DateTable VALUES (' ')
    SELECT

    CASE
    WHEN ISDATE(DateCol) = 1 AND CAST(DateCol AS DATETIME) BETWEEN '2009-11-12' AND '2020-11-12' THEN 'D'
    WHEN ISDATE(DateCol) = 1 THEN 'W'
    WHEN ISNULL(DateCol, '') = '' THEN '(BLANK)'
    END AS Date
    FROM DateTable
  3. Thank you and it did work.

Share This Page