SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to split the date format into month with year?


How to split the date format into month with year?

Author
Message
raghuldrag
raghuldrag
Mr or Mrs. 500
Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)

Group: General Forum Members
Points: 520 Visits: 473
create table estr

(
custo int,
product varchar(20),
val int,
billed_date datetime
)



insert into estr(custo,product,billed_date,value) values('10','Milk','2013-04-11','2000')

insert into estr(custo,product,billed_date,value) values('10','Milk','2013-04-21','500')

insert into estr(custo,product,billed_date,value) values('10','Milk','2013-05-05','100')

insert into estr(custo,product,billed_date,value) values('10','Milk','2013-05-11','2000')

insert into estr(custo,product,billed_date,value) values('10','fruits','2013-05-11','500')

insert into estr(custo,product,billed_date,value) values('30','fruits','2013-04-11','2000')


insert into estr(custo,product,billed_date,value) values('30','fruits','2013-05-11','1000')

from here i wanna extract the month with year for counting purpose how ll do ?
guide me?
twin.devil
twin.devil
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5464 Visits: 2692
Share your expected output as well for better understanding.
raghuldrag
raghuldrag
Mr or Mrs. 500
Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)

Group: General Forum Members
Points: 520 Visits: 473
custo product val no.of.mon
10 Friut 1300 1
10 Milk 2900 2
30 Milk 6000 2


i wanna calculate no.of.months along with year ?
twin.devil
twin.devil
SSCertifiable
SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)SSCertifiable (5.5K reputation)

Group: General Forum Members
Points: 5464 Visits: 2692
raghuldrag (12/31/2013)
custo product val no.of.mon
10 Friut 1300 1
10 Milk 2900 2
30 Milk 6000 2


i wanna calculate no.of.months along with year ?



what representation for 'no.of.mon' column ? need more details
ChrisM@Work
ChrisM@Work
SSC-Forever
SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)SSC-Forever (42K reputation)

Group: General Forum Members
Points: 42458 Visits: 20012
Split thread. Please post replies to the original thread here, where you will obtain background information and progress to date.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
raghuldrag
raghuldrag
Mr or Mrs. 500
Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)Mr or Mrs. 500 (520 reputation)

Group: General Forum Members
Points: 520 Visits: 473
i need the output like

for example 2013-04-01 is in table my expectation output is 04-2013 (extract month with year)......
SQLBlimp
SQLBlimp
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1691 Visits: 510
raghuldrag (12/31/2013)

i need the output like

for example 2013-04-01 is in table my expectation output is 04-2013 (extract month with year)......


Hi.

This seems to be a relatively simple single SELECT statement.

Please use DATEPART to convert billed_date into a year with yyyy, and a second datepart to get the month with mm.

Then group your query the way you need to summarize the data, using SUM to add up values.

Thanks
John.
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97159 Visits: 38988
Assuming this is still true: billed_date datetime

right('0' + cast(month(billed_date) as varchar(2)),2) + '-' + cast(year(billed_date) as varchar(4))

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)SSC Guru (97K reputation)

Group: General Forum Members
Points: 97159 Visits: 38988
Now, I have a suggestion. You should get to know Books Online. In SSMS press Shift-F1. There is a lot of good information in there and you don't have to rely on waiting for volunteers to answer all your questions. If something doesn't make sense there, come ask questions here.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)SSC Guru (219K reputation)

Group: General Forum Members
Points: 219884 Visits: 42002
Lynn Pettis (12/31/2013)
Assuming this is still true: billed_date datetime

right('0' + cast(month(billed_date) as varchar(2)),2) + '-' + cast(year(billed_date) as varchar(4))



... or...

 SELECT RIGHT(CONVERT(CHAR(10),GETDATE(),105),7)




--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search