How do I convert yymmdd to dd/mm/yy format? - sql

I am using SQL Server 2008 and have the column MyDate that contains the date as 20141208. Its type is user-defined (UTypeDate:varchar(8)), and I want to convert it to 08/12/2014.
For example, given
MyDate (UTypeDate:varchar(8))
20141208
20141218
20141204
20141216
what I want is to write the query that will give me output as:
MyNewDate
08/12/2014
18/12/2014
04/12/2014
16/12/2014
How should I write the query for this?

Try this
SELECT CONVERT(VARCHAR(10), CAST('20141208 ' AS DATE), 103)
RESULT
I think you need an extra column on your desired format. Then use the following query.
SELECT VARCHARDATE,
CONVERT(VARCHAR(10), CAST(VARCHARDATE AS DATE), 103) NEWDATE
FROM YourTable
which forms the below result
You can apply your logic con NEWDATE columns as per your requirements.
Let me know for any changes
Check the sqlfIDDLE http://sqlfiddle.com/#!3/af366/1 (Click RUNSQL button when site not loaded properly)

try this
SELECT CONVERT(VARCHAR(10), CAST(MyDate AS DATE), 103) FROM TableName
For visual prrof
Table Design
Data
Result

Related

SQL Server : searching between dates in string data type

I have a table, which stores dates as type varchar.
In the SELECT statement, I want to extract ALL values of the table, but filter the data BETWEEN two dates.
I have tried converting and this is the closest I have gotten. any help would be appreciated. The dates I want to filter by is called opened
SELECT *
FROM table
WHERE Opened BETWEEN CONVERT(DATETIME, '01/08/2006', 103)
AND CONVERT(DATETIME, '01/05/2020', 103)
I would discourage you from using between with dates. Here is a good explanation of why, provided by Aaron Bertrand.
select t.*
from table t
where t.opened >= '2016-08-01' and
t.opened < '2020-05-02'
If your opened is in the same date format, then use:
select t.*
from table t
where convert(date, t.opened, 103) >= '2016-08-01' and
convert(date, t.opened, 103) < '2020-05-02'
However, you should fix your table so the dates are stored using the correct types, not strings.

Selecting date in format dd/mm/yyyy from nchar column

I have date in column of nchar(255) datatype in this format
mm/dd/yyyy
Now I want to change it into
dd/mm/yyyy
I don't think that CONVERT help me in this as I have tried different queries like below
SELECT CONVERT(NCHAR(20), col1, 103) from table1
SELECT CONVERT(VARCHAR(20), col1, 103) from table1
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
In my view 103 which is in UK format converts only from yyyy-mm-dd
So I tried to CAST my col1 but I get an error.
Am I missing something? Is there any easy way to do this?
Do this in two expiicit steps. First, convert the string to a date using the 101 format (which is mm/dd/yyyy). Then explicitly convert the date back to a string using 103 (which is dd/mm/yyyy):
select convert(varchar(255), convert(date, datecol, 101), 103)
Two points. First, I don't think the result needs to be nvarchar() (the string only consists of numbers and a slash). Second, always include a length when using varchar() in SQL Server.
Here is a little SQL Fiddle.
You can try this:
SELECT CONVERT(VARCHAR, CONVERT(DATETIME, col1, 101), 103)
Check this:
Select convert(varchar(15), cast('01/26/2015' as datetime), 103) as FormattedDate
Where, '01/26/2015' is your col1
Edited Answer:
Select convert(nchar(255), cast(col1 as datetime), 103) as FormattedDate From table1
Where table1 is your table.
I am answering my own question.(Just in case anyone wants to know what is the solution)
There was no such problem with the query i was using. i.e.
select Convert(varchar(10),CONVERT(date,col1,101),103) from table1
The problem was with my nchar field.
It was having a special character (in my case a space) in every entry which was giving "out-of-range" error when tried to convert or cast.
So removing Special character(a space) solved my problem.

how to remove time from datetime

The field DATE in the database has the following format:
2012-11-12 00:00:00
I would like to remove the time from the date and return the date like this:
11/12/2012
First thing's first, if your dates are in varchar format change that, store dates as dates it will save you a lot of headaches and it is something that is best done sooner rather than later. The problem will only get worse.
Secondly, once you have a date DO NOT convert the date to a varchar! Keep it in date format and use formatting on the application side to get the required date format.
There are various methods to do this depending on your DBMS:
SQL-Server 2008 and later:
SELECT CAST(CURRENT_TIMESTAMP AS DATE)
SQL-Server 2005 and Earlier
SELECT DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
SQLite
SELECT DATE(NOW())
Oracle
SELECT TRUNC(CURRENT_TIMESTAMP)
Postgresql
SELECT CURRENT_TIMESTAMP::DATE
If you need to use culture specific formatting in your report you can either explicitly state the format of the receiving text box (e.g. dd/MM/yyyy), or you can set the language so that it shows the relevant date format for that language.
Either way this is much better handled outside of SQL as converting to varchar within SQL will impact any sorting you may do in your report.
If you cannot/will not change the datatype to DATETIME, then still convert it to a date within SQL (e.g. CONVERT(DATETIME, yourField)) before sending to report services and handle it as described above.
just use, (in TSQL)
SELECT convert(varchar, columnName, 101)
in MySQL
SELECT DATE_FORMAT(columnName, '%m/%d/%Y')
I found this method to be quite useful. However it will convert your date/time format to just date but never the less it does the job for what I need it for. (I just needed to display the date on a report, the time was irrelevant).
CAST(start_date AS DATE)
UPDATE
(Bear in mind I'm a trainee ;))
I figured an easier way to do this IF YOU'RE USING SSRS.
It's easier to actually change the textbox properties where the field is located in the report. Right click field>Number>Date and select the appropriate format!
SELECT DATE('2012-11-12 00:00:00');
returns
2012-11-12
Personally, I'd return the full, native datetime value and format this in the client code.
That way, you can use the user's locale setting to give the correct meaning to that user.
"11/12" is ambiguous. Is it:
12th November
11th December
For more info refer this: SQL Server Date Formats
[MM/DD/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 101) from tbl
[DD/MM/YYYY]
SELECT CONVERT(VARCHAR(10), cast(dt_col as date), 103) from tbl
Live Demo
TSQL
SELECT CONVERT(DATE, GETDATE()) // 2019-09-19
SELECT CAST(GETDATE() AS DATE) // 2019-09-19
SELECT CONVERT(VARCHAR, GETDATE(), 23) // 2019-09-19
In mysql at least, you can use DATE(theDate).
You may try the following:
SELECT CONVERT(VARCHAR(10),yourdate,101);
or this:
select cast(floor(cast(urdate as float)) as datetime);
Use this SQL:
SELECT DATE_FORMAT(date_column_here,'%d/%m/%Y') FROM table_name;

Wrong type in datetime column SQL Server

I have an application in asp. I insert data into SQL Server into a column of datetime type.
Let me give you an example for my question:
when I have the date 10/02/2012 and I insert it into SQL Server I see the data like this:
2012-10-02
but I would like to have it like this: 2012-02-10
When I have the date 29/02/2012 and I insert it into SQL Server I see the data in the correct format : 2012-02-29.
How can I manage the correct type I want?
The collation of database and table is Greek_CI_AS , in my language
any ideas how to fix it?
There are a few possibilities, but they all relate to the date format settings of the system components your strings are passing through (i.e. both the ASP runtime and your SQL Server).
There is a date format setting in SQL Server http://msdn.microsoft.com/en-us/library/ms189491.aspx
In ASP, the parsing of strings in VBScript depends upon the settings in effect during the parse - basically, http://support.microsoft.com/kb/306044
You can use CONVERT() to control the date format and you can specify a smaller target string to crop the result:
SELECT CONVERT(NVARCHAR(10), GETDATE(), 21) -- YYYY-MM-DD
SELECT CONVERT(NVARCHAR(10), GETDATE(), 102) -- YYYY.MM.DD
SELECT CONVERT(NVARCHAR(10), GETDATE(), 105) -- DD-MM-YYYY
SELECT CONVERT(NVARCHAR(10), GETDATE(), 110) -- MM-DD-YYYY
SELECT CONVERT(NVARCHAR(5), GETDATE(), 105) -- DD-MM
SELECT CONVERT(NVARCHAR(5), GETDATE(), 110) -- MM-DD
SELECT CONVERT(NVARCHAR(4), GETDATE(), 102) -- YYYY
-- To get YYYY-DD-MM, put two of the above together:
SELECT CONVERT(NVARCHAR(4), GETDATE(), 102)
+ '-' + CONVERT(NVARCHAR(5), GETDATE(), 105)
To force a date format for insertion, you can do a similar thing:
-- Insert in Italian dd-mm-yy (e.g. 10th February 2012)
INSERT INTO user_table VALUES (CONVERT(DATETIME, '10-02-12', 5));
-- Insert in USA mm-dd-yy (2nd October 2012)
INSERT INTO user_table VALUES (CONVERT(DATETIME, '10-02-12', 10));
See Microsoft MSDN reference CAST and CONVERT (Transact-SQL).
---- ANSWER TO ADDITIONAL QUESTION ----
I find your latest comment a little ambiguous. If you're asking how to search on a datetime field between two dates that you have in string format, then, try something like this:
SELECT *
FROM user_table
WHERE mydate BETWEEN convert(Datetime,'20/02/2012',103)
AND convert(Datetime,'01/03/2012')
whereas, if you are trying to search on an nvarchar field with two dates in string format, then, try something like this:
SELECT *
FROM user_table
WHERE convert(Datetime, mynvarchar, 103)
BETWEEN convert(Datetime,'20/02/2012',103)
AND convert(Datetime,'01/03/2012')
However, this is terribly inefficient. If you are going to be doing date searches a lot, I highly recommend storing your date field in datetime format. If you have a business requirement to store the nvarchar version, that's okay, but you can use dynamic columns, such as:
CREATE TABLE user_table
(
mynvarchar NVARCHAR(10), -- Date as a String in DD/MM/YYYY format
mydatetime AS CONVERT(DATETIME, mynvarchar, 103) PERSISTED
);
The advantage of this is the mydatetime field automatically updates itself and can be used in indexes and constraints if you wanted it to, but, you can manage it by manipulating the mynvarchar business columns.
In future, can I please request that when you ask question, that you provide more concrete examples, i.e. the name of your table, the name of your columns, so I don't have to keep inventing these.

Microsoft SQL Server 2008 - Dates

I have a couple of questions in regards to dates in SQL Server.
How do I separate a datetime value "2011-08-10 14:56:17.267" into date and timestamp in two separate columns. Eg. Date "2011-08-10" and timestamp "14:56:17"
I want remove the timestamp from datetime value into "2011-08-10" and still be able to order the data by date (therefore not converted to varchar). Also is there away to change the date value as '10 Aug 2011' and still can sort (not alphabetically but in real date order).
Thank you,
HL
For the first one:
UPDATE atable
SET
DateColumn = CAST(DateTimeColumn AS date),
TimeColumn = CAST(DateTimeColumn AS time)
As for the second one, date display format is something that is unrelated to the date value. You can order the result set by your date column, but in the SELECT clause you can use CONVERT to display the date in the desired format. For example:
SELECT
CONVERT(varchar, DateColumn, 106) AS Date,
…
FROM atable
ORDER BY DateColumn
use CONVERT function with parameters from resource http://www.mssqltips.com/tip.asp?tip=1145
-- simple conversion example:
SELECT CONVERT(VARCHAR(10), GETDATE(), 102) -- for date
SELECT CONVERT(VARCHAR(10), GETDATE(), 8) -- for time