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
 convert date to a day

Author  Topic 

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-12 : 14:22:03
how to convert a date to a day? For example the date, "2010-11-26" is on a Friday? How can i convert any date to a day in sql? What is the syntax for it?

Roger DeFour

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 14:29:20
select datename(dw, getdate())


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-12 : 14:37:54
But that syntax is going to give me today's day which is wednesday, but how would i place a date in there in order to get back the right day besides today??

Roger DeFour
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 14:42:53
Do you have an example so I can know where your date will come from?


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-12 : 14:45:02
ok for example, what would the syntax be if i just put in any date in the format like this for example, "2010-12-25"? That date is on saturday, what would the syntax be to get the answer saturday by using that date?

Roger DeFour
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 14:46:55
select datename(dw, '20101225')


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 14:48:06
Or
select datename(dw, replace('2010-12-25','-',''))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 14:53:49
Or
select datename(dw, convert(datetime,'2010-12-25',126))



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-12 : 14:59:49
Thank you

Roger DeFour
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 15:01:53
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Trininole
Yak Posting Veteran

83 Posts

Posted - 2010-05-12 : 15:14:00
How would i write the syntax if today's date was on a thursday and today's date is not located in the table called "holiday", how would i make it write "yes" if today's date is not in the table and "no" if it is in the table?


Roger DeFour
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 15:28:44
something like this:
set nocount on
declare @holiday table (name_of_day varchar(30))
insert @holiday
select 'Monday' union all
select 'Tuesday' union all
select 'Wednesday' union all
select 'Friday'



if exists(select * from @holiday where name_of_day = datename(dw, getdate()))
begin
print datename(dw, getdate()) + ' is in table'
end
else
begin
print datename(dw, getdate()) + ' is not in table'
end



No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2010-05-12 : 15:30:04
You can replace @holiday by the name of your holiday table.
@holiday is only created and filled to have testdata.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2010-05-13 : 05:09:30
quote:
Originally posted by webfred

select datename(dw, '20101225')


No, you're never too old to Yak'n'Roll if you're too young to die.


Just for clarity use

select datename(weekday, '20101225')


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

kashyapsid
Yak Posting Veteran

78 Posts

Posted - 2010-05-13 : 05:38:27
yes this works

KaShYaP
Go to Top of Page
   

- Advertisement -