How to update/Change the date format to MM/dd/yyyy from dd/MM/yyyy format in sql server? - sql

In my db DOB is saved in dd/mm/yyyy format. i want to change the DOB date format to MM/dd/yyyy. How can i do that?

First i want to know which datatype you are using for saving your date value. There is nothing provided with your code no sample code, table details nothing. anyway
i think your 'date of birth' field datatype is datetime,then you can use the following example
create table checktable(
ID int,
name nvarchar (30),
dob datetime);
Example data insert into the table
insert into checktable(ID,name,dob) values(10,'myname','03/01/2014');
//..........
select * from checktable
//Use CONVERT() it will give you the desired output
SELECT TOP 1 ID, dob,CONVERT(varchar,dob,101) 'mm/dd/yyyy'
FROM checktable
UPDATE
if your datatype is varchar and now it is in the format mm/dd/yyyy and you want to change it into dd/mm/yyyy format then use the following example it will help you
create table checktable1(
ID int,
name nvarchar (30),
dob varchar(20));
// insert sample data
insert into checktable1(ID,name,dob) values(10,'myname','21/05/2010');
select * from checktable1
// change the format using substring()
select * FROM checktable1
select dob,substring(dob,4,3)+substring(dob, 1, 3)+substring(dob, 7, 4) from checktable1
It will give you result in 05/21/2010 (mm/dd/yyyy)format

Microsoft SQL: https://msdn.microsoft.com/en-us/library/ms187928.aspx
Syntax for CONVERT:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
use example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) AS [MM/DD/YYYY]
For other databases search Date and Time Functions in documentation.

The CONVERT() function is a general function that converts an expression of one data type to another.
The CONVERT() function can be used to display date/time data in different formats.
http://www.w3schools.com/sql/func_convert.asp

if your data type is DateTime then no need to convert it, in later you can convert its format as you want but if your are using varchar or nvarchar
then you can use CONVERT() as below
SELECT convert(datetime, '23/10/2016', 103)
Result : 2016-10-23 00:00:00.000
if just want to conver in date then
SELECT convert(date, '23/10/2016', 103)
Result : 2016-10-23
for more information and other formats
http://www.sqlusa.com/bestpractices/datetimeconversion/
and if you just want to convert string to sting then
SELECT CONVERT(varchar,'23/10/2016',101)
Result : 23/10/2016

This is the query to show all data in dd/mm/yyyy format.Type your column name that contains date in place of datecolumnname and your table name in place of tablename in below query:
select convert(varchar,datecolumname,103) as datecolumname from tablename

Related

Convert Varchar field to datetime in SQL Server

I am trying to convert varchar date to date time field and having a really hard time. It is giving me:
Conversion failed when converting date and/or time from character string.
or
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
I have 2 datetime formats below :
Date field looks like 21-12-2009, 05-10-2005 etc
Date field looks like 19-03-2018 14:59
select cast(convert(varchar,Stg_hw.date_of_birth,110)as date)
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
select CONVERT(datetime,LEFT(date_of_birth,2)+SUBSTRING(Stg_hw.date_of_birth,4,2)+SUBSTRING(Stg_hw.date_of_birth,7,4))
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
select cast(Stg_hw.date_of_birth as date)
from Stg_Height_and_Weight Stg_hw
where Stg_hw.person_id = 1620458
CONVERT(DATETIME, DateFieldColumnName, 103) can help to convert those 2 format to datetime.
Sample exection:
CREATE TABLE TestTable (DateVal VARCHAR (20));
INSERT INTO TestTable (DateVal) VALUES
('21-12-2009'), ('05-10-2005'), ('19-03-2018 14:59');
SELECT CONVERT(DATETIME, DateVal, 103) AS Result
FROM TestTable
Please find the working demo on db<>fiddle

Converting a varchar column to datetime

I have a table that has a column for dateofbirth varchar(10). All the data inside is stored like this '02/01/1990'.
I need to convert it to datetime. I've tried
CAST(DateofBirth AS DATEITME) AS BirthDate
But I keep getting this error when I try importing:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
I need it like this:
1990-02-01 00:00:00:000
SELECT CONVERT(Datetime, '2011-09-28 18:01:00', 120) -- to convert it to Datetime SELECT CONVERT( VARCHAR(30), #date ,105) -- italian format [28-09-2011 18:01:00] + ' ' + SELECT CONVERT( VARCHAR(30), #date ,108 ) -- full date [with time/minutes/sec]
You need to convert a Varchar in format MM/DD/YYYY to a Datetime.
Sql server recognizes a set of predefined date formats that it is able to automatically parse. See this cheat list.
Your input format corresponds to sql server date format 101, hence you can do :
SELECT CONVERT(Datetime, '02/01/1990', 101)
If you try to do it in sql query there is syntax to do it .
Here a link ..
SQL Server Convert Varchar to Datetime
Your cast statement works for me:
Declare #dateofbirth varchar(10) = '02/01/1990'
select cast(#dateofbirth as datetime)AS BirthDate
Result:
BirthDate
1990-02-01 00:00:00.000

Dutch varchar date issue in SQL server 13 month

I have the following datetime format ( as varchar ) in my database 13-04-2018 1:05:00.
I need to convert it to the following format: 2018-04-13 01:05:00. As datetime.
Normal convert functions can't do this because they try to take the 13th month, and that month doesn't exist. This error:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
Does someone know how to convert this date issue?
Using datetimes is always a pain regardless of the language because of all the different formats across the world.
To sort your issue out currently, you need to use a format style which is a third parameter to CONVERT. Personally what I would suggest here is to store as a datetime, because storing datetimes as strings is never a good idea. It just gets too messy later on, but if saved in the format you would like, it would be saved as yyyy-MM-dd hh:mm:ss
SELECT CONVERT(DATETIME, '13-04-2018 1:05:00',103)
You can create your own function to format it in your desired output string.
CREATE FUNCTION FormatMyDate
(#Date DATETIME) RETURNS VARCHAR(20)
AS
BEGIN
RETURN FORMAT(#Date,'yyyy-dd-MM hh:mm:ss')
END
And then you can call it in SELECT statements like this:
SELECT dbo.FormatMyDate(yourDateCol)
FROM yourTable
this takes the date from the format where month comes before day and reverses the 2 values (month and day)
SELECT CONVERT(DATETIME, '2018-13-04 01:05:00', 103);
Results:
2018-04-13 01:05:00.000
This should work for you requirement...
SELECT FORMAT(yourdate, 'yyyy-dd-MM')
Your Solution Bro...
DECLARE #d DATETIME = GETDATE()
SELECT FORMAT ( #d, 'MM-dd-yyyy hh:mm:ss', 'de-de' ) AS 'Hoping your result'

Convert VARCHAR to DATE in SQL SERVER

I have VARCHAR column (MyValue) in my table. It has date value in two different format.
MyValue
----------
25-10-2016
2016-10-13
I would like to show them in DATE format.
I wrote query like below:
SELECT CONVERT(date, MyValue, 105) FROM MyTable
SELECT CAST(MyValue as date) FROM MyTable
Both are giving me this error. Conversion failed when converting date and/or time from character string.
Is there anyway convert to DATE datatype format even the value stored in different formats like above?
Expecting your answers. Thanks in advance.
Does this help?
declare #varchardates table
(
vcdate varchar(20)
)
INSERT INTO #varchardates VALUES
('25-10-2016'),
('2016-10-13')
SELECT CONVERT(date,vcdate, case when SUBSTRING(vcdate, 3, 1) = '-'
THEN 105 ELSE 126 END) as mydate
FROM #varchardates
Depending on how many different formats you have in your data, you may need to extend the case statement!
See here for list of the different format numbers
You can use TRY_CONVERT and COALESCE. TRY_CONVERT returns NULL if the conversion fails, COALESCE returns the first NOT NULL value:
SELECT COALESCE(TRY_CONVERT(DATETIME, x, 105), TRY_CONVERT(DATETIME, x, 120))
FROM (VALUES('25-10-2016'), ('2016-10-13')) a(x)
I assumed the value 2016-10-13 is in format yyyy-MM-dd.
You mention in a comment you may have other formats as well. In that case it gets very tricky. If you get a value 01-12-2017 and you have no idea about the format, there is no way to tell whether this is a date in januari or in december.

How to filter Time portion of a DateTime column

I want to retrive data from table according to a particular time so I use this query below.
select * from a
where convert(smalldatetime,date,datepart(hh,date)) ='09:12:00'
but I get no row as result. So what should I do for my expected result?
You can convert directly to varchar as below
SELECT * FROM a
WHERE CONVERT(VARCHAR(8), [date], 108) = '09:12:00'
Try this:
SELECT *
FROM a
WHERE CONVERT(VARCHAR(8), [DATE],108) = '09:12:00'
Your datatype is already smalldatetime so you have to convert to varchar and compare with string.
EDIT:
Answer explanation:
You have smalldatetime inside [DATE] column. For example 2015-10-01 09:12:00. To compare only time you need to convert to string which contains only time. For this reason you will use command CONVERT from TSQL.
It will convert smalldatetime to 8 characters string. As a result you will have 09:12:00 and then you compare it with your string.
In Sql server 2008 you can convert to TIME. By specifying TIME(0) you have the desired format:
SELECT *
FROM a
WHERE CAST(date as time(0)) ='09:12:00'
//CONVERT(data_type(length),expression,style)
SELECT * FROM a
WHERE CONVERT(varchar(8),[columnName],108) = '09:12:00'