SQL Server CONVERT and CAST not returning the correct date format - sql

I have a table in SQL Server with a column RGSTR_DATE that is of varchar(10) data type, and it has values like 2016-01-23, 1998-08-12, etc...
I want to select and convert those values to 20160123, 19980812, etc...
I tried running these queries:
SELECT CONVERT(DATE, CAST(RGSTR_DATE AS DATE), 112)
FROM [project].[dbo].[my_table];
SELECT CONVERT(VARCHAR(10), RGSTR_DATE, 112)
FROM [project].[dbo].[my_table];
But the results that came back were still 2016-01-23, 1998-08-12 etc...
What am I doing wrong?

Did you try
SELECT CONVERT(VARCHAR(10),cast(RGSTR_DATE as date),112)

You're converting a varchar to a date, but all you need to do is remove the hyphens.
SELECT REPLACE(RGSTR_DATE, '-', '')
FROM [project].[dbo].[my_table]

Related

Convert from varchar(50) to date

I have imported csv file to sql server manager. one of column is date but his data type is vachar(50).
I used the basic (cast, convert, try_parse) but still get message:
(Conversion failed when converting date and/or time from character
string.)
could you please help me :)
select cast(
replace(
case
when [factuur_datum] in ('null','')
then null
else [factuur_datum]
end, '-','') as date) as [factuur_datum]
from [dbo].[verkoop]
Please use below query to convert it into date
convert(date,'20-12-2018',105)
This should do what you intend:
select try_cast(replace(factuur_datum, '-', '') as date) as factuur_datum
from [dbo].[verkoop]
Non-dates will result in NULL.
I take it factuur_datum is the date column:
select
cast(factuur_datum as date) as [Alias]
from [dbo].[verkoop]
Not sure what the data looks like so can you include an example?
EDIT
convert(datetime,'factuur_datum',1)
possibly will error out if there are null values so you may need to case out the nulls then convert.

CONVERT throw Error converting data type date to varchar

I've got a table INTEG_LOG which contains a date column DATE_LOG. From that date, I need to extract the date in the format 20171907 and the hour like 094225.
In the first place I tried to get my date. For that I use this line:
SELECT
REPLACE(CONVERT(VARCHAR(10), DATE_LOG, 111), '/', '') AS LOG_DATE
FROM
INTEG_LOG
It's not really clean but it's working fine.
I decided to do the same for the hour with this line:
SELECT
REPLACE(CONVERT(VARCHAR(8), DATE_LOG, 108), ':', '') AS LOG_HOUR
FROM
INTEG_LOG
But this SQL throws
Error converting data type date to varchar
I even tried without the replace and I still get the same error.
I don't really get why the first convert with the style 111 is working fine but not the 108.
I'm using SQL Server 2008 R2.
Thanks
I've got a table INTEG_LOG which contains a date column DATE_LOG
The error you've got is legitimate because the date type has no time component, so you cannot convert it to string using style 108
Try this
select REPLACE(CONVERT(VARCHAR(100), CAST(getdate() AS DATE), 108), ':', '')
and you'll get the same error.
So you should just select a constant like this:
select '000000' AS LOG_HOUR
or alternatively cast your date to datetime and you'll get the thame using your code:
SELECT
REPLACE(CONVERT(VARCHAR(8), cast(DATE_LOG as datetime), 108), ':', '') AS LOG_HOUR
FROM
INTEG_LOG

Convert date to nvarchar and merge two columns

I am using SQL Server 2008 and I have two columns in date format:
Column_1: [2014-12-19]
Column_2: [2015-08-31]
I want to merge them and change the the data type to NVARCHAR.
I tried this code
CONVERT(NVARCHAR,[ Column_1])+CONVERT(NVARCHAR,[Column_2])AS TEST
but I get this result:
2014-12-192015-08-31
instead of 2014121920150831 without the hyphens.
Could you please help?
Thanks
Although you can fiddle around with conversion codes, just use replace:
REPLACE(CONVERT(NVARCHAR(255), Column_1) + CONVERT(NVARCHAR(255), Column_2), '-', '') AS TEST
Or, if you don't want to be dependent on the local date format:
CONVERT(NVARCHAR(255), Column_1, 112) + CONVERT(NVARCHAR(255), Column_2, 112) AS TEST
CONVERT has a third parameter which determines the format of the date/time. See here for definition. Code 112 will give you what you want.
You can also use REPLACE to remove the hyphens.
Try this
DECLARE #date1 date = '2014-12-19',
#date2 date = '2015-08-31'
SELECT CONVERT(VARCHAR(8), #date1, 112)+CONVERT(VARCHAR(8), #date2, 112)

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.

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.