how can we change the date format of SQL Server(2008) Database?

Abhishek Kumar Ashu

For data saved via win form, the input date format is(dd/mm/yyyy). The database showsin (yyyy-mm-dd) format. How can I change date format?

private void btnAllocate_Click(object sender, EventArgs e)
        {

            for (int i = 0; i < dgvDetails.Rows.Count; i++)
            {
                SqlConnection con = new SqlConnection("server=HP-HP; database=Restaurent; user id=sa; password=sql2008");
                SqlCommand cmd = new SqlCommand("INSERT INTO WtrTblAllot (WtrNm,Type,No,currDate) values (@WtrNm,@Type,@No,@currDate)", con);
                cmd.Parameters.AddWithValue("@WtrNm", dgvDetails.Rows[i].Cells[0].Value);
                cmd.Parameters.AddWithValue("@Type", dgvDetails.Rows[i].Cells[1].Value);
                cmd.Parameters.AddWithValue("@No", dgvDetails.Rows[i].Cells[2].Value);
                cmd.Parameters.AddWithValue("@currDate", dtTmPkr.Value);
                con.Open();
                cmd.ExecuteNonQuery();
                cmd = null;
              }
        MessageBox.Show("Added successfully!");
}

date time piker (dtTmPkr) format (dd/mm/yyyy)

marc_s

SQL Server doesn't store a DateTime in any string format - it's stored as an 8 byte numerical value.

The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime.

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT.

So really, it's not about changing the date format in SQL Server - it's about how to format and display a Datetime that you retrieve from SQL Server. Use the appropriate T-SQL CONVERT parameters, or format the DateTime in your C# front-end code

Update: if you're inserting the value from the DateTimePicker into SQL Server using your query shown in the question, you should be just fine - it's inserting a DateTime parameter so you won't have any issues with string formatting of your dates.

When you need to convert a DATETIME in SQL Server to a specific format - use

SELECT CONVERT(VARCHAR(50), YourDateColumn, 103)

and this will give you a date in dd/mm/yyyy (British/French) format. If you need a different format - use a different style (some other number than 103). Those styles are very well documented here.

If you read back the DateTime column into C# and you need to format it, then use

string formatted = YourDateTime.ToString("dd/MM/yyyy");

to get the values you need. Watch out: to format the month, use MM (capitalized!) because the mm would format the minutes of your DateTime instead.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I change properties of FK in SQL Server database project?

분류에서Dev

How to change date format coming from database to android

분류에서Dev

How to change format of date and insert it in database using php and html only

분류에서Dev

How to change Date/Time format to English?

분류에서Dev

How to change date-time format?

분류에서Dev

How to add date in SQL Server database in correct way?

분류에서Dev

Why we can edit view in sql server

분류에서Dev

How i can convert format date to d/M/yyyy and convert format time to h:mm:ss AM in SQL

분류에서Dev

How can I format date_space_hour to a time format

분류에서Dev

how to split a xml format data into row column format in sql server 2008 using stored procedure

분류에서Dev

How to change date format in file name using bash/linux?

분류에서Dev

How to change the date/time format to English from the command line?

분류에서Dev

Convert date format for SQL

분류에서Dev

How can I change this HAML format to normal CSS style?

분류에서Dev

What is the collation we have to use for ½ symbol in SQL Server 2008?

분류에서Dev

How to check if SQL Server 2008 R2 database exists using power shell

분류에서Dev

How can I access a SQL Server database with . in name via set-location

분류에서Dev

fmt:formatDate returns 12 hr format.How can i change it to 24 hr format

분류에서Dev

Inputting Date into Database (phpMyAdmin, SQL)

분류에서Dev

how can i retrieve date from mysql database and subtract it with current date?

분류에서Dev

Get Last Change Date in SQL

분류에서Dev

How to import huge blob into SQL Server database?

분류에서Dev

How to format output date in Itemwriter?

분류에서Dev

How to use date format in postgresql?

분류에서Dev

Can we fire a sql query with multiple where IN clause , if yes how does it work?

분류에서Dev

How to change the name of table in SQL Server that is a keyword

분류에서Dev

Execute Datetime from C# to date in SQL Server 2008

분류에서Dev

how can i run 3 while loops simultaneously in unix and update the output of each loop to database as soon we get the output

분류에서Dev

Can date format current time for GMT timezone?

Related 관련 기사

  1. 1

    How can I change properties of FK in SQL Server database project?

  2. 2

    How to change date format coming from database to android

  3. 3

    How to change format of date and insert it in database using php and html only

  4. 4

    How to change Date/Time format to English?

  5. 5

    How to change date-time format?

  6. 6

    How to add date in SQL Server database in correct way?

  7. 7

    Why we can edit view in sql server

  8. 8

    How i can convert format date to d/M/yyyy and convert format time to h:mm:ss AM in SQL

  9. 9

    How can I format date_space_hour to a time format

  10. 10

    how to split a xml format data into row column format in sql server 2008 using stored procedure

  11. 11

    How to change date format in file name using bash/linux?

  12. 12

    How to change the date/time format to English from the command line?

  13. 13

    Convert date format for SQL

  14. 14

    How can I change this HAML format to normal CSS style?

  15. 15

    What is the collation we have to use for ½ symbol in SQL Server 2008?

  16. 16

    How to check if SQL Server 2008 R2 database exists using power shell

  17. 17

    How can I access a SQL Server database with . in name via set-location

  18. 18

    fmt:formatDate returns 12 hr format.How can i change it to 24 hr format

  19. 19

    Inputting Date into Database (phpMyAdmin, SQL)

  20. 20

    how can i retrieve date from mysql database and subtract it with current date?

  21. 21

    Get Last Change Date in SQL

  22. 22

    How to import huge blob into SQL Server database?

  23. 23

    How to format output date in Itemwriter?

  24. 24

    How to use date format in postgresql?

  25. 25

    Can we fire a sql query with multiple where IN clause , if yes how does it work?

  26. 26

    How to change the name of table in SQL Server that is a keyword

  27. 27

    Execute Datetime from C# to date in SQL Server 2008

  28. 28

    how can i run 3 while loops simultaneously in unix and update the output of each loop to database as soon we get the output

  29. 29

    Can date format current time for GMT timezone?

뜨겁다태그

보관