Extract day,month,year from the recorded data in a database

  • hi,

    Can you help me how to extract the day,month,year from a data saved in a database.

    For example 2/4/2009 is in a database, What code or command should i use to extract the day, month and year from the data.

    thanks

  • You want to use the datepart function. You can look it up in BOL for all the permutations, but it basically works like this:

    SELECT DATENAME(yyyy,'3/12/2009') AS 'Year'

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Grant Fritchey (3/12/2009)


    You want to use the datepart function. You can look it up in BOL for all the permutations, but it basically works like this:

    SELECT DATENAME(yyyy,'3/12/2009') AS 'Year'

    Or:

    declare @ADate datetime;

    set @ADate = '2009-03-12';

    select YEAR(@ADate), MONTH(@ADate), DAY(@ADate)

    Again, look these up in BOL as well.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply