Change Date Format using Existing Date - sql

I have a table that contains 20,000 rows. I want to change my existing date format from 2016-04-04 to 4/4/16. I have googled it but I only found the solution for changing to the current time. This is my query.
UPDATE [master].[dbo].[TRY]
SET Target_Hire_Date = CONVERT(VARCHAR(20), GETDATE(), 103)
But this query only for the current time which is not working for my problem.

Use your date column instead.
UPDATE [master].[dbo].[TRY]
SET Target_Hire_Date = CONVERT(VARCHAR(20), [YOUR-DATE-COLUMN], 103)

UPDATE [master].[dbo].[TRY]
SET Target_Hire_Date =
CONVERT(VARCHAR(10),CAST(#a as DATE),103)

to have exactly the format d/m/yy, try this:
UPDATE [master].[dbo].[TRY]
SET Target_Hire_Date = convert(nvarchar,day(YOUR_EXISTING_DATE)) + '/'
+ convert(nvarchar,MONTH(YOUR_EXISTING_DATE)) + '/' +
convert(nvarchar,RIGHT(YEAR(YOUR_EXISTING_DATE),2))
just supposing the YOUR_EXISTING_DATE is DATETIME
is better to store dates in a DATE column, then choose the format with the query

Related

SQL Server : change date format

I need to change the date format from 'yyyy-mm-dd' to 'dd.mm.yyyy'.
I have data in my table like this '2018-08-08', I need convert it to '08.08.2018'.
I have tried:
UPDATE daily_tasks
SET date = REPLACE(date, date, CONVERT(VARCHAR(255), daily_tasks.date, 102))
WHERE 1;
But, it doesn't work.
Ideally you should be storing your dates as bona-fide date columns, not as text. That being said, the date text '2018-08-08' is in fact in an ISO format, and would still allow you to do things like sort and compare against other date literals, so it is not so bad.
But converting this text to a '08.08.2018' format is the wrong thing to do. If a anything, you might want to consider adding a new date column new_date to store this date information. Do that, and then populate it with:
UPDATE daily_tasks
SET new_date = TRY_CONVERT(datetime, date);
Store your date as DATE datatype and when you read data from database use
DECLARE #myDate DATE = '2018-08-08'
SELECT FORMAT(#myDate, 'dd.MM.yyyy')
SELECT CONVERT(VARCHAR(10), #myDate, 104)
Your syntax looks like SQL Sever, so i would do :
UPDATE daily_tasks
SET Col = REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
WHERE . . . ;
However, i would not recommend to do this, just use CONVERT() with SELECT statement whenever necessary :
SELECT REPLACE(CONVERT(VARCHAR(10), daily_tasks.date, 103), '/', '.')
Regardless of the database, dates are stored in an internal format. This is the correct way to store dates. Do not store dates as strings.
You can specify the format when you query:
CONVERT(VARCHAR(255), daily_tasks.date, 102)
Or, you can even add a computed column to provide this information:
alter table daily_tasks
add date_display as ( CONVERT(VARCHAR(255), daily_tasks.date, 102) ) ;
You could convert the date column to a varchar to store the date in your specified format. However I strongly recommend against this. You should leave it stored as a date.
If you want to do a SELECT to get the data out then you can convert it to your specified format like this:
SELECT CONVERT(VARCHAR, daily_tasks.date, 4)

finding data lying between a specific date range in sql

I want to find records from my database which lie between any user input date range(say between 10/2/2008 to 26/9/2024). I tried using
SELECT NAME
,TYPE
,COMP_NAME
,BATCH_NO
,SHELF
,MFG_DATE
,EXP_DATE
,QTY
,VAT
,MRP
FROM STOCK_LOCAL
WHERE
convert(VARCHAR(20), EXP_DATE, 103)
BETWEEN convert(VARCHAR(20), #MEDICINEEXP_DATE, 103)
AND convert(VARCHAR(20), #MEDICINEEXPDATE, 103)
but with this query i need to enter perfect date range which is available in my database, it is not giving me data lying in between any date entered.
Thanks in advance
Since it is a poolr designed schema there isnt going to be any decent/Efficient solution for this.
In sql server if you are storing Date or Date & Time data. Use the Data or DATETIME datatypes for your columns.
In your case you are trying to compare a string with passed date. and even when you tried to convert the string (Date) into date datatype you didnt do it correctly.
My suggestion would be Add new columns to your table with Date datatype and update these columns with existing date/string values.
For now you can convert the Date(string) into date datatype using the following code.
DECLARE #MEDICINEEXP_DATE DATE = 'SomeValue1'
DECLARE #MEDICINEEXPDATE DATE = 'SomeValue1'
SELECT query....
FROM TableName
WHERE
CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) >= #MEDICINEEXP_DATE
AND CAST(
RIGHT(EXP_DATE, 4)
+SUBSTRING(EXP_DATE,CHARINDEX('/',EXP_DATE)+1,2)
+LEFT(EXP_DATE,2)
AS DATE) <= #MEDICINEEXPDATE
Note
This solution will get you the expected results but very inefficient method. It will not make use of any indexses on your EXP_DATE Column even if you have a very buffed up index on that column.

update date format in SQL developer

I need to convert date format in SQL
The current format is yyyy/mm/dd-hh:mm:ss:sss and I need to convert it to yyyy-mm-dd hh:mm:ss CST
I don't really know SQL but did some research and found that I can use instr to find the string and replace it, however, no matter what I try,there is always something off :(
Could anyone here help me with it? thanks in advance.
By the way, it's in oracle sql developer so syntax are different from previous answer. Thanks again
If your current column is stored as a varchar (which it looks like it is based on your example) you can convert it the following way to a datetime.
declare #date varchar(25)
set #date = '2012/02/16-09:40:30:000'
select Convert(datetime, Left(#date, 4) + '-' +
substring(#date, 6, 2) + '-' +
substring(#date, 9, 2) + ' ' +
substring(#date, 12, 8)) As NewDate
And the result would be 2012-02-16 09:40:30.000
There are lots of sites that have details on converting datetime:
http://www.sql-server-helper.com/tips/date-formats.aspx
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
If the date is stored as a datetime, then you can get your format by doing:
SELECT convert(varchar, getdate(), 120)
thank you so much for your guys help! I got this sorted out by other user's help, the command is,
update b
set first= to_char(substr(FIRST,1,4)||'-'||substr(FIRST, 6, 2)||'-'||substr(FIRST, 9, 2)||' '||substr(FIRST, 12, 8))
Thanks :)
Mylie
The CONVERT() function is normally used for this. The third parameter lets you specify the required date formatting.
If the date is already in a string, then convert it first into a date type and then convert it back to a string in the required format.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
If you're using SQL Server and it's a date field you can do:
convert(varchar,getdate(),120)
Replacing getdate() with the column name
If the date is stored as a DATETIME, then you can convert it to a string in the format you want by doing the following:
SELECT CONVERT(VARCHAR(19),YourColumn,120)
FROM YourTable
CAST AND CONVERT
declare #ds varchar(23)='2012/02/16 12:57:03:002'; --your current format
select convert(varchar(30),cast(#ds as datetime),120)+' CST' --new format you specified

How to change the date format

Table:
Dates (nvarchar)
==
23/02/2009 (dd/mm/yyyy)
24/02/2009
25/08/2009
28/12/2011
....
I want to change the date form like this (yyyymmdd)
Tried query
select cast(dates as datetime) from table1
Showing error
ЮArithemetic expression overflow
select Convert(char(10), dates, 112) from table1
Showing the same value 23/02/2009
The above query is not working.
Expected Output
20090232
20090224
20090825
20111228
...
How to make a query?
Use this article.
select CONVERT(varchar(8),CONVERT(datetime, '23/02/2009',103),112)
Try this simple query:
SELECT DATEPART(yyyy,dates)+DATEPART(m,dates)+DATEPART(d,dates) FROM table1
You can get more information on this link:
sql-server-dates
select convert(varchar, getdate(), 102)[Dates] from tablename
Take a look on this link: http://www.technoreader.com/SQL-Server-Date-Time-Format.aspx
Since these are stored as string data then you might as well just use the string functions to give you what you want.
First to give you a valid date
select
cast(SUBSTRING(dates,4,2) + '/' + left(dates,2) + '/' + right(dates,4) as datetime) NowItsADateTime
from DateTable
And second, what you asked for.
select right(dates,4) + SUBSTRING(dates,4,2) + left(dates,2) as YYYYMMDD
from DateTable

SQL generate string from splitting another

I have a number of corrupt Dates in an Advantage database, but luckily I have a reverse date field which is intact.
I need to use SQL to recreate the Date field in the form 'DD/MM/YYYY' from the RDate string 'YYYYMMDD'.
Something equivalent to:
UPDATE table SET Date = FormatDate('DD/MM/YY',StrToDate('YYYYMMDD',RDate))
I don't know if this is possible to select character positions and use variables this way in SQL or if I'll have to write a program (in Delphi) to do the operation.
Thanks
Try something like
update table set
date = cast(substring(rdate, 5, 2) + '/' +
right(rdate, 2) + '/' +
left(rdate, 4) as date)
/* where ... */
(assuming your connection's date format setting is the default 'MM/DD/YYYY')
TOndrej I'm not sure if it was a syntax error or if cast is not recognised but I achieved the desired result with the following SQL:
update members set "Expiry Date" = ((substring(RExpiryDate,7,2)) + '/' + (substring(RExpiryDate,5,2)) + '/' + (substring(RExpiryDate,1,4)))
Thanks for your help