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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 converting the date to month and day with examples

Author  Topic 

pavlos
Yak Posting Veteran

59 Posts

Posted - 2011-12-27 : 21:15:52
Hey all,

I have run the following query:

select GETDATE()

--and it returns
(No column name)
2011-12-28 13:08:40.350

i want to convert the date into 3 seperate columns

YEAR | MONTH | DAY
2011 | 12 | 28

is it possible?

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-27 : 21:23:18
[code]
select datepart(year, getdate()), datepart(month, getdate()), datepart(day, getdate())
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

pavlos
Yak Posting Veteran

59 Posts

Posted - 2011-12-27 : 21:28:21
Wow thanks,

how would I go just subtracting 1 day off the date
is it getdate(day)-1

Go to Top of Page

pavlos
Yak Posting Veteran

59 Posts

Posted - 2011-12-27 : 21:31:18
select (getdate() - DAY(7))

this works..
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-27 : 22:13:58
quote:
Originally posted by pavlos

Wow thanks,

how would I go just subtracting 1 day off the date
is it getdate(day)-1





select getdate() - 1

or

select dateadd(day, -1, getdate())



KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-12-27 : 22:16:27
quote:
Originally posted by pavlos

select (getdate() - DAY(7))

this works..



this actually subtract 8 days from current day.

date value 0 is 1900-01-01
date value 7 is 1900-01-08
so DAY(7) which is DAY('1900-01-08') gives you the "day" of the input date which is 8

day( <date> ) is the same as datepart(day, <date> )


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -