I'm selecting a date from SQL Server, but having an issue. If I inserted the data as 06/06/2012, it gets saved in this format in SQL Server. When I SELECT it from the table, it looks like 6/6/2012.
I tried to use this:
SELECT
FirstName, LastName,
CONVERT(VARCHAR, DOB, 101)
FROM
TblClients
but I get an error:
ITEM cannot be found in the collection corresponding to this name or ordinal.
Sounds like your VB code is fine until you don't return the expected "ITEM".
Try aliasing your convert with the column name:
SELECT FirstName, LastName, convert(varchar,DOB, 101) AS DOB From TblClients
What I needed to do is compare the value from tblClients and value from a mskDOB.text. So what I did is I converted both to string, formatted them in this way--> "YYYYMMDD", compared them and it works.
Related
I need help redesigning a view. Currently it is giving an error when you do a simple select of the view. The error is as follows.
Error converting data type nvarchar to bigint
This is the current design of the view.
SELECT
FirstName,
MiddleName,
LastName,
CONVERT(bigint, SUBSTRING(Reference, 2, 10)) AS LogNum,
Address,
Birthday,
Gender,
BirthDate,
Gender,
ClientCode
FROM
dbo.Client
WHERE
(Reference LIKE 'C%')
The issue is happening because the reference column which has a nvcarchar(16) datatype has values that are as follows.
'C3456423445'
'2234567310'
'C8921244532'
The substring function basically strips out the "C" and returns the 10 digit numbers after it as a bigint.
What is happening now is there is new data that came into the client table that has values for reference column as follows
"CC4309842387"
"CC29383761760"
Since there is an extra C in the new values, the function is not doing its job.
I need to redesign the view so that it can handle both iterations of the values in the reference column. What is essential is the 10 digit numbers coming out intact for the front end report.
Thank you for your help.
If there are only two options "C" and "CC", then you can use REPLACE with LEFT instead of SUBSTRING.
SELECT
FirstName,
MiddleName,
LastName,
Convert(bigint,Left(Replace(Reference, 'C',''),10)) AS LogNum,
Address,
Birthday,
Gender,
BirthDate,
Gender,
ClientCode
FROM dbo.Client
WHERE (Reference LIKE 'C%')
If other than preceding C's, you can use patindex() to find the position of the 1st numeric
Also, I prefer try_convert() because if the conversion fails, it will return a NULL rather than throwing an error.
Example
Declare #YourTable Table ([Reference] nvarchar(50)) Insert Into #YourTable Values
('C3456423445')
,('2234567310')
,('C8921244532')
,('CC29383761760')
,('XX29383761760')
Select *
,NewValue = try_convert(bigint,substring([Reference],patindex('%[0-9]%',[Reference]),25))
from #YourTable
Results
Reference NewValue
C3456423445 3456423445
2234567310 2234567310
C8921244532 8921244532
CC29383761760 29383761760
XX29383761760 29383761760
I am trying to create a view on a table called petients in my database. The table has five columns. One of them is the column which I want to keep patient admitted date. It data type is datetime so I want to create a query that filters the data in this table based on current date. For example I want create a view that shows only details of petients who have been recorded on the current day.
Here is my code:
CREATE VIEW [dbo].[recent petients]
AS
SELECT petient_id, name, age, contact
FROM [petients]
WHERE [date] = 'date.Today'
I am getting an error saying that failed to convert date to string. Can you help me to solve it, or where is my code wrong?
Your code looks like SQL Server code. If so, I would recommend:
SELECT petient_id, name, age, contact
FROM [patients]
WHERE [date] = CONVERT(date, GETDATE());
As a note: This version is much better than DATEDIFF() because it allows the use of an index on patient([date]).
If the "date" column has a time component, you can use:
WHERE CONVERT(date, [date]) = CONVERT(date, GETDATE())
Note that this is also index-safe in SQL Server.
I'm assuming you are using Transact-SQL from Microsoft SQL Server, but you should specify the sql dialect you are using.
Since the datetime field type generally includes also a time, it is better to use the DATEDIFF function: https://learn.microsoft.com/it-it/sql/t-sql/functions/datediff-transact-sql?view=sql-server-ver15
In your case, to consider only the record where date=today, the difference in days must be zero:
--SQL QUERY
WHERE DATEDIFF(day, GETDATE(), [date]) = 0
day identifies the element you want to consider the difference. A list of names or abbreviations can be found in the link
GETDATE() returns now datetime
2nd and 3rd arguments are the dates you want to make the difference between
Using SQL Server 2014, I have a date field named LAST_BASELINE_UPDATE_DATE that is stored as datetime.
I used the CONVERT function to convert to mm/dd/yyyy:
convert(date,LAST_BASELINE_UPDATE_DATE,101) as BASELINE_UPDATE_DATE
What I want to be able to do is then select the MAX value of BASELINE_UPDATE_DATE without creating a table. Is this even possible?
It's a little unclear from your post what your data is and what you're trying to get out. Here are a couple solutions, hopefully one of which is applicable
Assuming you want your result as a string formatted mm/dd/yyyy you can do this
select convert(varchar(10), max(LAST_BASELINE_UPDATE_DATE), 101))
from YourTable
If you just need it as a date, just do
select max(LAST_BASELINE_UPDATE_DATE)
from YourTable
if LAST_BASELINE_UPDATE_DATE is already a string (formatted mm/dd/yyyy) and you want it as a date,
select max(convert(date, LAST_BASELINE_UPDATE_DATE, 101))
from YourTable
I think you are complicating this. Just do the conversion on the max datetime values.
declare #table table (LAST_BASELINE_UPDATE_DATE datetime)
insert into #table
values
('20160701 12:21'),
('20160705 03:21'),
('20160401 19:21'),
('20161201 04:21')
select
convert(varchar(10),max(LAST_BASELINE_UPDATE_DATE),101)
from
#table
This method converts your single returned row, which is the max() value of your datetime columns, as opposed to converting every row and then finding the max value.
Good day!
I have 2 questions how to update a date data type column field using varchar and numeric column field
1.)mydate varchar(8)--> varchar column field
SELECT mydate from mytable
Result: 20141120
my question is how can I update my date column field using my varchar column field using cast or convert
update table2
set date = (select mydate from mytable)
which I get an error!!! and I'm stuck.
2.)mydate numeric(8) --> numeric column field
SELECT mydate from mytable
Result:
20101015
20140910
etc.......
update table2
set date = (select mydate from mytable a, mytable2 b
where a.id=b.id)
my question is how can I update my date column field using my numeric column field using cast or convert
I used different CAST and CONVERT but still I'm getting error!
What is the correct syntax for this?
Thank your for your help!
To convert a string to a date field you will need to use the CONVERT function:
CONVERT(datetime, mydate, 101)
This is expecting a string field, so if your mydate field is really a numeric field then you will need to CAST that to a string, so the CONVERT command will then look like:
CONVERT(datetime, CAST(mydate as VarChar), 101)
The third parameter of the function is determined by the format of the date in the previous parameter, you can find the full list on MSDN at http://msdn.microsoft.com/en-us/library/ms187928.aspx
I need to use a select query to create a dummy column and pad it with a constant value. I found a partial answer to my question in a related post, here was the answer:
select name, address, 'No' as vacationing, Zipcode from mytable;
My question is what do you do if you want to pad it with a number, such as:
'9999' AS vacationing - I get an error with that
Also, what if I want the cell to be empty, such as;
'' AS vacationing - I get an error here also
TIA
'9999' AS vacationing is perfectly valid.
you must be using union with different column type - hence error.
SELECT name, address, '9999' AS vacationing, Zipcode FROM mytable;
And:
SELECT name, address, '' AS vacationing, Zipcode FROM mytable;
Are both valid T-SQL queries. You don't say what error you're getting, but neither of these should throw an error.