Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Date


Date

Author
Message
zulmanclock
zulmanclock
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 455
Hello all. I need to get the last day of a month based on the months name. I dont want to have to do a case statement i would like to use, if possible,
(SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,getdate()))),DATEADD(mm,1,getdate())),101))

which i use to get the last day of the current month. I just cant wrap my head around how to pass the datename or even the date number such as 1 for january and have the query return the last day of january. To add a dimension i also have the year so i would pass something like January 2012 or 1-2012...is this possible?

Thank you for your help.
zulmanclock
zulmanclock
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 Visits: 455
Figured it out. Had to do some string manipulation but i got itSmile Would have been easy if i wasnt working with data that came from Unix.

   SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,Cast(Field1 as varchar(10))+'/1/'
            + Case When Field2 = 'L' THEN '2012'
            When Field2 = 'T' THEN '2013' End))),DATEADD(mm,1,Cast(Field1 as varchar(10))+'/1/'
            + Case When Field2 = 'L' THEN '2012'
            When Field2 = 'T' THEN '2013' End)),101)
            from dbo.GJORHIST


Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
zulmanclock (5/10/2013)
Figured it out. Had to do some string manipulation but i got itSmile Would have been easy if i wasnt working with data that came from Unix.

   SELECT CONVERT(VARCHAR(25),DATEADD(dd,-(DAY(DATEADD(mm,1,Cast(Field1 as varchar(10))+'/1/'
            + Case When Field2 = 'L' THEN '2012'
            When Field2 = 'T' THEN '2013' End))),DATEADD(mm,1,Cast(Field1 as varchar(10))+'/1/'
            + Case When Field2 = 'L' THEN '2012'
            When Field2 = 'T' THEN '2013' End)),101)
            from dbo.GJORHIST



There may be a better way if you would provide us with the information needed. Your original post was vague and your second post appears to be using data from a table.

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)
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