select CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(31 AS VARCHAR)) AS DATE)
Conversion failed when converting date and/or time from character string is coming.
how to check the condition for above problem...
Use ISDATE() function to check whether string is a valid date
Use the query: using ISDate()
IF ISDATE((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-'
+ CAST(31 AS VARCHAR))) = 1
SELECT CAST(CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' +
CAST(31 AS VARCHAR) AS DATETIME)
ELSE
SELECT NULL
Use DateTime.. Also for 9th month no 31st
IF ISDATE(CAST((CAST(2014 AS VARCHAR) + '-' + CAST(9 AS VARCHAR) + '-' + CAST(30 AS VARCHAR)) AS DATETIME)) = 1
PRINT 'VALID'
ELSE
PRINT 'INVALID';
Related
I am trying to run the script below
SELECT
(ISNULL(AMOUNT, '') + '-' + ISNULL(TRAN_ID, '') + '-' + ISNULL(CONVERT(VARCHAR(8), TRAN_DATE, 112), '-')) AS TRAN_ID_NEW,
ID,
ACCOUNT_STATUS,
ACCT_BAL, ACCT_BAL_DATE,
AMOUNT,
AUTH_IP_ADDRESS,
BRANCH_ID,
ENTRY_TIME,
TRAN_CURRENCY,
TRAN_DATE,
TRAN_ID,
TRAN_PARTICULAR,
TRAN_SUB_TYPE, TRAN_TYPE,
VALUE_DATE,
ACCOUNT,
REF_DESC,
ISNULL(REF_DESC, '') + ' ' + ISNULL(TRAN_PARTICULAR, '') + ' ' + ISNULL(AMOUNT, '') AS TRAN_PARTICULAR_NEW
FROM
Txn;
but I get an error:
'Error converting data type varchar to numeric'
Please any solution
My objective, is to select the first available address. If there isn't a AddressCity, then I should select the AddressRegion, and if there is not a AddressRegion I should select the AddressCountry.
IF AddressCity IS NOT NULL
SELECT AddressName + ' is from ' + AddressCity
ELSE
IF AddressRegion IS NOT NULL
SELECT Address+ ' is from ' + AddressRegion
ElSE
IF AddressCountry IS NOT NULL
SELECT AddressName + ' is from ' + AddressCountry
FROM DBO.Address
When I execute it, I get Invalid column name 'AddressCity'
You want something like case or coalesce():
SELECT coalesce(AddressName + ' is from ' + AddressCity,
AddressName + ' is from ' + AddressRegion
AddressName + ' is from ' + AddressCountry
)
FROM DBO.Address;
in database table I got :
int day
int month
int year
I want to write a select query to show the date : dd/mm/yyyy
I try this but didn't work :
1-
select (day + '\' + month + '\' + year) as xdate from table1
but give me an error
2-
select (day + '-' + month + '-' + year) as xdate from table1
it return the sum of these field : 2036 instead of 21/1/2014
any solution ?
Try something like this:
select (cast(day as varchar(2)) + '/'
+ cast(month as varchar(2)) + '/'
+ cast(year as varchar(4))) as xdate
from table1
If TSQL / SQL-SERVER, then + only works to concatenate strings... So do some conversions on your numbers in that case:
select
CONVERT(VARCHAR, day) + '\' + CONVERT(VARCHAR, month) + '\' ... etc.
Since you are Casting int to Varchar, so you need to use CAST or CONVERT function
CAST and CONVERT
Try like this
select (Cast(day AS Varchar(10)) + '\' + Cast(month AS Varchar(10)) + '\' + Cast(year AS Varchar(10))) as xdate from table1
(Or)
select (Cast(day AS Varchar(10)) + '-' + Cast(month AS Varchar(10)) + '-' + Cast(year AS Varchar(10))) as xdate from table1
CONVERT(date, , 103) converts a date to a string representation in dd/mm/yyyy form
SELECT CONVERT(date,DATEFROMPARTS([year],[month],[day]),103)
I have a query that has this syntax
coalesce(cast([tb_groups].[f_member_id_creator] as varchar) + ',', '') + coalesce(cast([tb_groups].[f_member_id_officer1] as varchar) + ',','') + coalesce(cast([tb_groups].[f_member_id_officer2] as varchar) + ',', '') + coalesce(cast([tb_groups].[f_member_id_officer3] as varchar) + ',','') + coalesce(cast([tb_groups].[f_member_id_officer4] as varchar) + ',', '') + coalesce(cast([tb_groups].[f_member_id_officer5] as varchar),'') as admin_list
F_member_id_creator is always present in the db, however f_member_id_officer1 through 5 may be available in any combination.
For example, a returned value from the above statement might be
"25431,32663,,,,"
or "25431,,32663,,,"
or "25431,,,32663,,"
or "25431,,,,32663,"
etc.
What I need to do is tell SQL to remove the empty list elements and just return "25431,32663"
Any way to do this in SQL Server 2005?
Or better yet, have it not select the empty ordinal at all if its null or blank?
YOU got the result because [tb_groups].[f_member_id_creator], maybe, the empty instead of null. Try this:
coalesce(NULLIF(LTRIM(RTRIM(cast([tb_groups].[f_member_id_creator] as varchar))), '') + ',', '') + ...
If you agree that you haven't trailing and/or leading spaces you can omit LTRIM/RTRIM.
I slightly modified above code... I think it works... because it worked for me
COALESCE(CAST([tb_groups].[f_member_id_creator] as varchar), '') +
COALESCE(NULLIF(',' + CAST([tb_groups].[f_member_id_officer1] as varchar),','), '') +
COALESCE(NULLIF(',' + CAST([tb_groups].[f_member_id_officer2] as varchar),','), '') +
COALESCE(NULLIF(',' + CAST([tb_groups].[f_member_id_officer3] as varchar),','), '') +
COALESCE(NULLIF(',' + CAST([tb_groups].[f_member_id_officer4] as varchar),','), '') +
COALESCE(NULLIF(',' + CAST([tb_groups].[f_member_id_officer5] as varchar),','), '') as admin_list
I have a field varchar(14) = 20090226115644
I need convert it to -> 2009-02-26 11:56:44 (datetime format)
My idea. use cast and convert.. but I always have errors.
Conversion failed when converting
datetime from character string.
I made this, but don`t like it..
SELECT
SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2) as new --
FROM [Test].[dbo].[container_events]
where move IS not null
Result :2009-02-26 11:56:44
The CAST operator in SQL Server has a well-defined list of formats which are supported - see on MSDN SQL Server Books Online.
Your format doesn't seem to fit any of those defined formats -> you're on your own, you have to use some custom logic (as you do) to bring that varchar field into a format that CAST can understand.
So what is your question now??
your logic looks correct and works for the given data. however, I'll bet that you have some bad data out there.
try this:
DECLARE #container_events table (PK int, move varchar(14))
SET NOCOUNT ON
INSERT INTO #container_events VALUES (1,'20090226115644')
INSERT INTO #container_events VALUES (2,'20090226116644')
INSERT INTO #container_events VALUES (3,'20090227010203')
INSERT INTO #container_events VALUES (4,'20090228010203')
INSERT INTO #container_events VALUES (5,'20090229010203')
SET NOCOUNT OFF
---list all bad dates
SELECT
SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2) as BadDatesOnly, move
FROM #container_events
where ISDATE(SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2))=0
---list all bad dates
SELECT
CONVERT(datetime,SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2)) as GoodDatesOnly, move
FROM #container_events
where ISDATE(SUBSTRING(move,1,4) + '-' + SUBSTRING(move,5,2) + '-' + SUBSTRING(move,7,2) + ' ' + SUBSTRING(move,9,2) + ':' + SUBSTRING(move,11,2) + ':'+SUBSTRING(move,13,2))=1
OUTPUT:
BadDatesOnly move
------------------- --------------
2009-02-26 11:66:44 20090226116644
2009-02-29 01:02:03 20090229010203
(2 row(s) affected)
GoodDatesOnly move
----------------------- --------------
2009-02-26 11:56:44.000 20090226115644
2009-02-27 01:02:03.000 20090227010203
2009-02-28 01:02:03.000 20090228010203
(3 row(s) affected)
Using the ISDATE (Transact-SQL) function you can determine if the date is valid or not. AS a result you can filter out the bad rows in your query, or find the bad rows and fix them, etc. it is up to you what to do with the bad data.