Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dateformat in SQL SERVER 2005

Posted on 2007-03-30
19
Medium Priority
?
513 Views
Last Modified: 2008-02-01
HI,

How do i get date in different format as output.
User gives a string 11/21/2005
and also gives dateformat MM/DD/YYYY
I have to insert this value in SQL SERVER 2005


0
Comment
Question by:anusdesai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 5
  • 3
19 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18824372
select convert(datetime, '11/21/2005', 101)

alternatively:
SET DATEFORMAT MDY
select convert(datetime, '11/21/2005' )

0
 

Author Comment

by:anusdesai
ID: 18824530
Hi,
i get an error when i type SET DATEFORMAT MDY
select convert(datetime, '11/21/2005' )
Let Set statements are no longer supported...


0
 

Author Comment

by:anusdesai
ID: 18824532
I am using vb.net 2.0
0
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 

Author Comment

by:anusdesai
ID: 18824577
Hi,
I ned a function in my application to convert the string in the date format given by the user and then insert the value in sql.

Regards.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18824616
if the user gives the date in the format you told in the question, then you simply put that value into the sql as I showed above.
0
 

Author Comment

by:anusdesai
ID: 18824669
Hi,
How can i put this in query?


0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18824685
please show the code that you use in vb.net to run a query.
0
 

Author Comment

by:anusdesai
ID: 18824714
Besides I dont want to change the date format of sql server ...
Example...user has given
05/02/2005
he mentions
MM/DD/YYYY....
WHEN I CONVERT the string  IT TO A DATE
IT shows 05/02/2005 which according to system date is DD/MM/YYYY AND WHEN I SAVE IN SQL

YYYY/MM/DD IT gets saved as 2005/02/05 which is wrong as i expect 2005/05/02
How can I do it?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18824743
please, show the code that you use in vb.net to run a query.
0
 

Author Comment

by:anusdesai
ID: 18828393
Hi,
Sorry for the delay..
I have a column in datagridview for entering  date format
 Me.clmDTF.DataPropertyName = "DATA_TYPE_FORMAT"
        Me.clmDTF.HeaderText = "SPECIAL TYPE"
        Me.clmDTF.Name = "clmDTF"
I am loading a file in database depending on the  columns given.
In file the data is:
23/12/2004   xys  fgfg  

Query
   strsql = "Insert into " & frmmain.txttablename.Text & "( refno," & frmmain.txtdateText & ")VALUES(@refno,@" & frmmain.txtdate.Text & ")"

                        Dim command As New SqlCommand(strsql, cndb)

                        If fieldname <> "" Then
                            command.Parameters.AddWithValue("@refno", fieldname)
                            command.Parameters.AddWithValue("@" & frmmain.txtdate.Text & "", frmmain.txtfielddate.Text)


                            command.ExecuteNonQuery()
                        End If



       '''''''''''''''''''
 & frmmain.txtprmt.Text & is for the formated date.


Regards.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18828521
just try to change the strsql contents like this:

strsql = "SET DATEFORMAT DMY   Insert into " & frmmain.txttablename.Text & "( refno," & frmmain.txtdateText & ")VALUES(@refno,@" & frmmain.txtdate.Text & ")"

0
 
LVL 3

Accepted Solution

by:
r1937 earned 2000 total points
ID: 18830824
Hi,
To answer your original question:

Dim strMyDate As String
strMyDate = Format(DateTime.ParseExact("11-21-2005", "MM-dd-yyyy", _
Globalization.CultureInfo.CurrentCulture), "yyyy-MM-dd")

This will convert from MM-dd-yyyy to yyyy-MM-dd

To apply to your code, just replace the example date "11-21-2005" with frmmain.txtdate.Text and SQL to be like:

   strsql = "Insert into " & frmmain.txttablename.Text & "( refno," & frmmain.txtdate.Text & ")VALUES(@refno, '" & strMyDate & "')"

I am not sure about your SQL, but make sure date goes like '2005-11-21' - (within apostrophes - as I can remember).

Hope this will help, Good luck.

r1937
0
 

Author Comment

by:anusdesai
ID: 18831994
Hi Experts,

Thanks a lotz..will try and get back..

Regards.
0
 

Author Comment

by:anusdesai
ID: 18835067
Hi r1937,

It works great only if MM is in Uppercase but, the user can enter it in lower case in that case it takes the minutes...is there any method in either case it will work correct for foramttting the date.
0
 
LVL 3

Expert Comment

by:r1937
ID: 18835234
What about taking the entered date format into a string and then do a validation check or write code to convert mm to MM.
0
 

Author Comment

by:anusdesai
ID: 18835468
Yes works gr8

Cheers mate...
0
 

Author Comment

by:anusdesai
ID: 18835554
HI,

Can you suggest what it will be in oracle 9i.

Regards.
0
 
LVL 3

Expert Comment

by:r1937
ID: 18838294
I am not familiar with Oracle, but most simple SQL will be the same.
As I think Oracle dates goes like "dd-MMM-yyyy". Again I do not know Oracle.
0
 

Author Comment

by:anusdesai
ID: 18842676
I found it...
Cheers Mate
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question