I want to find january 1st of a year based on the year from datetimepicker
-
Friday, January 25, 2013 10:10 PMI have a issue where I select a date (m/d/yyyy) from datetimepicker. What I need to do is, using the year from the date selected, find january 1st of that year. Any one got any ideas? Sounds simple enough but the selected date is a string. It is a string because the key in the database is the date in string format.
- Moved by Kalman TothMicrosoft Community Contributor Saturday, January 26, 2013 12:58 AM Not T-SQL.
All Replies
-
Friday, January 25, 2013 10:15 PMselect convert(date,'01/01/'+convert(varchar(4),DATEPART(year,'12/12/2012')))
Hope it Helps!!
-
Friday, January 25, 2013 10:38 PM
Try:
SELECT CONVERT(DATE, RIGHT('03/30/2013',4)+'0101'); -- 2013-01-01You should always use YYYY-MM-DD string date format.
Datetime/text conversion:
http://www.sqlusa.com/bestpractices/datetimeconversion/
Kalman Toth SQL 2008 GRAND SLAM
Paperback: Pass SQL Exam 70-461 & Job Interview: Programming SQL Server 2012
- Edited by Kalman TothMicrosoft Community Contributor Friday, January 25, 2013 11:05 PM
- Edited by Kalman TothMicrosoft Community Contributor Saturday, January 26, 2013 12:57 AM
-
Friday, January 25, 2013 10:52 PM
I get what you are saying but I notice you have a hardcoded date in your function. I don't have that. I have to pull the year from the string variable containing the date selected from datetimepicker which can be any date from 1/1/2007 to 12/31/2020. This is the code (as much as it is):
selected_date = DateTimePicker1.Text
where:
selected_date is in the format mm/dd/yyyy or m/d/yyyy
and:
the year is the LAST four positions in the string variable
which must be extracted and combined with '1/1' to get january 1 of that year
Any clearer?
-
Friday, January 25, 2013 10:54 PMselect '01/01/'+convert(varchar(4),DATEPART(year,Selected_date))
Hope it Helps!!
-
Friday, January 25, 2013 10:57 PMReplace the hardCoded date value with your parameter..
Hope it Helps!!
- Edited by Stan210 Friday, January 25, 2013 10:57 PM
-
Saturday, January 26, 2013 12:16 AM
Actually guys. I found the way. Here ya go.
Dim sdate As Date = selected_date
smonth = Microsoft.VisualBasic.DateAndTime.Month(sdate)
sDay = Microsoft.VisualBasic.DateAndTime.Day(sdate)
syear = Microsoft.VisualBasic.DateAndTime.Year(sdate)
totals_date = "1/1/" & syearworks!
-
Saturday, January 26, 2013 2:20 AM
Your result is not a date - it's a string. A solution that provides a date is
Dim Totals_Date As Date = New Date(sDate.Year, 1, 1)
Once you have a date you can express it as a string in any format you choose.
- Proposed As Answer by Blackwood Saturday, January 26, 2013 3:47 AM
- Marked As Answer by Shanks ZenMicrosoft Contingent Staff, Moderator Monday, February 04, 2013 6:21 AM
-
Saturday, January 26, 2013 3:50 AM
Acamar is correct. And the way to get a date (rather than a string) from a DateTimePicker is to use the Value property of DateTimePicker rather than the Text property:
Dim Totals_Date As Date = New Date(DateTimePicker1.Value.Year, 1, 1)
- Proposed As Answer by dbasnett Saturday, January 26, 2013 1:41 PM
- Marked As Answer by Shanks ZenMicrosoft Contingent Staff, Moderator Monday, February 04, 2013 6:21 AM

