|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Pulling parts of a date from a SQL table
I've got two date fields in a SQL table, dateStarted and dateEnded. I'm trying to pull the day, month and year info from them into separate textboxes (i.e. txtMth, txtDay, txtYear, txtMth2, txtDay2, txtYear2) using VB. Assuming I've already open the SQL connection how would I proceed to populate the textboxes w/ the days, months, and years separately?
|
|
#2
|
|||
|
|||
|
Are you trying to do the date parts in ASP, ASP.NET or SQL? It's slightly different for each way of creating it.<br><br>Classic ASP (assumes we are assigning the date value to the variable) <div class="msgQuoteWrap"><div class="msgCode"><br><%<br>Dim dateStarted <br>dateStarted = {assign value from SQL recordset}<br>Dim dsMonth : dsMonth = Month(dateStarted)<br>Dim dsDay : dsDay = Day(dateStarted)<br>Dim dsYear : dsYear = Year(dateStarted)<br>%></div></div><br><br>ASP.NET (using VB.NET)<div class="msgQuoteWrap"><div class="msgCode"><br>Dim dateStarted As Datetime = {assign value from SQL Recordset}<br>Dim dsMonth as String = dateStarted.ToString("mm")<br>Dim dsDay as String = dateStarted.ToString("dd")<br>Dim dsYear as String = dateStarted.ToString("yyyy")<br></div></div><br><br>SQL (use this to output individual values, assumes value of dateStarted) <div class="msgQuoteWrap"><div class="msgCode"><br>DatePart(mm, dateStarted) -- month<br>DatePart(dd, dateStarted) -- day<br>DatePart(yyyy, dateStarted) -- year<br></div></div><p> </p><p><hr size="1" width="50%" align="left" />dotNetBB Forum Developer<br><b><i>Building A Better Community, One Post At A Time</i></b><br><a href="http://www.dotnetbb.com" target="_blank">www.dotnetbb.com</a></p>
|
|
#3
|
|||
|
|||
|
ASP.NET as always. So for that example, would the name of the SQL field go in where {assign value from SQL Recordset} is? Am I understanding that right?
|
|
#4
|
|||
|
|||
|
Yes, my fault, I should have said DataReader (or whatever method you are using) since Recordsets no longer exist in .NET.<p> </p><p><hr size="1" width="50%" align="left" />dotNetBB Forum Developer<br><b><i>Building A Better Community, One Post At A Time</i></b><br><a href="http://www.dotnetbb.com" target="_blank">www.dotnetbb.com</a></p>
|
|
#5
|
|||
|
|||
|
So I could do the first variable like so:<br><br><div class="msgQuoteWrap"><div class="msgCode">Dim dateStarted As Datetime = Trim(objSQLListPick.returnDataViewValue(intCounter )("dateInTable").ToString())</div></div> <br><br><img src="/forum/emoticons/idea.gif" alt="idea">
|
|
#6
|
|||
|
|||
|
You would need to do...<br><br>Dim dateStarted As String = Trim(objSQLListPick.returnDataViewValue(intCounter )("dateInTable").ToString())<br><br>Since you are using a .ToString() on the end. Otherwise you could use the Datetime as you previously defined.<p> </p><p><hr size="1" width="50%" align="left" />dotNetBB Forum Developer<br><b><i>Building A Better Community, One Post At A Time</i></b><br><a href="http://www.dotnetbb.com" target="_blank">www.dotnetbb.com</a></p>
|
|
#7
|
|||
|
|||
|
Well, it works both ways apparently. Thanks for your help.
|
|
#8
|
|||
|
|||
|
<img src="/forum/emoticons/yeah.gif" alt="yeah"><p> </p><p><hr size="1" width="50%" align="left" />dotNetBB Forum Developer<br><b><i>Building A Better Community, One Post At A Time</i></b><br><a href="http://www.dotnetbb.com" target="_blank">www.dotnetbb.com</a></p>
|
|
#9
|
|||
|
|||
|
Cute.
<br><br>One thing I noticed; I just realized I need to convert the month value from numeric to alpha and vice-versa (ex: 2 to February and back). How can this be accomplished? |
![]() |
| Viewing: ASP Free Forums > Programming > .NET Development > Pulling parts of a date from a SQL table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|