Sql 当存储过程中存在空值时,如何设置列的默认值
您好,我正在使用存储过程从不同的表中获取数据。这里是我的存储过程Sql 当存储过程中存在空值时,如何设置列的默认值,sql,sql-server,stored-procedures,default-value,Sql,Sql Server,Stored Procedures,Default Value,您好,我正在使用存储过程从不同的表中获取数据。这里是我的存储过程 SELECT ev.Id, ev.Title, ev.PageUrl, ev.FromDate, ev.ToDate, ev.Isactive, CONVERT(char(10), eventtime, 108) as EventTime, ev.UserType, ev.Street, ev.Image, ev.Description, ev.City, ev.Countr
SELECT
ev.Id,
ev.Title,
ev.PageUrl,
ev.FromDate,
ev.ToDate,
ev.Isactive,
CONVERT(char(10), eventtime, 108) as EventTime,
ev.UserType,
ev.Street,
ev.Image,
ev.Description,
ev.City,
ev.CountryCode,
ev.CategoryId,
ev.UserId,
ev.StateCode,
cm.Name as 'CountryName',
sm.name as 'StateName',
asp.FirstName as 'FirstName',
Cat.Name as 'CategoryName',
ev.ZipCode
from events ev
inner join countrymaster cm on ev.CountryCode=cm.Id
inner join statemaster sm on ev.StateCode=sm.Id
inner join category cat on ev.Categoryid=cat.Id
left join aspnetusers asp on ev.userid=asp.Id
order by createddate desc
第七栏
CONVERT(char(10), eventtime, 108) as EventTime,
我通过塑造人物来获得活动时间
但是当我的事件时间为空时,它抛出如下错误
从“System.String”到“System.TimeSpan”的强制转换无效。
事件时间的数据类型为时间
因此,如果eventtime列中没有值,如何设置该列的默认值。在该列上使用
COALESCE
:
CONVERT(char(10), COALESCE(eventtime, GETDATE()), 108) AS EventTime
这将使用当前日期/时间作为默认值,尽管您可以使用此方法中所需的任何默认值。使用
CASE
expression like
CASE WHEN eventtime IS NULL THEN GETDATE()
ELSE CONVERT(char(10), eventtime, 108) END AS EventTime,
使用ISNULL()
检查EventTime是否为NULL
。如果为NULL,则可以根据您的选择替换空字符串“”或其他日期
CONVERT(char(10), ISNULL(eventtime,''), 108) as EventTime // If you want to replace with empty string
CONVERT(char(10), ISNULL(eventtime,GETDATE()), 108) as EventTime // If you want to replace with current date
DECLARE @default datetime='2016-12-22 16:43:22.560'
CONVERT(char(10), ISNULL(eventtime,@default), 108) as EventTime // If you want to relace it with some variable.
请使用
将(char(10),isnull(eventtime,getdate()),108)转换为eventtime
CONVERT(char(10), ISNULL(eventtime,''), 108) as EventTime // If you want to replace with empty string
CONVERT(char(10), ISNULL(eventtime,GETDATE()), 108) as EventTime // If you want to replace with current date
DECLARE @default datetime='2016-12-22 16:43:22.560'
CONVERT(char(10), ISNULL(eventtime,@default), 108) as EventTime // If you want to relace it with some variable.